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 Kommentar

Muhammad Tahir
Muhammad Tahir am 24 Dez. 2023
Verschoben: Dyuman Joshi am 26 Dez. 2023
y'=2x-3y+1, y(1)=5, y(1.2)=? MATLAB code using euler'method to obtain a four decimal and h= 0.1

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

James Tursa
James Tursa am 11 Apr. 2016

12 Stimmen

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 Kommentare

Sanjida Ahmed
Sanjida Ahmed am 12 Apr. 2016
Bearbeitet: Sanjida Ahmed am 12 Apr. 2016
Thank you Tursa.I don't know what will teacher give me to solve but I am now practicing to solve f=x+2y equation.I type exact same code you provide and my code is,
% Euler's Method
% Initial conditions and setup
h = 0.01; % step size
x = 0:h:1; % the range of x
y = zeros(size(x)); % allocate the result y
y(1) = 1; % the initial y value
n = numel(y); % the number of y values
% The loop to solve the DE
for i=1:n-1
f = (x(i)+2*(y(i)));
y(i+1) = y(i) + h * f;
end
but it gives no answer in the editor.
James Tursa
James Tursa am 13 Apr. 2016
Bearbeitet: James Tursa am 13 Apr. 2016
After you enter this in the editor and save it, you need to run it either by typing the file name at the command prompt, or by pressing the green triangle Run button at the top of the editor. Since all of the lines end with a semi-colon ;, there will be no output to the screen when this runs. However, the variables are there. If you look in the Workspace list you will see them, or if you issue the whos command you also will see them. To see the result you could plot them. E.g.,
plot(x,y); grid on
ATUL
ATUL am 10 Mär. 2023
how many iterations, we will decide in this?
Ahmed J. Abougarair
Ahmed J. Abougarair am 20 Mär. 2024
% 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']

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

3 Stimmen

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 Kommentare

Tendai Kufandirori
Tendai Kufandirori am 1 Nov. 2020
Hi, I am trying to solve dy/dx = -2x^3 + 12x^2- 20x + 9 and am getting some errors when trying to use Euler's method. Do you know how to go about it please
John D'Errico
John D'Errico am 1 Nov. 2020
Was it necessary to post 3 identical answers, to an old question?
James Tursa
James Tursa am 3 Mär. 2021
Bearbeitet: James Tursa am 3 Mär. 2021
@shireesha myadari Please delete this comment and open up a new question for this.
Ahmed J. Abougarair
Ahmed J. Abougarair am 20 Mär. 2024
% 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']

Melden Sie sich an, um zu kommentieren.

Rakshana
Rakshana am 13 Nov. 2022

0 Stimmen

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;

Kategorien

Mehr zu Numerical Integration and Differential Equations finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by