I am trying to calculate and graph the variations of g with height for the different planets I have in my variables, but once i hit run i get the graph, but no line being plotted. This is the code
clc;
load project_1.mat
g= @(x,M)((G*M)./(x.^2));
dist = vpa(linspace(1,10^8,1000));
figure
hold on
g_Earth = g(dist, Mass_Earth);
Unrecognized function or variable 'Mass_Earth'.
g_Jupiter = g(dist, Mass_Jupiter);
g_Mars = g(dist, Mass_Mars);
g_Moon = g(dist, Mass_Moon);
plot(dist, g_Earth)
plot(dist, g_Jupiter)
plot(dist, g_Mars)
plot(dist, g_Moon)
xlabel('Height from surface of planet');
ylabel('Acceleration due to gravity');
title('g vs Height from surface of planets')
legend({'Earth','Jupiter','Mars','Moon'})
savefig('task3_graph.fig')

3 Comments

Torsten
Torsten on 13 Sep 2022
G is undefined.
hold on freezes the axes limits. After you plot
xlim auto; ylim auto
Bryan
Bryan on 13 Sep 2022
how would you go to define it then? I placed G as G = 6.6743*10^-11; %(m^3)(kg^-1)(s^-2)in the workspace but I still get the same issue.

Sign in to comment.

 Accepted Answer

Tested.
Your .mat file does not contain anywhere near what you thought it does. Instead there is a 3 x 1 string array for each object, with the strings being the character representation of the measurements. You need to convert the string to numeric form to do calculations.
load project_1.mat
Mass_Earth = double(EarthArray(1));
%r_Earth = double(EarthArray(2));
Mass_Jupiter = double(JupiterArray(1));
%r_Jupiter = double(JupiterArray(2));
Mass_Mars = double(MarsArray(1));
%r_Mars = double(MarsArray(2));
Mass_Moon = double(MoonArray(1));
%r_Moon = double(MoonArray(2));
G = 6.6743*10^-11; %(m^3)(kg^-1)(s^-2)
dist = linspace(1,10^8,1000);
g= @(x,M)((G*M)./(x.^2));
figure
hold on
g_Earth = g(dist, Mass_Earth);
g_Jupiter = g(dist, Mass_Jupiter);
g_Mars = g(dist, Mass_Mars);
g_Moon = g(dist, Mass_Moon);
plot(dist, g_Earth)
plot(dist, g_Jupiter)
plot(dist, g_Mars)
plot(dist, g_Moon)
set(gca, 'YScale', 'log')
xlabel('Height from surface of planet');
ylabel('Acceleration due to gravity');
title('g vs Height from surface of planets')
legend({'Earth','Jupiter','Mars','Moon'})
savefig('task3_graph.fig')

3 Comments

Bryan
Bryan on 13 Sep 2022
it worked ! thank you very much.
xlabel('Height from surface of planet');
No, the values are distance from the center of the planet, not from the surface of the planet. Have a look
g= @(x,M)((G*M)./(x.^2));
The surface of the planet is distance 0 above the surface of the planet, so if we examine the value x = 0, if the label is correct, we should get the gravity at the surface of the planet. But we have a division by 0.
If you want x to be height above surface then you need different code.
load project_1.mat
Mass_Earth = double(EarthArray(1));
r_Earth = double(EarthArray(2));
Mass_Jupiter = double(JupiterArray(1));
r_Jupiter = double(JupiterArray(2));
Mass_Mars = double(MarsArray(1));
r_Mars = double(MarsArray(2));
Mass_Moon = double(MoonArray(1));
r_Moon = double(MoonArray(2));
G = 6.6743*10^-11; %(m^3)(kg^-1)(s^-2)
dist = linspace(1,10^8,1000);
g= @(x,M,r)((G*M)./((x+r).^2));
figure
hold on
g_Earth = g(dist, Mass_Earth, r_Earth);
g_Jupiter = g(dist, Mass_Jupiter, r_Jupiter);
g_Mars = g(dist, Mass_Mars, r_Mars);
g_Moon = g(dist, Mass_Moon, r_Moon);
plot(dist, g_Earth)
plot(dist, g_Jupiter)
plot(dist, g_Mars)
plot(dist, g_Moon)
set(gca, 'YScale', 'log')
xlabel('Height from surface of planet');
ylabel('Acceleration due to gravity');
title('g vs Height from surface of planets')
legend({'Earth','Jupiter','Mars','Moon'})
savefig('task3_graph.fig')

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!