*To*: mathgroup@smc.vnet.net*Subject*: [mg10561] Re: Derivative via mathematica*From*: Allan Hayes <hay@haystack.demon.co.uk>*Date*: Tue, 20 Jan 1998 16:54:04 -0500*References*: <69ncl9$8d5@smc.vnet.net>

cai wrote: > > Hi, > > I just used mathematica for a couple of days. I am trying to compute > the derivative under mathematica. Because the function is complicated, > I like to break it down. > > f[t_] = (m/(1+Exp[1/t] +b) > > Here m and b are functions of t. > If I directly use command D after insert m and b terms, a very > complicated equaion is gerenated, which I do not want. > > What I want is if I define the values of m' and b', rewrite the f > > m' = p > b' = q // well, I dont know how to define, this is the idea > > f[t_, m[t], b[t]] = (m/(1+Exp[1/t] +b) > > then use the command D[f[t,m[t],b[t]],t] hopeful get a equation which is > the function of t, p and q. How to do that? > > A related question, I tried to use non-defined function In[19]:= m[t_] > Out[19]= m[t_] > In[20]:= D[m[t],t] > Out[20]= m'[t] > and expected D[f[t,m[t],b[t]],t] contains m'[t]. Is it possible? > > Basically, it is a chain derivative question, I just want it to stop > earlier. > > Could somebody help? > Thanks. > > ccai1@ohiou.edu In[25]:= Clear["`*"] In[26]:= $Line = 0; Wan, here are some suggestions and comments. They do not address all of your points but I hope that they may be of help - please mail me if you need more help. Mathematica, of course, uses the standard results In[1]:= D[a f[t]+ b g[t],t] Out[1]= a f'[t] + b g'[t] In[2]:= D[f[t] g[t],t] Out[2]= g[t] f'[t] + f[t] g'[t] In[3]:= D[1/ g[t],t] Out[3]= g'[t] -(-----) 2 g[t] In[4]:= D[f[g[t]],t] Out[4]= f'[g[t]] g'[t] And we can put in values for f and g , f' and g' retrospectively. This will be demonstrated in the treatment of the example below In[5]:= f1[t_] = m/(1+Exp[1/t] +b) Out[5]= m ------------ 1/t 1 + b + E Mathematica assumes that m and b are independent of t. In[6]:= d1=D[f1[t],t] Out[6]= 1/t E m ------------------ 1/t 2 2 (1 + b + E ) t Which is not what you want. We can avoid this by using In[7]:= f2[t_] = m[t]/(1+Exp[1/t] +b[t]) Out[7]= m[t] --------------- 1/t 1 + E + b[t] Now we get, In[8]:= d2=D[f2[t],t] Out[8]= 1/t E m[t] (-(----) + b'[t]) 2 t m'[t] -(----------------------) + --------------- 1/t 2 1/t (1 + E + b[t]) 1 + E + b[t] If we define the functions m and b In[9]:= m[t_] := Sin[t]; b[t_]:= Exp[k t] we get In[10]:= d2 Out[10]= 1/t k t E (E k - ----) Sin[t] 2 Cos[t] t --------------- - ---------------------- 1/t k t 1/t k t 2 1 + E + E (1 + E + E ) It is possible to define the derivatives of these functions but we cannot consistently define a function ad its derivative independently so we clear the definitions above (there are also programming reasons for this) In[11]:= Clear[m,b] In[12]:= m'[t_] = Cos[t^2]; b'[t_]:= t Exp[k t]; These derivative definitiions are used In[13]:= d2 Out[13]= 1/t E k t (-(----) + E t) m[t] 2 2 Cos[t ] t --------------- - ----------------------- 1/t 1/t 2 1 + E + b[t] (1 + E + b[t]) but Mathematica has left m[t]; it has not integrated m'[t] to find it. Defining derivatives can be useful but perhaps not so much here. Incidentally the derivatives are stored under Derivative, not m and b. In[14]:= ??m >From In[14]:= Global`m In[15]:= ??Derivative >From In[15]:= f' represents the derivative of a function f of one argument. Derivative[n1, n2, ... ][f] is the general form, representing a function obtained from f by differentiating n1 times with respect to the first argument, n2 times with respect to the second argument, and so on. Derivative[1][m][t_] = Cos[t^2] Derivative[1][b][t_] := t*Exp[k*t] This, and the notation m'[t], b'[t], leads us to the important contrast between functions and formulas: g' is the first derivative of the function g; g'[t] is its value at t - it is the same as D[g[t],t], the derivative of the formula g[t] with respect to the variable t. The FullForm of g' is In[16]:= FullForm[g'] Out[16]//FullForm= Derivative[1][g] In[17]:= g'[t] Out[17]= g'[t] In[18]:= D[g[t],t] Out[18]= g'[t] Once you get into pure functions you may wish to use a different apporoach which reduces the chance of interference between definitions. But first, I need to clear the definitions of the derivatives. In[19]:= Clear[Derivative] In[20]:= f2[t]/.{ m -> Function[t, Sin[t]], b -> Function[t, Exp[k t]]} Out[20]= Sin[t] --------------- 1/t k t 1 + E + E In[21]:= d2/.{ m ->Sin, b -> Function[t, Exp[k t]]} Out[21]= 1/t k t E (E k - ----) Sin[t] 2 Cos[t] t --------------- - ---------------------- 1/t k t 1/t k t 2 1 + E + E (1 + E + E ) or, more briefly, In[22]:= d2/.{ m -> Sin,b -> (Exp[k #]&)} Out[22]= 1/t k t E (E k - ----) Sin[t] 2 Cos[t] t --------------- - ---------------------- 1/t k t 1/t k t 2 1 + E + E (1 + E + E ) A variant of your idea for f can be implemented by In[23]:= f[m_,b_]= f2[t] Out[23]= m[t] --------------- 1/t 1 + E + b[t] In[24]:= D[f[ Cos, Exp[k #]&],t] Out[24]= 1/t k t E (E k - ----) Cos[t] 2 t Sin[t] -(----------------------) - --------------- 1/t k t 2 1/t k t (1 + E + E ) 1 + E + E You might like to look up the following in the Help Browser D Derivative Dt (total derivative) Please notice that these functions also deal with several variables. -- -- Allan Hayes Training and Consulting Leicester, UK hay@haystack.demon.co.uk http://www.haystack.demon.co.uk voice: +44 (0)116 271 4198 fax: +44 (0)116 271 8642