Message Boards Message Boards

3 Replies
4 Total Likes
View groups...
Share this post:

Finding the Maximum Value of an Interpolating Function

Howdy!  I'm new to the forums, but I'm having some trouble with a script I'm writing, so I thought I'd see if the community can help.  I'm solving a set of second order differential equations using NDSolve and I'm trying to extract the maximum values of a specific one of the interpolating functions produced, but I keep getting an error message saying something along the lines of "the function value {0.132074} is not a real number at {t} = {2.}".  I know that the function exists at that point because it is continuous and I can put t=2 in to get that value out.

Any ideas on how to get the value to resolve?  Any help is appreciated!

Code below:
 \[Omega] = 1;
 k = 100000;
 M = 10000;
 F = {x1''[t] == -k/M (2*x1[t] - x2[t]) - k/M Cos[\[Omega] t],
    x2''[t] == -k/M (2*x2[t] - x1[t] - x3[t]),
    x3''[t] == -k/M (2*x3[t] - x2[t] - x4[t]),
    x4''[t] == -k/M (2*x4[t] - x3[t] - x5[t]),
    x5''[t] == -k/M (2*x5[t] - x4[t] - x6[t]),
    x6''[t] == -k/M (2*x6[t] - x5[t] - x7[t]),
   x7''[t] == -k/M (x7[t] - x6[t]),
   x1[0] == x2[0] == x3[0] == x4[0] == x5[0] == x6[0] == x7[0] == 0,
   x1'[0] == x2'[0] == x3'[0] == x4'[0] == x5'[0] == x6'[0] ==
    x7'[0] == 0};
sol = NDSolve[F, {x1, x2, x3, x4, x5, x6, x7}, {t, 1, 200}];
FindMaximum[{x2[t] /. sol, 1 <= t <= 100}, {t}]
POSTED BY: Adam Broussard
3 Replies
Replace your NDSolve with this
sol = First@NDSolve[F, {x1, x2, x3, x4, x5, x6, x7}, {t, 1, 200}]
now FindMaximum is happy
FindMaximum[{x2[t] /. sol, 1 <= t <= 100}, {t}]
(*  {0.412503902064569, {t -> 9.66977049029881}} *)
POSTED BY: Nasser M. Abbasi
Thanks for the help!  What is the reason for this though?  Is it because NDSolve spits out {InterpolatingFunction} rather than InterpolatingFunction without brackets that you have to specify First?

Edit: On further examination, I have determined that this is the case.  Thanks again for the help!
POSTED BY: Adam Broussard
What is the reason for this though?
Well, I simply saw the error message itself saying
FindMaximum::nrnum: "The function value {0.188062918964778} is not a real number at {t} = {2.`}."
And saw the extra {} around the number above. Since the number itself was clearly a real number, then it was the {} that was confusing it. It saw something with List Header when it was expecting a number.

The only place this {} could come from is from the solution itself, which has the extra {} around it.
{{x1 -> InterpolationFunction)}}
Using First@NDSolve removed this extra {}.

One might say that may be FindMaximum should have sorted this extra {} out itself internally and Falttened it or did FIrst@ itself and such, but I am sure there are good reason why it did not do that, as that can break other things.
POSTED BY: Nasser M. Abbasi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract