I cannot figure out why my code (to solve for a system of ODE's) is running continuously. Every time I run it, I have to use CTRL+C to stop it. Please help! The code is:
clear
clc
%Error in ODE solver, will not "stop" solving.
fprintf('\nQuestion 3\n');
tspan= [0:.05:20];
F0=[2.5; 3; -1; 1];
[t,fn]=ode45(@fn_feq3,tspan,F0);
y=fn(:,1);
x=fn(:,2);
plot(t,x,t,y);
xlabel('Time (sec)');
ylabel('x and y');
legend('x vs t','y vs t');
title('#3 Plot');
function dF=fn_feq3(t,f)
dy=f(1);
dx=f(2);
dz=(2*f(2))+(-6*f(1))+(-3*f(1)*f(3));
dw=14-2*f(4)-2*f(2)*f(1);
dF=[dy;dx;dz;dw];
end

 Réponse acceptée

Walter Roberson
Walter Roberson le 10 Déc 2017

0 votes

One of your outputs is heading to -infinity. When it reaches about -1E12 then MATLAB needs to take a lot of smaller steps to try to maintain integration tolerances.
If you switch to ode15s then you will fairly quickly get a final result around -9E17

3 commentaires

Antoine Mora
Antoine Mora le 10 Déc 2017
So I had this problem in a final exam today where we were instructed to use ode45 (as that was the only ode solver we learned) so how would I have fixed this so that it would run using ode45?
Thank you!
Walter Roberson
Walter Roberson le 10 Déc 2017
You could either just let it run until it finishes or you could pass in an options structure that permits less accurate integration tolerance
Antoine Mora
Antoine Mora le 10 Déc 2017
I'll try that out. Thank you

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by