Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'FindMinimum NDSolve conflict' topicStudent Support Forum > General > Archives > "FindMinimum <-> NDSolve conflict"

< Previous Comment | Next Comment >Help | Reply To Comment | Reply To Topic
Author Comment/Response
Bill Simpson
09/15/12 2:53pm

I modify your code slightly so that we can plot the solution for a range of values of b.

In[1]:= testFct[b_?NumericQ]:= NDSolve[{y''[t]==10,y[0]==b,y'[0]==0},y,{t,-10,10}][[1,1]];
g=Table[f[t_]:=y[t]/.testFct[b];Plot[f[t],{t,-10,10}],{b,-10,10,1}];
Show[g]

<plot snipped>

Notice that the range is so large that the small variation in the minimum is obscured, so zoom in to see a smaller range.

In[4]:= testFct[b_?NumericQ]:= NDSolve[{y''[t]==10,y[0]==b,y'[0]==0},y,{t,-1,1}][[1,1]];
g=Table[f[t_]:=y[t]/.testFct[b];Plot[f[t],{t,-1,1}],{b,-10,10,1}];
Show[g]

<plot snipped>

By inspection the minimum will occur at t==0 and the value at that point will be b. Thus without constraint on the value of b the minimum will go to -Infinity and Nminimize may fail before it gets that far.

So I modify your example to have y[0]==b^2+1 to avoid having the limit at -Infinity and it does correctly find that the minimum is 1 when b is approximately 0.

In[7]:= testFct[b_?NumericQ]:=(sol=y/.NDSolve[{y''[t]==10,y[0]==b^2+1,y'[0]==0},y,{t,-1,1}][[1,1]];sol[0]);
NMinimize[testFct[b],{b}]

Out[8]= {1.`, {b->-7.883649986482402`*^-9}}

Rather than brute forcing testFct to return sol[0], as discovered by inspection, I would like to have it use NMinimize to find that minimum itself, but for some reason I can't get the syntax correct at the moment.

Hopefully these examples will be enough to help you.

It would be really nice if the documentation had a brilliantly clear example of doing problems like this. Google searches show that again and again users are asking how to do this, and it took me some time and repeated searches and aggravation before I got this partly correct.

URL: ,

Subject (listing for 'FindMinimum <-> NDSolve conflict')
Author Date Posted
FindMinimum <-> NDSolve conflict Enrico 09/10/12 2:46pm
Re: FindMinimum <-> NDSolve conflict Bill Simpson 09/15/12 2:53pm
Re: Re: FindMinimum <-> NDSolve conflict Enrico 09/16/12 05:03am
Re: Re: Re: FindMinimum <-> NDSolve conflict Bill Simpson 09/17/12 1:17pm
< Previous Comment | Next Comment >Help | Reply To Comment | Reply To Topic