Solve IVP with modified Euler's method
14 次查看(过去 30 天)
显示 更早的评论
I am trying to solve the initial value problem x'(t) = t/(1+x^2) with x(0) = 0 and 0 <= t <= 5 using modified Euler's method with 10 steps however I am not too sure about my code can anyone double check/provide a more efficient code? thanks in advance
function [T,Y] = euler_modified(f,a,b,ya,m)
h = (b - a)/m;
T = zeros(1,m+1);
Y = zeros(1,m+1);
T(1) = a;
Y(1) = ya;
for j=1:m,
Y(j+1) = Y(j) + h*feval(f,T(j) + h/2,Y(j) + h*feval(f,T(j),Y(j)));
T(j+1) = a + h*j;
end
1 个评论
John D'Errico
2017-11-13
Why do you care if the code is not as efficient as you wish? This is homework, as otherwise, you would not want to use Euler's method in any form. If not homework, then there are batter methods to solve an ODE, and they are already written. NEVER write code when professionally written code is given to you as part of the language itself.
回答(2 个)
ali alnashri
2021-4-14
function [T,Y] = euler_modified(f,a,b,ya,m)
h = (b - a)/m;
T = zeros(1,m+1);
Y = zeros(1,m+1);
T(1) = a;
Y(1) = ya;
for j=1:m,
Y(j+1) = Y(j) + h*feval(f,T(j) + h/2,Y(j) + h*feval(f,T(j),Y(j)));
T(j+1) = a + h*j;
end
0 个评论
My Anh Vu
2023-4-1
Y(j+1) = Y(j) + h*feval(f,T(j) + h/2,Y(j) + h*feval(f,T(j),Y(j)));
should be Y(j+1) = Y(j) + h*feval(f,T(j) + h/2,Y(j) + h/2*feval(f,T(j),Y(j)));
Good luck!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!