How does this Euler's Method for first ODE work?

2 views (last 30 days)
1 - function [x,y] = Euler(h, x0, y0, interval_length, func)
2 - nsteps = floor(interval_length/h) + 1;
3 - x = zeros(nsteps,1);
4 - y = zeros(nsteps,1);
5- x(1) = x0;
6- y(1) = y0;
7 - for i=2:nsteps
8 - y(i) = y(i-1) + h* func(x(i-1), y(i-1));
9 - x(i) = x(i-1) + h;
10 - end
Please explain how this program works line by line I am trying to understand it so I can learn how to use it properly.

Answers (1)

darova
darova on 3 Oct 2019
Here are some basic concepts
1 - function [x,y] = Euler(h, x0, y0, interval_length, func)
% h - step in X direction (dx)
% x0 - initial position (start value)
% y0 - initial position (start value)
% interval_length - how far object will move from x0
% func - derivate function of y (dy/dx == y' == f(x,y))
2 - nsteps = floor(interval_length/h) + 1; % number of points (steps)
3 - x = zeros(nsteps,1); % preallocation (memory reserving for array)
4 - y = zeros(nsteps,1); % preallocation (memory reserving for array)
5- x(1) = x0; % initialization of array
6- y(1) = y0; % initialization of array
7 - for i=2:nsteps % for loop. Everything between FOR .. LOOP is repeated
8 - y(i) = y(i-1) + h* func(x(i-1), y(i-1)); % increase Y position
9 - x(i) = x(i-1) + h; % increase X position
10 - end
Google more about Euler Method

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!