# Matlab code help on Euler's Method

5,913 views (last 30 days)

Show older comments

Sanjida Ahmed
on 11 Apr 2016

##### 0 Comments

### Accepted Answer

James Tursa
on 11 Apr 2016

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?

##### 2 Comments

James Tursa
on 13 Apr 2016

Edited: James Tursa
on 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

### More Answers (4)

mahmoud mohamed abd el kader
on 27 Oct 2020

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;

##### 3 Comments

James Tursa
on 3 Mar 2021

Edited: James Tursa
on 3 Mar 2021

@shireesha myadari Please delete this comment and open up a new question for this.

Bakary Badjie
on 14 Jun 2021

what is the Matlab function that implements Euler’s method

##### 2 Comments

Israel Morris
on 1 Aug 2022

Rakshana
on 13 Nov 2022 at 1:54

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!