ode45 returns NAN, HOW could I modify it?

clear
clc
[t,x]=ode45(@eqns,[0.1:0.2:100],[10;0.01;100;-0.0225;10;-0.01]);
for i=1:6
plot(t,x(:,i))
hold on
end
function dxdt = eqns(t,x) % function definition
m=9.0807;
v1=6;
v2=150;
rc=1;
dxdt = [x(2);
2*v1*x(4)+v2*x(3)+v1^2*x(1)-(m*(rc+x(1)))/(((rc+x(1))^2+(x(3))^2+(x(5))^2)^1.5)+(m/rc^2);
x(4);
2*v1*x(2)-v2*x(1)+(v1^2)*x(3)-m*x(3)/(((rc+x(1))^2+x(3)^2+x(5)^2)^1.5);
x(6);
(-m*x(5))/(((rc+x(1))^2+x(3)^2+x(5)^2)^1.5);]
end

Answers (2)

madhan ravi
madhan ravi on 25 Dec 2018
Edited: madhan ravi on 25 Dec 2018
Reduce the time span and increase time steps (or don't put any time step let matlab assume it) for instance from 0 to 1 -> [0 1] .
Add figure before plot(...) and remove hold on so that each solution can be viewed nicely.
[t,x]=ode45(@eqns,[0 10],[10;0.01;100;-0.0225;10;-0.01]); % change to this
% ^^----example
for i=1:6
figure % add this
plot(t,x(:,i))
% hold on %% remove this
end
After about ‘t=60’, your differential equation saturates (becomes greater than realmax (link)), and subsequent values are NaN. You need to examine your differential equations to be certain that you have coded them correctly, and that the constants are correct.
Take a look at a higher-resolution version of the output of your system:
tspan = linspace(0, 10, 5000);
[t,x]=ode45(@eqns,tspan,[10;0.01;100;-0.0225;10;-0.01]);
semilogy(t,abs(x))
That could give you some idea of where the problems are.
Also see the documentation section on how to Debug a MATLAB Program (link).

Tags

Asked:

on 25 Dec 2018

Edited:

on 25 Dec 2018

Community Treasure Hunt

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

Start Hunting!