Function Issue with ODE45
Show older comments
Hello,
I have this function which I am trying to pass to ODE45. I do not understand why my X array is all zeros. Does anyone know why? Thank you.
clear
clc
tspan = [0,6];
options = odeset('RelTol', 1e-7, 'AbsTol', 1e-7);
%I.C's
x0= [0;0;0];
rdot0= [0;0;0];
psi0= [0;0;0];
phi0= [0;0;0];
theta0=[0;0;0];
p0= [0;0;0];
q0= [0;0;0];
r0= [0;0;0];
[t,X] = ode45(@xdot,tspan,[x0,rdot0,psi0,theta0,phi0,p0,q0,r0],options);
plot(X)
function x = xdot(t,x)
g = 9.8;
m = 0.450;
l = 0.225;
k = 2.98e-6;
b = 1.14e-6;
omegaih=4*m*g;
omega1h=m*g;
omega2h=m*g;
omega3h=m*g;
omega4h=m*g;
if t <= 1
omega1 = omegaih + 70*sin((2*pi*t)/4);
omega2 = omegaih + 70*sin((2*pi*t)/4);
omega3 = omegaih + 70*sin((2*pi*t)/4);
omega4 = omegaih + 70*sin((2*pi*t)/4);
elseif t <= 2
omega1 = omegaih - 77*sin((2*pi*t)/4);
omega2 = omegaih - 77*sin((2*pi*t)/4);
omega3 = omegaih - 77*sin((2*pi*t)/4);
omega4 = omegaih - 77*sin((2*pi*t)/4);
elseif t <= 3
omega1=m*g;
omega2 = sqrt(omega2h^2 - 70^2*sin((2*pi*(t-2))/4));
omega3=m*g;
omega4 = sqrt(omega4h^2 + 70^2*sin((2*pi*(t-2))/4));
elseif t <= 4
omega1=m*g;
omega2 = sqrt(omega2h^2 + 70^2*sin((2*pi*(t-2))/4));
omega3=m*g;
omega4 = sqrt(omega4h^2 - 70^2*sin((2*pi*(t-2))/4));
elseif t <= 5
omega1 = sqrt(omega1h^2 - 70^2*sin((2*pi*(t-4))/4));
omega2=m*g;
omega3 = sqrt(omega3h^2 + 70^2*sin((2*pi*(t-4))/4));
omega4=m*g;
elseif t <= 6
omega1 = sqrt(omega1h^2 + 70^2*sin((2*pi*(t-4))/4));
omega2=m*g;
omega3 = sqrt(omega3h^2 - 70^2*sin((2*pi*(t-4))/4));
omega4=m*g;
end
xdot=zeros(12,1);
T = 4*k*(omega1^2 +omega2^2 + omega3^2 +omega4^2);
%State Space
xdot(1) = x(4);
xdot(2) = x(5);
xdot(3) = x(6);
xdot(4) = (T*(sin(x(7))*sin(x(9)) + cos(x(7))*cos(x(9))*sin(x(8))))/m;
xdot(5) = -(T*(cos(x(9))*sin(x(7)) - cos(x(7))*sin(x(8))*sin(x(9))))/m;
xdot(6) = (T*cos(x(7))*cos(x(8)))/m;
xdot(7) = x(10) + (x(12)*cos(x(7))*sin(x(8)))/(cos(x(8))*cos(x(7))^2 + cos(x(8))*sin(x(7))^2) + (x(11)*sin(x(7))*sin(x(8)))/(cos(x(8))*cos(x(7))^2 + cos(x(8))*sin(x(7))^2);
xdot(8) = (x(11)*cos(x(7))/(cos(x(7)))^2 + sin(x(7))^2) - (x(12)*sin(x(7)))/(cos(x(7))^2 + sin(x(7))^2);
xdot(9) = (x(12)*cos(x(7)))/(cos(x(8))*cos(x(7))^2 + cos(x(8))*sin(x(7))^2) + (x(11)*sin(x(7)))/(cos(x(8))*cos(x(7))^2 + cos(x(8))*sin(x(7))^2);
xdot(10) =(3166346726764097*omega4^2)/4722366482869645213696 - (79*x(8)*x(9))/20000 - (3166346726764097*omega2^2)/4722366482869645213696;
xdot(11) =(3166346726764097*omega3^2)/4722366482869645213696 + (79*x(7)*x(9))/20000 - (3166346726764097*omega1^2)/4722366482869645213696;
xdot(12) =(1345874447617849*omega1^2)/1180591620717411303424 + (1345874447617849*omega3^2)/1180591620717411303424 - (1345874447617849*omega2^2)/1180591620717411303424 - (1345874447617849*omega4^2)/1180591620717411303424;
end
Accepted Answer
More Answers (0)
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!