I need a detailed explination of this code
1 view (last 30 days)
Show older comments
So I have this script but i have no clue what it does, and I got the text that says that it is Euler's method to find an approximate solution to the startvalue problem
y' = sin(sqrt(x+y)) with the start value y(0) = 0.4
on the intervall [0,20]. And the following code implements Euler's methode and plots the graph for the approximat solution of y.
But I was wondering what this script does.
So can someone please explain to me line by line what it does?
ymark = @(x,y) sin(sqrt(x+y));
t = 0:0.01:20;
y = zeros(1,length(t));
y(1) = 0.04;
dt = diff(t);
for s= 1:length(t)-1
y(s+1) = y(s)+dt(s)*ymark(t(s),y(s));
end
plot(t,y)
1 Comment
John D'Errico
on 24 Mar 2020
Edited: John D'Errico
on 24 Mar 2020
Do you have any clue what a differential equation is, what it means, etc.? Because Euler's method is the most basic numerical method you will ever see for solving an ordinary differetial equation.
This is what students are taught when they are introduced to the idea of solving an ODE numerically. In fact, the comments that you show, indicate exactly the differential equation that is approximately solved, including the initial value at t==0.
So are you asking someone to tutor you about differential equations from scratch? Taught well, this usually involves a semester or two.
Answers (1)
Ameer Hamza
on 24 Mar 2020
[Give a man a fish, he'll eat for a day; teach a man to fish and he'll eat for a lifetime]
Instead of explaining just this one piece of code, I can point you to documentation, which will help you to understand this code by yourself.
MATLAB commands:
Then about differential equations
Euler Method (ode1) https://www.mathworks.com/matlabcentral/answers/98293-is-there-a-fixed-step-ordinary-differential-equation-ode-solver-in-matlab-8-0-r2012b#answer_107643
New solvers (ode45): https://www.mathworks.com/help/matlab/ref/ode45.html
0 Comments
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!