MATLAB Trouble with Euler's method in MATLAB

  • Thread starter Malby
  • Start date
16
0
I'm trying to solve an ODE using MATLAB and Euler's method but I've having some trouble understanding what's going on with the code. This is something relatively simple but I'm new to MATLAB so I'm not really sure what's going on.

The question:

Write a Matlab M-file that uses Euler’s method to numerically solve the IVP: dy/dt = y, y(1859) = 12. Solve the IVP up to the year 1864 using a step size of 0.1 years. This is modelling the exponential growth of rabbits in Australia for five years from the introduction of 12 rabbits at the start of 1859.

We are then given this code as an example:

t=0:0.5:5;
y(1)=1;
for i = 1:10
y(i+1)=y(i)+t(i)^2*0.5;
end
plot(t,y,’b:’)
hold on
yexact = t.^3/3+1;
plot(t,yexact,’r--’)
legend(’Numerical solution’, ’Analytical solution’);
title(’Example IVP solution’)
xlabel(’t’)
ylabel(’y’)

I used that code and then modified in in a way which I thought would give me what I need, for the first part anyway (without the labelling etc.):

clear all
t=1859:0.1:1864;
y(1)=12;
for i = 1:50
y(i+1)=y(i)+y(i)*0.1;
end
y(50)
plot(t,y,'-')

However this isn't giving me what I need.

Any suggestions would be greatly appreciated.
 

Want to reply to this thread?

"Trouble with Euler's method in MATLAB" You must log in or register to reply here.

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving
Top