t0 = 0;
tf = 0.4;
h = 0.1;
y0 = [2,4];
f = @(y,t) [-2*y(1) + 5*exp(-t); (-y(1).*(y(2).^2))/2];
prob2res1 = myEuler3(f,y0,t0,tf,h)
[t, prob2res4] = ode45(f,[0,0.4],y0)
my euler function is below
function [sol] = myEuler3(F, y0, ti, tf, h)
t = ti:h:tf;
y = zeros(length(y0),length(t));
y(:,1) = y0;
for i = 2:length(t)
y(:,i) = y(:,i-1) + h*F(t(i-1),y(:,i-1));
end
sol = [ t; y ];
end

 채택된 답변

Ameer Hamza
Ameer Hamza 2018년 5월 6일
편집: Ameer Hamza 2018년 5월 6일

0 개 추천

The first argument in anonymous function f should be time t. Reverse the order like this
f = @(t, y) [-2*y(1) + 5*exp(-t); (-y(1).*(y(2).^2))/2];

댓글 수: 4

Susan Santiago
Susan Santiago 2018년 5월 6일
Thank you so much! I've been stuck on this for hours. Is there any reason why t needs to be first?
Ameer Hamza
Ameer Hamza 2018년 5월 6일
ode45 documentation mention that it consider that input should be of the form f(t, y).
Susan Santiago
Susan Santiago 2018년 5월 6일
thank you!
Ameer Hamza
Ameer Hamza 2018년 5월 6일
you are welcome.

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

추가 답변 (0개)

카테고리

질문:

2018년 5월 6일

댓글:

2018년 5월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by