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

2 次查看(过去 30 天)
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.

回答(1 个)

darova
darova 2019-10-3
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

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by