# Matlab Plotting Euler Iteration Against Exact Solution how to Set Initial Condition

#### Beetle37

Hi

I have the following code to plot an approximation using Euler's iteration against the exact value of the function.

I have used x and y for time and population when coding the Euler iterations and t and m for time and population when plotting the exact function. I can't work out how to initialise the m value when plotting the exact function. M(0)=13. When executed the plot looks correct except for that.

"%Compute and Graph Euler's Iteration of dM/dt=-1/40M(M-30) (using x and y)
%Graph Exact Solution of m=30./(1+((17/3)*exp(-3.*t/4)))
clear;
format short;
x=0; %Initialise x value
y=13; %Initialise y value
dx=1; %Step size
n=20; %Number of values to be calculated
for i=1:n
y(i+1)=y(i)+dx.*(-1/40*y(i).*(y(i)-30));
x(i+1)=x(i)+dx;
end
disp(' x y')
disp([x' y'])
t=0:1:20; %Set range of t being alternate units for time in years
m=30./(1+((17/3)*exp(-3.*t/4))); %Population from Exact Function
plot(x,y,t,m)
xlabel('Time in Years');
ylabel('Population')
title('Graph Showing Euler Approximation and Exact Value of Function')"

Wondering if anyone is able to help.

Kind regards
Beetle

#### Rebesques

Re: Matlab Plotting Euler Iteration Against Exact Solution how to Set Initial Conditi

Recode with m(1)=13, as matlab cannot accept zero as an index.