What is wrong with my code, my equation is vdv/dy=-G*M/(y+R)^2 ?
3 views (last 30 days)
Show older comments
cssCopy codefunction dydt = gravity(y, v, G, M, R)
% y: height above surface
% v: velocity at height y
% G: gravitational constant
% M: mass of the planet
% R: radius of the planet
dydt(1,1) = v; % v = dy/dt
dydt(2,1) = -G*M/(y+R)^2; % a = d^2y/dt^2 = dv/dt
end
scssCopy codeG = 6.67430e-11; % gravitational constant
M = 5.9722e24; % mass of Earth
R = 6.371e6; % radius of Earth
7 Comments
Dyuman Joshi
on 4 Apr 2023
Edited: Dyuman Joshi
on 4 Apr 2023
@Laura I am well aware that you are trying to solve the equation by MATLAB, that is not my point.
For what time range are you trying to solve the equation? What are the initial conditions to the differential equation?
Answers (2)
Sam Chak
on 4 Apr 2023
Edited: Sam Chak
on 4 Apr 2023
Hi @Laura
This is a simple example of using the solver's syntax that you can find in the ode45() documentation. If there are universal constants that they don't change, then place them inside the ode function, as suggested by @Dyuman Joshi. Otherwise, parameters that you would like to test with different value (i.e., mass), then you can pass the parameters to the ode function like @Cris LaPierre did.
What you need to do is to replace my free fall ODE with your description of Newton's law of universal gravitation.
Logically, when the object (point mass) hits the ground, then the simulation should stop. Anyhow, get familiar with the basic code. Click into the documentation for more examples.
% solving the ODE
tspan = [0 6]; % simulation time
y0 = [100 0]; % initial condition y(1) = 100 m; y(2) = 0 m/s
[t, y] = ode45(@freefall, tspan, y0);
% plotting the solution y(1)
plot(t, y(:,1), 'linewidth', 1.5),
xlabel('Time (sec)'), ylabel('Height (m)')
yline(0, '--', 'Ground level');
yregion(-80, 0)
% Describing the ODE
function dydt = freefall(t, y)
m = 1; % point mass
g = 9.8203; % gravity
dydt(1,1) = y(2); % dy/dt
dydt(2,1) = -g/m; % assume no air drag
end
0 Comments
Torsten
on 4 Apr 2023
syms G M R y v(y)
Dv = diff(v,y);
eqn = diff(v,y,2) == -G*M/(y+R)^2;
cond = [v(0)==100,Dv(0)==0];
sol = dsolve(eqn,cond)
Gnum = 6.67430e-11; % gravitational constant
Mnum = 5.9722e24; % mass of Earth
Rnum = 6.371e6; % radius of Earth
sol = vpa(subs(sol,[G M R],[Gnum,Mnum,Rnum]))
fplot(sol,[0 6])
2 Comments
Dyuman Joshi
on 4 Apr 2023
@Torsten, the equation you wrote is incorrect.
It's
v*diff(v,y)
instead of
diff(v,y,2)
And it needs only 1 intial condition
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!