1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Euler's Method In Matlab

  1. Feb 2, 2014 #1
    1. The problem statement, all variables and given/known data
    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.

    2. Relevant equations

    3. The attempt at a solution

    Here is my code
    Code (Text):
    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);


    hold on

    fa= a*exp(a*t)


    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?

    Attached Files:

    • !.png
      File size:
      1.2 KB
  2. jcsd
  3. Feb 2, 2014 #2


    User Avatar
    Homework Helper
    Gold Member

    Have you tried a smaller time step?
  4. Feb 2, 2014 #3
    Yes, lewando. I actually just tried it, and it made the approximation closer to the analytic solution. Thank you for the suggestion.
  5. Feb 2, 2014 #4


    User Avatar
    Homework Helper
    Gold Member

    You should have a chat with that professor then... :wink:
  6. Feb 2, 2014 #5


    User Avatar
    Science Advisor
    Homework Helper

    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted