ode45 returns NAN, HOW could I modify it?

3 次查看(过去 30 天)
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

回答(2 个)

madhan ravi
madhan ravi 2018-12-25
编辑:madhan ravi 2018-12-25
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

Star Strider
Star Strider 2018-12-25
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).

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by