Comparing Analytic and Approximate Solutions Using Euler's Method in Matlab

In summary: This will help you see more clearly what is happening. In summary, the Euler method gives an approximate solution to the differential equation when b=0, but the analytic solution will always coincide with the approximation.
  • #1
Bashyboy
1,421
5

Homework Statement


Hello, I am working on a problem involves my using the Euler Method to approximate the differential equation [itex]\displaystyle \frac{df}{dt} = af(t) - b[f(t)]^2[/itex], both when b=0 and when b is not zero; and I am to compare the analytic solution to the approximate solution when b=0.


Homework Equations





The Attempt at a Solution



Here is my code
Code:
f(1) = 1000;
t(1)= 0;
a = 10;
b = 0 ;
dt = 0.01;
Nsteps = 10/dt;

for i = 2:Nsteps
    t(i) = dt + t(i-1);
    %f(i) = f(i-1)*(1 + dt*(a - b*f(i-1)));
    f(i) = f(i-1)*(1 + a*dt); 
end

plot(t,f,'r-')

hold on

fa= a*exp(a*t)

plot(t,fa,'bo')

When b=0, the solution to the differential equation is [itex]f(t) = c e^{at}[/itex]. When I apply the initial condition, that f(0) = 1000, then the differential equation becomes [itex]f(t) = 1000 e^{at}[/itex]. Now, my professor said that a differential equation has an analytic solution, no matter what time step you use, the graph of analytic solution and the approximation (Euler's Method) will coincide. So, I expected the two graphs to overlap. I attached a picture of what I got.

Why did this occur? In order to get the graphs to overlap, I changed 1000 to 10, which is a, just for the heck of it. When I did this, the two overlapped. I don't understand. What am I doing incorrectly?
 

Attachments

  • !.png
    !.png
    1.3 KB · Views: 450
Physics news on Phys.org
  • #2
Have you tried a smaller time step?
 
  • #3
Yes, lewando. I actually just tried it, and it made the approximation closer to the analytic solution. Thank you for the suggestion.
 
  • #4
You should have a chat with that professor then... :wink:
 
  • #5
Bashyboy said:
Now, my professor said that a differential equation has an analytic solution, no matter what time step you use, the graph of analytic solution and the approximation (Euler's Method) will coincide.

I think you misunderstood what your professor said. That quote is so wrong that it's very hard to believe it is what your prof actually meant.

If you change the time step in the Euler method you are using, you will definitely get different results. As somebody else said, try a smaller step size.

Also, it might be better to plot the y-axis of your graphs on a log scale.
 

Related to Comparing Analytic and Approximate Solutions Using Euler's Method in Matlab

1. What is Euler's Method and how is it used?

Euler's Method is a numerical method used to approximate the solution to a differential equation. It works by breaking the problem into smaller steps and using the slope of the tangent line at each step to estimate the next point on the graph.

2. How do you implement Euler's Method in Matlab?

To implement Euler's Method in Matlab, you first need to define the differential equation and initial conditions. Then, using a for loop, you can calculate the slope at each step and update the solution using the previous point. Finally, plot the points to visualize the solution.

3. What are the advantages of using Euler's Method in Matlab?

One advantage of using Euler's Method in Matlab is that it is relatively simple to implement and understand. It also allows for quick and efficient estimation of the solution to a differential equation. Additionally, Matlab has built-in functions and tools that make it easier to visualize and analyze the results.

4. Are there any limitations to using Euler's Method in Matlab?

Yes, there are some limitations to using Euler's Method in Matlab. One limitation is that it can only approximate the solution, so the accuracy of the results may be affected. It also may not work well for more complex differential equations or for problems with a lot of discontinuities or sharp turns.

5. How can I improve the accuracy of Euler's Method in Matlab?

One way to improve the accuracy of Euler's Method in Matlab is to use a smaller step size. This means breaking the problem into more smaller steps, which can result in a more accurate estimation of the solution. Additionally, using a more advanced numerical method, such as the Runge-Kutta method, may also improve the accuracy.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
963
  • Engineering and Comp Sci Homework Help
Replies
1
Views
919
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
982
Replies
7
Views
2K
  • Differential Equations
Replies
1
Views
813
  • Engineering and Comp Sci Homework Help
Replies
3
Views
834
Back
Top