MATLAB: Write a MATLAB script and user defined function that solves the following orbital equation of motion using ode45

ode45

how do i set this up in a code? r ⃗ '' ̈=-μ/r^3 r ⃗
where r ⃗=[(x(t) y(t) z(t))] and μ=398600 〖km〗^3/s^2
Test your algorithm using the following initial condition
x(0)=43278 km y(0)=0 z(0)=0
x ̇(0)=0 y ̇(0)=√(μ/43278) km/s z ̇(0)=0

Best Answer

Your code is not even remotely close to what it needs to be for this. In the first place, since you have a 2nd order ODE in 3-space, the size of the state vector will need to be 2 * 3 = 6 elements. So you will have a 6 element state vector. The first three elements are associated with "x", "y", "z", and the last three elements will be associated with "xdot", "ydot", "zdot".
You need to write a derivative function that is of this form (taken from the doc for ode45):
% File orbit_derivative.m
% The y vector is a 6-element vector
function dy = orbit_derivative(t,y)
dy = zeros(6,1);
dy(1:3) = _________; % <-- you fill this in

dy(4:6) = _________; % <-- you fill this in
return
end
The y vector has 6 elements as noted above. Using the definitions of what these elements are from above:
dy(1) = The derivative of "x" with respect to time = "xdot" = y(4)
dy(2) = The derivative of "y" with respect to time = "ydot" = y(5)
dy(3) = The derivative of "z" with respect to time = "zdot" = y(6)
For the dy(4), dy(5), and dy(6) stuff, you need to derive what these would be based on your gravity formula you posted above. It will involve first deriving the r value:
r = norm(y(1:3));
Then use this r value with your gravity formula to get expressions for dy(4), dy(5), and dy(6).
Once you get all of this done, you will need to create another file that calls this derivative function using ode45. That file will have:
% You create initial conditions here
% You create function handle to your derivative function here
% You call ode45 here
You need to create code for all of the comment lines I have above. Just remember to put everything in terms of your 6-element state vector y.