Trouble with Euler's method in MATLAB

  • MATLAB
  • Thread starter Malby
  • Start date
  • #1
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.
 

Answers and Replies

Related Threads on Trouble with Euler's method in MATLAB

  • Last Post
Replies
1
Views
6K
Replies
2
Views
3K
  • Last Post
Replies
1
Views
22K
  • Last Post
Replies
1
Views
2K
Replies
1
Views
1K
  • Last Post
Replies
8
Views
3K
  • Last Post
Replies
3
Views
3K
  • Last Post
Replies
2
Views
7K
  • Last Post
Replies
1
Views
3K
  • Last Post
Replies
4
Views
23K
Top