Finding the maximum value of x in a differential equation.

In summary, the conversation discusses using MATLAB to find the maximum value of x given a differential equation and initial conditions. The user is using Euler's method, but is having trouble with their code and getting imaginary numbers. They also discuss alternative methods, such as solving for the zeros of the equation, to find a more accurate solution. Ultimately, the conversation concludes with the user realizing their mistake and finding the correct solution.
  • #1
theBEAST
364
0

Homework Statement


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:
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?
 
Physics news on Phys.org
  • #2
Looks like a, b and N never change, so the only use you make of them in the code reduces to the constant 1.
 
  • #3
haruspex said:
Looks like a, b and N never change, so the only use you make of them in the code reduces to the constant 1.

Yeah so shouldn't the code output the value of x at t = (0,1,2,...,1000)?
 
  • #4
theBEAST said:
Yeah so shouldn't the code output the value of x at t = (0,1,2,...,1000)?
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?
 
  • #5
haruspex said:
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?

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...?
 
  • #6
theBEAST said:
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...
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.
 
  • #7
haruspex said:
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.

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.
 
  • #8
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.
 
  • #9
haruspex said:
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.

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!
 
  • #10
theBEAST said:
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!
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?
 
  • #11
haruspex said:
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?

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.
 

Related to Finding the maximum value of x in a differential equation.

1. What is a differential equation?

A differential equation is a mathematical equation that describes the relationship between the rate of change of a variable and the variables themselves. It is used to model a wide range of real-world phenomena in various fields such as physics, engineering, and economics.

2. How do you find the maximum value of x in a differential equation?

To find the maximum value of x in a differential equation, you need to first solve the equation to find the critical points. These are the points where the derivative of the function is equal to zero. Then, you can use the second derivative test to determine if the critical points are maximum points. Alternatively, you can graph the function and visually identify the maximum point.

3. What is the second derivative test?

The second derivative test is a mathematical method used to determine the nature of critical points in a function. If the second derivative at a critical point is positive, then the point is a minimum point. If the second derivative is negative, then the point is a maximum point. If the second derivative is zero, then the test is inconclusive.

4. Are there any other methods for finding the maximum value of x in a differential equation?

Yes, there are other methods for finding the maximum value of x in a differential equation. These include using optimization techniques such as the Lagrange multiplier method and the gradient descent method. These methods are often used in more complex differential equations with multiple variables.

5. Why is finding the maximum value of x in a differential equation important?

Finding the maximum value of x in a differential equation is important because it allows us to understand the behavior of the function and make predictions about its maximum value. This information is useful in many fields, such as economics and engineering, where it can help optimize processes and make informed decisions.

Similar threads

  • Calculus and Beyond Homework Help
Replies
7
Views
393
  • Calculus and Beyond Homework Help
Replies
5
Views
594
  • Calculus and Beyond Homework Help
Replies
1
Views
310
  • Calculus and Beyond Homework Help
Replies
18
Views
1K
  • Calculus and Beyond Homework Help
Replies
7
Views
600
  • Calculus and Beyond Homework Help
Replies
3
Views
1K
  • Calculus and Beyond Homework Help
Replies
1
Views
366
  • Calculus and Beyond Homework Help
Replies
2
Views
959
  • Calculus and Beyond Homework Help
Replies
5
Views
563
  • Calculus and Beyond Homework Help
Replies
2
Views
935
Back
Top