필터 지우기
필터 지우기

Matlab code help on Euler's Method

조회 수: 652 (최근 30일)
Sanjida Ahmed
Sanjida Ahmed 2016년 4월 11일
댓글: Ahmed J. Abougarair 2024년 3월 20일
I have to implement for academic purpose a Matlab code on Euler's method(y(i+1) = y(i) + h * f(x(i),y(i))) which has a condition for stopping iteration will be based on given number of x. I am new in Matlab but I have to submit the code so soon. I am facing lots of error in implementing that though I haven't so many knowledge on Matlab. If anyone provide me so easy and simple code on that then it'll be very helpful for me. Thank you.
  댓글 수: 1
Muhammad Tahir
Muhammad Tahir 2023년 12월 24일
이동: Dyuman Joshi 2023년 12월 26일
y'=2x-3y+1, y(1)=5, y(1.2)=? MATLAB code using euler'method to obtain a four decimal and h= 0.1

댓글을 달려면 로그인하십시오.

채택된 답변

James Tursa
James Tursa 2016년 4월 11일
Here is a general outline for Euler's Method:
% Euler's Method
% Initial conditions and setup
h = (enter your step size here); % step size
x = (enter the starting value of x here):h:(enter the ending value of x here); % the range of x
y = zeros(size(x)); % allocate the result y
y(1) = (enter the starting value of y here); % the initial y value
n = numel(y); % the number of y values
% The loop to solve the DE
for i=1:n-1
f = the expression for y' in your DE
y(i+1) = y(i) + h * f;
end
It is based on this link, which you have already read:
You need to fill in the values indicated, and also write the code for the f line. What is the DE you are trying to solve?
  댓글 수: 4
ATUL
ATUL 2023년 3월 10일
how many iterations, we will decide in this?
Ahmed J. Abougarair
Ahmed J. Abougarair 2024년 3월 20일
% Euler's Method
% Initial conditions and setup
clc
clear
h = input('Enter your step size here :'); % step size
x = input('Enter the starting value of x :');
xend = input('Enter the ending value of xend :'); % the range of x
n = (xend-x)/h; % the number of y values
y = zeros(1,n); % allocate the result y
y(1) = input('Enter the starting value of y :'); % the initial y value
% The loop to solve the DE
for i=1:n
f(i) = 6- 2*(y(i)/x(i)); % dy/dx = 6-2y/x
y(i+1) = y(i) + h * f(i);
x(i+1)=x(i)+h;
end
[x' y']

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

mahmoud mohamed abd el kader
mahmoud mohamed abd el kader 2020년 10월 27일
h=0.5;
x=0:h:4;
y=zeros(size(x));
y(1)=1;
n=numel(y);
for i = 1:n-1
dydx= -2*x(i).^3 +12*x(i).^2 -20*x(i)+8.5 ;
y(i+1) = y(i)+dydx*h ;
fprintf('="Y"\n\t %0.01f',y(i));
end
%%fprintf('="Y"\n\t %0.01f',y);
plot(x,y);
grid on;
  댓글 수: 4
James Tursa
James Tursa 2021년 3월 3일
편집: James Tursa 2021년 3월 3일
@shireesha myadari Please delete this comment and open up a new question for this.
Ahmed J. Abougarair
Ahmed J. Abougarair 2024년 3월 20일
% Euler's Method
% Initial conditions and setup
clc
clear
h = input('Enter your step size here :'); % step size
x = input('Enter the starting value of x :');
xend = input('Enter the ending value of xend :'); % the range of x
n = (xend-x)/h; % the number of y values
y = zeros(1,n); % allocate the result y
y(1) = input('Enter the starting value of y :'); % the initial y value
% The loop to solve the DE
for i=1:n
f(i) = 6- 2*(y(i)/x(i)); % dy/dx = 6-2y/x
y(i+1) = y(i) + h * f(i);
x(i+1)=x(i)+h;
end
[x' y']

댓글을 달려면 로그인하십시오.


Bakary Badjie
Bakary Badjie 2021년 6월 14일
what is the Matlab function that implements Euler’s method
  댓글 수: 1
Chris Horne
Chris Horne 2022년 3월 31일
Is the term 'forward Euler' the same as 'Euler' in terms of the algorithm? Here is my method for solving 3 equaitons as a vector:
% This code solves u'(t) = F(t,u(t)) where u(t)= t, cos(t), sin(t)
% using the FORWARD EULER METHOD
% Initial conditions and setup
neqn = 3; % set a number of equations variable
h=input('Enter the step size: ') % step size will effect solution size
t=(0:h:4).';%(starting time value 0):h step size
nt = size(t,1); % size of time array
%(the ending value of t ); % the range of t
% define the function vector, F
F = @(t,u)[t,cos(t),sin(t)]; % define the function 'handle', F
% with hard coded vector functions of time
u = zeros(nt,neqn); % initialize the u vector with zeros
v=input('Enter the intial vector values of 3 components using brackets [u1(0),u2(0),u3(0)]: ')
u(1,:)= v; % the initial u value and the first column
%n=numel(u); % the number of u values
% The loop to solve the ODE (Forward Euler Algorithm)
for i = 1:nt-1
u(i+1,:) = u(i,:) + h*F(t(i),u(i,:)); % Euler's formula for a vector function F
end

댓글을 달려면 로그인하십시오.


Rakshana
Rakshana 2022년 11월 13일
h=0.5; x=0:h:4; y=zeros(size(x)); y(1)=1; n=numel(y); for i = 1:n-1 dydx= -2*x(i).^3 +12*x(i).^2 -20*x(i)+8.5 ; y(i+1) = y(i)+dydx*h ; fprintf('="Y"\n\t %0.01f',y(i)); end %%fprintf('="Y"\n\t %0.01f',y); plot(x,y); grid on;

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by