Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #12854 > unrolled thread

Re: How to use FindMaximum with a parameter passed to NDSolve??

Started byBob Hanlon <hanlonr357@gmail.com>
First post2012-12-18 07:35 +0000
Last post2012-12-19 09:56 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.soft-sys.math.mathematica

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: How to use FindMaximum with a parameter passed to NDSolve?? Bob Hanlon <hanlonr357@gmail.com> - 2012-12-18 07:35 +0000
    Re: How to use FindMaximum with a parameter passed to NDSolve?? JBB <barandiaran.juan@gmail.com> - 2012-12-19 09:56 +0000

#12854 — Re: How to use FindMaximum with a parameter passed to NDSolve??

FromBob Hanlon <hanlonr357@gmail.com>
Date2012-12-18 07:35 +0000
SubjectRe: How to use FindMaximum with a parameter passed to NDSolve??
Message-ID<kap6bo$463$1@smc.vnet.net>
>From the documentation for FindMaximum: "FindMaximum first localizes
the values of all variables, then evaluates f with the variables being
symbolic, and then repeatedly evaluates the result numerically." This
initial symbolic evaluation launches a sequence of symbolic calls that
if not stopped will result in a call to NDSolve symbolically.
Restricting the definition of pendulum for only numeric arguments
terminates this sequence before the problem occurs.


Bob Hanlon


On Mon, Dec 17, 2012 at 4:38 PM, Juan Barandiaran
<barandiaran.juan@gmail.com> wrote:
> Thanks Bob,
> your answer solves my question: just by adding ?NumericQ to the function
> "pendulum" with NDSolve everything works fine apart from the precision
> problems which is another matter (not important in this case).
>
> pendulum[long_?NumericQ] :=
>  First[fi /.
>    NDSolve[{fi''[t] + 9.8 Cos[long] Sin[fi[t]] + .5 fi'[t] == 0,
>      fi[0] == Pi/2, fi'[0] == 0}, fi, {t, 0, 50}]]
>
> total[dynamics_, p_] := dynamics[3.1] + p
> FindMaximum[total[pendulum[long], long ] , {long, 0.01}]
>
> But why?
> What is the rationale behind it?
> If I understand correctly, using ?NumericQ in the variables of a function
> should test the variable and only execute the function if the variable is a
> number.
> Is this test enough for the FindMaximum function to assign a value to the
> long before calling total, and then pendulum, and then NDSolve? Why?
>
> Thanks a lot for your answer, it helped me a lot.
>
> Best regards,
>
> JBB
>
>
>
>
> 2012/12/17 Bob Hanlon <hanlonr357@gmail.com>
>>
>> Since pendulum uses NDSolve it can only be evaluated for a numeric
>> argument; consequently, its definition should be restricted to numeric
>> arguments. You will also need to use a higher precision than machine
>> precision.
>>
>> eqns = Rationalize[{
>>     fi''[t] + 9.8 Cos[long] Sin[fi[t]] +
>>       .5 fi'[t] == 0, fi[0] == Pi/2, fi'[0] == 0}, 0];
>>
>> pendulum[long_?NumericQ] := First[fi /. NDSolve[
>>     eqns, fi, {t, 0, 50}, WorkingPrecision -> 25]]
>>
>> total[dynamics_, p_] := dynamics[31/10] + p
>>
>> max = FindMaximum[{total[pendulum[long], long]},
>>    {long, 0.01`30}, WorkingPrecision -> 30];
>>
>> max /. x_?NumericQ :> Round[x, 10.^-6]
>>
>> {13.3595, {long -> 9.81034}}
>>
>>
>> Bob Hanlon
>>
>>
>> On Mon, Dec 17, 2012 at 2:58 AM, JBB <barandiaran.juan@gmail.com> wrote:
>> > Hello,
>> >
>> > This is probably a simple sintax question, but could somebody tell me
>> > how can I use the FindMaximum function when the variable used has to be used
>> > in an internal NDSolve??
>> >
>> > A simplified version of some code to show the error is as follows:
>> >
>> > pendulum[long_] :=
>> >  First[fi /.
>> >    NDSolve[{fi''[t] + 9.8 Cos[long] Sin[fi[t]] + .5 fi'[t] == 0,
>> >      fi[0] == Pi/2, fi'[0] == 0}, fi, {t, 0, 50}]]
>> >
>> > total[dynamics_, p_] := dynamics[3.1] + p
>> >
>> > FindMaximum[{total[pendulum[long], long ] }, {long, 0.01}]
>> >
>> > I get the following error:
>> >
>> > In[146]:= FindMaximum[
>> >  Evaluate[total[pendulum[long], long ] ], {long, 0.01}]
>> >
>> > During evaluation of In[146]:= NDSolve::ndnum: Encountered non-numerical
>> > value for a derivative at t == 0.`. >>
>> >
>> > During evaluation of In[146]:= ReplaceAll::reps: {NDSolve[{9.8 Cos[long]
>> > Sin[fi[<<1>>]]+0.5
>> > (fi^\[Prime])[t]+(fi^\[Prime]\[Prime])[t]==0,fi[0]==\[Pi]/2,(fi^\[Prime])[0]==0},fi,{t,0,50}]}
>> > is neither a list of replacement rules nor a valid dispatch table, and so
>> > cannot be used for replacing. >>
>> >
>> > During evaluation of In[146]:= FindMaximum::nrnum: The function value
>> > -0.01-fi[3.1] is not a real number at {long} = {0.01}. >>
>> >
>> > Out[146]= FindMaximum[long + fi[3.1], {long, 0.01}]
>> >
>> > If I understand correctly, probably because the variable "long" has not
>> > been assigned a value by FindMaximum  before calling the internal NDSolve.
>> >
>> > Is there a simple way of doing this?
>> > In my real case the functions are longer but the essence of the problem
>> > is the same: I have to find the maximum of some function in which the
>> > variables are parameters of other functions including a NDSolve.
>> >
>> > Thanks for any hint,
>> >
>> > JBB
>
>

[toc] | [next] | [standalone]


#12864

FromJBB <barandiaran.juan@gmail.com>
Date2012-12-19 09:56 +0000
Message-ID<kas2vr$cif$1@smc.vnet.net>
In reply to#12854
Crystal clear!! Thanks again Bob!
I'll have to learn how to read more carefully. ;-)

JBB

El martes, 18 de diciembre de 2012 08:35:20 UTC+1, Bob Hanlon  escribi=F3:
> >From the documentation for FindMaximum: "FindMaximum first localizes
>
> the values of all variables, then evaluates f with the variables being
>
> symbolic, and then repeatedly evaluates the result numerically." This
>
> initial symbolic evaluation launches a sequence of symbolic calls that
>
> if not stopped will result in a call to NDSolve symbolically.
>
> Restricting the definition of pendulum for only numeric arguments
>
> terminates this sequence before the problem occurs.
>
>
>
>
>
> Bob Hanlon
>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web