need plot a graph using Euler's method for both numerical and analytical

% Euler's Method
% Initial conditions and setup
h = (10); % step size
x = (0):h:(60); % the range of x
y = zeros(size(x)); % allocate the result y
y(1) = (2); % the initial y value
n = numel(y); % the number of y values
for i=1:n-1
pi = 3.1416
a = 2
r = 1
f = a + (2 - 0.4*a) * x / r^2 * pi
y(i+1) = y(i) + h * f;
end
plot (x,y,'b')
grid on

4 Comments

Why do you think you need to define a value for pi? And worse, the value you chose is not even very accurate? Of course you are using Euler's method, with an extremely coarse step size. So 5 digits fo pi is probably adequate. But learn tol leave pi alone. It already exists. So there is no need to define it as a variable.
format long g
pi
ans =
3.14159265358979
And that is the correct value out to that many digits.
But you seem to be plotting the numrical solution. So what is your problem? What is the differential equation? Can you integrate that analytically? Surely, since f is LINEAR in x, you know the integral, since the integral of x is x^2/2? Or you could use dsolve in the symbolic toolbox?
Do you need to know how to add a second plot to the existing figure?
help hold
HOLD Hold current graph HOLD ON holds the current plot and all axis properties, including the current color and linestyle, so that subsequent graphing commands add to the existing graph without resetting the color and linestyle. HOLD OFF returns to the default mode whereby PLOT commands erase the previous plots and reset all axis properties before drawing new plots. HOLD, by itself, toggles the hold state. HOLD does not affect axis autoranging properties. HOLD ALL is the same as HOLD ON. This syntax will be removed in a future release. Use HOLD ON instead. HOLD(AX,...) applies the command to the Axes object AX. Algorithm note: HOLD ON sets the NextPlot property of the current figure and axes to "add". HOLD OFF sets the NextPlot property of the current axes to "replace". See also ISHOLD, NEWPLOT, FIGURE, AXES. Documentation for hold doc hold
Avoid to define constants inside a loop. Although runtime does not matter here, it is worth to start programming with a clean and clear style.
Enclosing constants in parentheses is clutter. 0:h:60 is fine.
You have mentioned your goal and posted some code. To get a proper answer, ask a specific question. What is the problem?
Thank you for the help. tht ting is when I try to plot the graph it gives out error message like : unable to perform assignment because the left and right sides have a different number of element, so I was asking what should I do or change and how
@Mpho Mokgotlo: If you mention an error, post a copy of the complete error message. Seeing some parts of the message only is less useful, e.g. the readers do not know, in which line the error occurs.

Sign in to comment.

Answers (2)

f = a + (2 - 0.4*a) * x(i) / r^2 * pi; % x(i) not just x.
% Also take note of the other comments above.
h = 10; % step size
x = 0:h:60; % the range of x
y = zeros(size(x)); % allocate the result y
y(1) = 2; % the initial y value
n = numel(y); % the number of y values
a = 2;
r = 1;
for i = 1:n-1
f = a + (2 - 0.4 * a) * x(i) / r^2 * pi;
% ^^^
y(i+1) = y(i) + h * f; % I guess this line fails
end
plot (x,y,'b')
grid on
A cleaned version of your code. Alan Stevens' hint is included (please accept his answer).
You can solve such problems using the debugger. Type in the command window:
dbstop if error
and run the code again. Matlab stops at the problem and you can check the values of the used variables.
size(y(i+1)) % A scalar, of course
size(y(i) + h * f) % Not a scalar
size(f) % Same size as x!
You do not want x in the line to get the value of f, but only the element x(i).
Do you really mean: ... / r^2 * pi or .../ (r^2 * pi) ?

Categories

Find more on Programming in Help Center and File Exchange

Products

Release

R2022a

Asked:

on 29 Oct 2022

Edited:

Jan
on 30 Oct 2022

Community Treasure Hunt

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

Start Hunting!