Code help on Euler's method

84 次查看(过去 30 天)
I would like to implement a Matlab code based on Euler's method. This is a project work in the university, and I have a sample solution from my professor to make this project easier. I have succesfully modified this sample solution to fit my task.
Differential equation:
My code:
fv = @(t,y) t * sin(-y) + cos(t)./(t+1);
t0 = 0; tv = 7; y0 = 2; steps = 10;
[tE,yE] = Euler(fv,[t0,tv],y0,steps);
plot(tE,yE,'*;Euler;');
function [t,y] = Euler(fv,tr,y0,steps)
t = linspace(tr(1),tr(2),steps + 1);
y = zeros(size(t));
h = t(2)-t(1);
y(1) = y0;
for i = 1:steps
y(i+1) = y(i) + h * fv(t(i), y(i));
end
end
When I attemp to run my code I always get an error message:
Unrecognized function or variable 'Euler'.
My Matlab skills are horrible, so I can't figure out what is the problem. :(
If anyone provide me an explanation or a proper code, it'll be very helpful for me. Thank you in advance.
  2 个评论
DGM
DGM 2021-4-14
编辑:DGM 2021-4-14
Is this all in one file, or is Euler() in a separate file? If it's the first case, what version are you using? If it's the second case, is Euler() on the path? Running this in R2019b (all in one file), the code runs for me (except the plot() call)
Unrelated, but I don't really know what you're trying to do here:
plot(tE,yE,'*;Euler;')
'*;Euler;' isn't a valid line spec. If you're trying to add a legend, you can do that a couple different ways, but this is one:
h=plot(tE,yE,'b'); % or pick whatever line/marker color you want
legend(h,'call it something')
Szalka Gergo
Szalka Gergo 2021-4-14
编辑:Szalka Gergo 2021-4-14
Thank you for your help. I use MATLAB R2020b (Linux). Euler() is not a separate file, it is located in my main code. I could not solve this issue. Maybe I will try it on another computer.
Meanwhile, I tried to write a new code:
fv = @(t,y) t * sin(-y) + cos(t)./(t+1); % diff. eq.
a = 0; % start point
b = 7; % end point
l = 100; % number of steps
y(1) = 2; % initial condition: y(1) = 2
h = (b-a)/l;
t = a:h:b;
y = zeros(size(t));
% Euler's method
for i = 1:l
y(i+1)= y(i)+ h * fv(y(i),t(i));
end
%ode45 -- it's just a benchmark
[t45,y45] = ode45(fv,[a,b],y(1));
plot(t,y,'b-')
hold on;
plot(t45,y45,'r-')
legend('Euler', 'ode45', 'Location','NorthWest')
hold off;
It's FINALLY working without error message, but I believe that it contain some bugs.
Figure:
It doesn't look too good. The Euler method can't approximate the original DE.

请先登录,再进行评论。

采纳的回答

John D'Errico
John D'Errico 2021-4-14
What you need to understand is that Euler is a poor method to use. Easy to write, easy to understand. But is it good? NO. And this is why you should not be writing code to solve numerical methods problems. You want to UNDERSTAND those codes, yes. But then for any real work, you need to use tools like ODE45, ODE15S, etc.
Regardless, look closely at the code you wrote.
fv = @(t,y) t * sin(-y) + cos(t)./(t+1); % diff. eq.
...
y(i+1)= y(i)+ h * fv(y(i),t(i));
Do you see a difference in how you call fv, compared to how you defined the function? You swapped y and t around.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by