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!

Finding the maximum value of x in a differential equation.

  1. Feb 10, 2013 #1
    1. The problem statement, all variables and given/known data
    I want to find the maximum value of x given that the differential equation relating x is:

    x' = 0.7548 - 599.49x^6 -2.4038x^2 - 0.12236x^1.5

    Where x = 0 at t = 0.

    I have to do this with MATLAB using Euler's Method and here is my code:

    Code (Text):
    x = 0
    a = 0 % initial t value
    b = 1000 % final t value
    N = 1000 % number of intervals

    for i = 1:N-1
        x(i+1) = x(i) + (b-a)/N * (0.7546-599.49*x(i)^6 - 2.4038*x(i)^2 - 0.12236*x(i)^1.5)
    end

    max(x)
    I get an imaginary number for the value... And most of the numbers in the vector x are NaN + NaNi.

    Does anyone know what I am doing wrong?
     
  2. jcsd
  3. Feb 10, 2013 #2

    haruspex

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member
    2016 Award

    Looks like a, b and N never change, so the only use you make of them in the code reduces to the constant 1.
     
  4. Feb 10, 2013 #3
    Yeah so shouldn't the code output the value of x at t = (0,1,2,....,1000)?
     
  5. Feb 10, 2013 #4

    haruspex

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member
    2016 Award

    If that's what you want, yes, but how did you pick that range? Your algorithm is shooting way beyond where you want to be and dives down to where x goes negative. Once it has done that the power of 1.5 will produce imaginary results.
    Can you think of a way of determining a smaller range that should include a solution?
     
  6. Feb 10, 2013 #5
    Alright, but before figuring out a smaller range... I am having problems with my code already.

    So if I use a = 0 and b = 1000 I get a vector x where x(2) = 0.7546. In other words x value at t = 1 gives x = 0.7546.

    NOW if I decide to use a = 0 and b = 1. Then I should expect that max(x) gives me some value greater than or equal to 0.7546. Instead it gives me 0.3074...

    From this I think there is something wrong with my code...?
     
  7. Feb 10, 2013 #6

    haruspex

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member
    2016 Award

    With b=1000, you are effectively considering t up to 1000. x[2] corresponds to t=2. With b = 1, you are only considering t up to 1. x[2] now corresponds to t=2/1000.
    Even if two setting of b encompass the maximum you're looking for, you would expect different values because of error accumulation, and it's not obvious which will give the greater value.
     
  8. Feb 10, 2013 #7
    Ah, so the one where b = 1 would give me a more accurate value but it may not be the interval where the max is located... I tried to figure out the interval by changing the values of b but as you said that would cause error to accumulate. Right now I am not sure what to do...

    As I increase b, it oscillates more and more.
     
  9. Feb 10, 2013 #8

    haruspex

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member
    2016 Award

    Well, I would not be using Euler's method to solve this problem. You want the point where x'=0, so that's the same as solving 0.7548 - 599.49x6 -2.4038x2 - 0.12236x1.5 = 0, or, as a polynomial in y = √x, 0.7548 - 599.49y12 -2.4038y4 - 0.12236y3 = 0.
    At the least, you could use that to get a handle on the range. Just find some smallish y, ylim, which makes the polynomial go negative. (It's going to be less than 1, right?) Set b-a = ylim2.
     
  10. Feb 10, 2013 #9
    LOL wow I can't believe I forgot about that. When the derivative is zero you get either a max or min. In this case it is a max because the second derivative at the solution I got x = 0.30748 is negative!

    And this corresponds to the value I get from Euler's!

    Thanks!
     
  11. Feb 10, 2013 #10

    haruspex

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member
    2016 Award

    The only reason I didn't mention this before is that you said in the OP that you had to use Euler's method. Was that not true?
     
  12. Feb 10, 2013 #11
    Oh, the question said I had to use MATLAB so the first thing that popped up in my mind was either euler's method or ode45. But then after what you said, you can also use MATLAB to solve for the zeros of the equation, which is a much better method.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook