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

 채택된 답변

Walter Roberson
Walter Roberson 2017년 12월 10일

0 개 추천

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

Antoine Mora
Antoine Mora 2017년 12월 10일
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 2017년 12월 10일
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 2017년 12월 10일
I'll try that out. Thank you

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

질문:

2017년 12월 10일

댓글:

2017년 12월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by