Howdy, I need to write two separate scripts in matlab. The first one uses the va
ID: 3818372 • Letter: H
Question
Howdy, I need to write two separate scripts in matlab. The first one uses the variables mass 1, mass 2, distance, and a given gravitational constant(G) to compute the gravity between two objects. In the second script, I am supposed to ask the user for their weight (mass 1), and then call the first script to compute their weight on the moon. I am given the mass of the moon (mass2) and the radius of the moon (distance) to use in the second script, however when writing the first script, I am supposed to assume the variables mass1, mass2, and distance already exist. How do I get the first script to run using these variables without assigning them a number, so that when I call it into the second script it recognizes the values assigned to the variables? Thanks
Explanation / Answer
% You need to use a function if you want to pass the values dynamically
% Script-1
function force = compute_gravity_force(m1, m2, d, G)
force = (G*m1*m2)/power(d,2);
end
% Script-2
moon_mass = 7.35 * 10^22;
moon_radius = 1.737 * 10^6;
G = 6.673 * 10^-11;
user_mass = input("Enter your mass : ");
weight = compute_gravity_force(user_mass, moon_mass, moon_radius, G);
fprintf("Your weight on the moon is %f (Kg.m/s^2) ",weight);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.