Compute delta t with MATLAB. Since, by using Euler's method, dP/dt can be written as (Pn + 1 -- Pn)/delta t, first decide on this value to obtain your approximation. All you need to do is define the solving time, as well as the number of time intervals, and divide them to obtain delta t. An example is as follows:
t = 5;
n = 50;
deltat = t/n;
Set a value for all the parameters of the equation. If, for example, you would like to implement Pn + 1 = Pn + x * delta t * Pn, you would only need to define x in MATLAB, since delta t was already defined in the previous step:
r = .01;
Create an array that you will be using to compute your solutions. Fill it with 0 and set a starting value as follows:
P = zeros (1, n + 1);
P(1) = 2;
Use a "for" loop to obtain the Euler's solution recurrently. Make sure you also use the "end" command to avoid compile errors.
for i = 1:n
P(i+1) = P(i) * (1 + x * deltat);
end
Plot the solution in order to visualize it. This step is optional, but you will have a clearer view of your approximation if you do it. You can use the "plot" function as follows:
plot (linspace(0,t,n+1), p)