I have three 2nd order differential equations with my initial conditions and I'm trying to use the ode45 function in matlab to solve this. I wish to get the solution where my output is x,y,z position vs. time plot(2nd derivative) as well as a dx,dy,dz velocity vs. time plot. I get multiple errors and I'm not sure how to fix it. Here is my code:
%Clohessy-Wiltshire Equations
% d2x = 2*n*dy + 3*(n^2)*x;
% d2y = -2*n*dx;
% d2z = (-n^2)*z;
%
% %Initial Conditions
% x(0) = -0.066538073651029; %km
% y(0) = 0.186268907590665; %km
% z(0) = 0.000003725378152; %km
% dx(0) = -0.000052436200437; %km/s
% dy(0) = 0.000154811363681; %km/s
% dz(0) = 0.000210975508077; %km/s
%Constants
a = 6793.137; %km
mu = 398600.5; %km^3/s^2
n = sqrt(mu/a^3);t0 = 0; %seconds
tf = 5400; %secondsinitial_condition = [x(0) y(0) z(0)][T,position] = ode45(@(t,position)Clohessy_Wiltshire(t,x,y,z),[t0 tf],initial_condition);figure(1), hold on, plot(T,position(:,1),'b'), plot(T,position(:,2), 'r'), plot(T,position(:,3), 'g')title('Position(X,Y,Z) vs. Time')ylabel('Position(X,Y,Z)(km)')xlabel('Time(s)')figure(2), hold on, plot(T,position(:,4),'b'), plot(T,position(:,5), 'r'), plot(T,position(:,6), 'g')title('Velocity(dX,dY,dZ) vs. Time')ylabel('Velocity(dX,dY,dZ)')xlabel('Time(s)')function position = Clohessy_Wiltshire(~,x,y,z,dx,dy,dz,n)x(0) = -0.066538073651029;dx(0) = -0.000052436200437;dx(2) = 2*n*dy;y(0) = 0.186268907590665;dy(0) = 0.000154811363681;dy(2) = -2*n*dx;z(0) = 0.000003725378152;dz(0) = 0.000210975508077;dz(2) = -n^2*z;end
Best Answer