Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1999
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Re: NonlinearRegress and numerical functions...

  • To: mathgroup at smc.vnet.net
  • Subject: [mg20190] RE: [mg20180] Re: [mg20132] NonlinearRegress and numerical functions...
  • From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
  • Date: Tue, 5 Oct 1999 04:04:20 -0400
  • Sender: owner-wri-mathgroup at wolfram.com

Larske Ragnarsson presented a problem about NonLinearRegression. In his
demonstration of the problem he used the following line.

In[25]:=
g1[t1_]:=y[t]/.DSolve[{y'[t]==-3*y[t],y[0]==6},y[t],t][[1]]/.
    t->t1


The line above stores the following definition for (g1).

In[26]:=
??g1

   Global`g1

   g1[t1_]:=y[t]/.DSolve[{y'[t]==-3 y[t],y[o]==6},y[t],t][[1]]/.t->t1

-------------------------------
Bob Hanlon noted that the line above will solve the diff-eq each time 
g1[_] is evaluated.  Bob recommended the following line which will only
solve the diff-eq when the function is defined.


In[29]:=
g2[t1_]:=Evaluate[y[t]/.DSolve[{y'[t]==-3*y[t],y[0]==6},y[t],t][[1]]/.
      t->t1]

In[31]:=
??g2

   Global`g2

   g2[t1_]:=6/E^(3*t1)


--------------------------------
I point out that the following line does the same thing and requires a
little less typing.


In[33]:=
g3[t1_]=y[t]/.DSolve[{y'[t]==-3*y[t],y[0]==6},y[t],t][[1]]/.
      t->t1;


In[34]:=
??g3

   Global`g3

   g3[t1_]:=6/E^(3*t1)


--------------------------------
However, the previous two solutions don't store the intended solution if
(t1) happens to have a global value.  It may be that the global value was
assigned early in a Mathematica session, and you have long since forgot
about it.  Consider the examples below.

In[36]:=
Remove[g2,g3];
t1=7;


In[38]:=
g2[t1_]:=Evaluate[y[t]/.DSolve[{y'[t]==-3*y[t],y[0]==6},y[t],t][[1]]/.
      t->t1]


In[39]:=
g2[a]

Out[39]=
6/E^21    (* Wrong Answer!  *)


---------------------------------

In[40]:=
g3[t1_]=y[t]/.DSolve[{y'[t]==-3*y[t],y[0]==6},y[t],t][[1]]/.
      t->t1;

In[41]:=
g3[b]

Out[41]=
6/E^21  (* Wrong Answer!  *)


----------------------------------
You can solve the diff-eq only once, and ensure the intended definition is
stored by clearing global values from the symbol (t1) before the definition
is made.  Of course (t1) better not have any values that you care about.
That is what I do below.

In[44]:=
Clear[t1];
g4[t1_]=y[t]/.DSolve[{y'[t]==-3*y[t],y[0]==6},y[t],t][[1]]/.
      t->t1;


In[45]:=
??g4

   Global`g2

   g2[t1_]:=6/E^(3*t1)

-----------------------------

For another approach you can use a function in my (LocalPatterns.m) package.
With this package you can be sure the right definition is stored, and you
don't have to clear any definitions.  You can download the package and a
notebook where it's demonstrated from
http://www.mathsource.com/cgi-bin/msitem?0210-609

  
------------------------------
Regards,
Ted Ersek

For Mathematica tips, tricks see 
http://www.dot.net.au/~elisha/ersek/Tricks.html


  • Prev by Date: RE: Rebuilding polygon from CoefficientList?
  • Next by Date: Drawing Rectangular Grid
  • Previous by thread: Re: NonlinearRegress and numerical functions...
  • Next by thread: Re: NonlinearRegress and numerical functions...