Mathematica - Find max value NDSolve Plot

In summary, the forum members discuss ways to determine the single maximum value in a plotted range after numerically solving a differential equation and plotting the results. One member suggests using the NDSolve function and plotting the results using the numerical solution, while another member suggests finding the maximum value by turning off warnings and using the FindMaximum function.
  • #1
carey1000
2
0
Dear forum members:

After numerically solving a differential equation and plotting the results I would like to determine the single maximum value in the plotted range but do not know how.

The code below works for numerically solving the differential equation and plotting the results.

NDSolve[{x''[t] + x[t] - 0.167 x[t]^3 == 0.005 Cos[t + -0.0000977162*t^2/2], x[0] == 0, x'[0] == 0}, x, {t, 0, 1000}]

Plot[Evaluate[x[t] /. s], {t, 0, 1000},
Frame -> {True, True, False, False}, FrameLabel -> {"t", "x"}, FrameStyle -> Directive[FontSize -> 15], Axes -> False]

Thank you!
Carey
 
Physics news on Phys.org
  • #2
I can't find an easy way to do it, with mapping or anything, but deconstructing the interpolating function can get it:

Max[s[[1]][[1]][[2]][[4]][[3]]]

This is basically the y-values of the array. Just found it by looking at what creates an interpolating function. I'm not sure if its always at this location, but I don't see why it wouldn't be.
 
  • #3
Tabulate the data and use Max@
 
  • #4
Maxdata = Evaluate[x[1]/.s];

Then, find a way to loop t = 2 thru 1000:
(Newmax = If[Evaluate[x[t]/.s]>Maxdata, Evaluate[x[t]/.s, Maxdata];
Maxdata = Newmax;)
 
  • #5
carey1000 said:
Dear forum members:

..

Plot[Evaluate[x[t] /. s], {t, 0, 1000},
Frame -> {True, True, False, False}, FrameLabel -> {"t", "x"}, FrameStyle -> Directive[FontSize -> 15], Axes -> False]

Carey

To plot the results, why you need to divide by " .s "??
 
  • #6
panzer7910 said:

To plot the results, why you need to divide by " .s "??

In the help manual of Mathematica, it gives the example:
Code:
s = NDSolve[{y'[x] == y[x] Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30}]
Plot[Evaluate[y[x] /. s], {x, 0, 30}, PlotRange -> All]

Also you could find the symbol /. means ReplaceAll (/.) in its help manual.
Code:
In[1]:= {x, x^2, y, z} /. x -> a

Out[1]= {a, a^2, y, z}

In[2]:= {x, x^2, y, z} /. x -> {a, b}

Out[2]= {{a, b}, {a^2, b^2}, y, z}

In[3]:= Sin[x] /. Sin -> Cos

Out[3]= Cos[x]
Combine these two, you could find that s represents the result of this numerical solution, and
Code:
Evaluate[y[x] /. s]
means using the numerical results s to represent the value of y[x] as y[x] itself does not have a definition in the ordinary form so the plot function cannot plot it unless you use something(here is the numerical results s) to represent it
 
Last edited:
  • #7
Will this do?

In[1]:= f = x/.NDSolve[{x''[t]+x[t]-0.167x[t]^3==0.005 Cos[t+ -0.0000977162*t^2/2], x[0]==0, x'[0]==0}, x, {t,0,1000}][[1,1]];
Plot[f[x], {x, 0, 1000}]

Out[2]= ...PlotSnipped...

In[3]:= {*Looks like max is between 775 and 825, turn off warnings and hunt for it*)
Off[FindMaximum::eit];
Sort[Table[FindMaximum[{f[x], 0<x<1000}, {x,y,y-1,y+1}], {y,775,825}], OrderedQ[{First[#1], First[#2]}] &]

Out[4]= {
{-0.699441, {x -> 796.}}, {-0.695645, {x -> 803.}}, {-0.688966, {x -> 789.}},
{-0.681066, {x -> 810.}}, {-0.661475, {x -> 782.}}, {-0.66001, {x -> 817.}},
{-0.637412, {x -> 824.}}, {-0.614744, {x -> 775.}}, {0.094972, {x -> 820.}},
{0.108052, {x -> 778.}}, {0.121419, {x -> 813.}}, {0.144966, {x -> 785.}},
{0.145051, {x -> 806.}}, {0.16006, {x -> 799.}}, {0.161367, {x -> 792.}},
{0.412197, {x -> 795.}}, {0.414114, {x -> 788.}}, {0.423985, {x -> 802.}},
{0.432996, {x -> 781.}}, {0.444894, {x -> 809.}}, {0.469508, {x -> 816.}},
{0.470473, {x -> 774.}}, {0.492212, {x -> 823.}}, {1.02978, {x -> 779.}},
{1.05629, {x -> 821.}}, {1.05931, {x -> 786.}}, {1.0706, {x -> 814.}},
{1.07659, {x -> 793.}}, {1.08044, {x -> 807.}}, {1.08311, {x -> 800.}},
{1.16416, {x -> 780.}}, {1.17012, {x -> 787.}}, {1.17933, {x -> 794.}},
{1.19063, {x -> 801.}}, {1.20206, {x -> 808.}}, {1.21145, {x -> 815.}},
{1.21692, {x -> 822.}}, {1.22094, {x -> 779.646}}, {1.22094, {x -> 779.647}},
{1.23615, {x -> 786.619}}, {1.23615, {x -> 786.621}}, {1.24892, {x -> 793.61}},
{1.24892, {x -> 793.612}}, {1.25869, {x -> 800.615}}, {1.25869, {x -> 800.617}},
{1.26499, {x -> 807.63}}, {1.26499, {x -> 807.632}}, {1.26624, {x -> 821.672}},
{1.26624, {x -> 821.673}}, {1.26753, {x -> 814.65}}, {1.26753, {x -> 814.652}}}
 

Related to Mathematica - Find max value NDSolve Plot

What is Mathematica?

Mathematica is a software program used for mathematical and scientific computing. It allows users to perform various mathematical operations, create visualizations, and solve complex equations.

How do I find the maximum value using NDSolve Plot in Mathematica?

To find the maximum value using NDSolve Plot in Mathematica, you can use the Maximize function. First, define your equation using NDSolve, then use Maximize on the output of NDSolve to find the maximum value. For example, Maximize[NDSolve[{x'[t] == 1 - y[t], y'[t] == x[t], x[0] == 1, y[0] == 1}, {x, y}, {t, 0, 10}], x] will give you the maximum value of x within the specified time range.

What is NDSolve in Mathematica?

NDSolve is a function in Mathematica that is used to solve differential equations numerically. It takes in an equation, initial conditions, and the range of independent variables to solve the equation and provide a numerical solution.

Can I plot the output of NDSolve in Mathematica?

Yes, you can plot the output of NDSolve in Mathematica using the Plot function. For example, Plot[NDSolve[{x'[t] == 1 - y[t], y'[t] == x[t], x[0] == 1, y[0] == 1}, {x, y}, {t, 0, 10}], {t, 0, 10}] will plot the solution of the differential equation over the specified time range.

What is the purpose of using FindMaximum in Mathematica?

The FindMaximum function in Mathematica is used to find the maximum value of a function numerically. It takes in a function and a range of values and returns the maximum value of the function within that range. This can be useful when trying to optimize a function or find the maximum value of a dataset.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
403
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
310
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
375
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
178
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
979
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top