How does this Euler's Method for first ODE work?
6 ビュー (過去 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.
0 件のコメント
回答 (1 件)
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Startup and Shutdown についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!