Trouble with Euler's method in MATLAB

In summary, the code you provided uses Euler's method to solve the IVP dy/dt = y, y(1859) = 12. The code was modified to set the initial conditions and the time step. The right-hand side of the equation was defined as dy/dt = y. The range of values for t was set to 1859 to 1864.
  • #1
Malby
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.
 
Physics news on Phys.org
  • #2
The code you provided is an example of solving an IVP using Euler's method. To solve the problem you have given, you need to modify it accordingly. First, you need to set the initial conditions. In your example, the initial condition is y(1) = 12. Second, you need to set the time step. In your example, the time step is 0.1 years. Third, you need to define the right-hand side of the equation. In your example, the right-hand side is dy/dt = y.Finally, you need to set the range of values for t. In your example, this is from 1859 to 1864.Using these parameters, you can write the following code:clear allt=1859:0.1:1864;y(1)=12;for i = 1:50 y(i+1)=y(i)+y(i)*0.1;endy(50)plot(t,y,'-')
 

1. What is Euler's method in MATLAB?

Euler's method in MATLAB is a numerical method used to approximate the solution of a differential equation. It is based on the idea of dividing the interval of the independent variable into smaller subintervals and calculating the values of the dependent variable at each subinterval.

2. What is the purpose of using Euler's method in MATLAB?

The purpose of using Euler's method in MATLAB is to approximate the solution of a differential equation when an exact solution cannot be found. It is a useful tool in many fields of science and engineering, such as physics, chemistry, and biology.

3. What are some common issues or errors encountered when using Euler's method in MATLAB?

Some common issues or errors encountered when using Euler's method in MATLAB include: round-off errors due to the use of finite precision arithmetic, instability when the step size is too large, and the accumulation of errors over many iterations.

4. How can these issues be addressed when using Euler's method in MATLAB?

To address round-off errors, it is important to use a smaller step size and to check the accuracy of the results by comparing them with known solutions. Instability can be avoided by choosing an appropriate step size and by using more accurate methods, such as the Runge-Kutta method. To reduce the accumulation of errors, it is helpful to use a smaller step size and to limit the number of iterations.

5. Are there any alternatives to using Euler's method in MATLAB?

Yes, there are alternatives to using Euler's method in MATLAB, such as the Runge-Kutta method, the Adams-Bashforth method, and the Adams-Moulton method. These methods are more accurate and can handle a wider range of differential equations, but they may be more computationally intensive.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
987
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
248
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
Back
Top