Physics Lab

Veikko Keränen -99
Rovaniemi Polytechnic
School of Technology
veikko.keranen@ramk.fi

Computing the Error

Differential of a Function

Below we define a function differential in Mathematica that computes all the partial derivatives of a given function g in symbolic form and uses these derivatives for computing the whole differential of the function g.

(* You may download the Fyslab.m
		package from here. So, you will not
  need the last part of this Notebook
		(The Mathematica Code Behind the Computations)
  for your own report. *)

<<"Y:\\Opettajat\\Veikko Keränen\\VeikkoKeranen1999-2004\\Fyslab.m"

In the next cell, press Shift Enter to activate the initialization cells of this notebook.

Then test that the computations work for the function  g. 

After testing, you may define your own functon, and substitute that for g. Please remember to do the substitution for each occurrence of  g  below.

Clear[g];
g[x_,y_,z_]:=(x^2)y + y z

g[a,b,c]

differential[ g[a,b,c] ]

g[37,15.2,21.7] //N

absoluteError[ g[a,b,c], 
{{37,.2},{15.2,.1},{21.7,.1}}] //N

Thus the result is   g  =  21100 ±  400   (in proper units)

The Mathematica Code Behind the Computations

The whole differential in a more general form:

Clear[g];
differential[ g[x,y,z,t] ]

Note the build-in Mathematica function Dt

myDiff = Dt[ g[x,y,z,t] ]

Abs /@ myDiff

Firstly, we define a function  partialDer  which computes all the partial derivatives of a given function in the following way:

Clear[v];
v[a_,b_,c_] := a^2 b^2 c^2

partialDer[ v[x,y,z] ]

The function partialDer can be defined as follows:

Clear[partialDer];
SetAttributes[partialDer,HoldAll];
partialDer[fu_]:=
 D[fu,#]& /@ 
(Hold[fu] /. 
  Hold[fu][[1,0]]->List)[[1]]

An Example

Clear[f];
f[x_,y_,z_]:=(x^2)+2 y-x z

partialDer[ f[x,y,z] ]

Explanation for the definition of partialDer

Clear[v];
v[a_,b_,c_]:=(a^2) (b^2) (c^2)

partialDer[ v[x,y,z] ]

Hold[v[x,y,z]][[1,0]]

myRes = Hold[v[x,y,z]] /. 
  Hold[v[x,y,z]][[1,0]]->List

myRes[[1]]

myRes // ReleaseHold

Clear[g];
SetAttributes[g,HoldAll];
g[x_]:=Hold[x]

(g[v[x,y,z]]/.v->List)[[1]]

Differential

The next function delta forms δx symbols for each argument x:

Clear[delta];
SetAttributes[delta,HoldAll];
delta[fu_]:= ToExpression[#]& /@
(StringJoin["δ",#]& /@
 (ToString /@ (Hold[fu] /. 
  Hold[fu][[1,0]]->List)[[1]]))

delta[f[a,b,c]]

Finally, the differential function can be defined simply as folllows:

Clear[differential];
SetAttributes[differential,HoldAll];
differential[fu_]:=
 Abs[partialDer[fu]].Abs[delta[fu]]

differential[ f[x,y,z] ]

Where we had  f[x_,y_,z_]:= x2 + 2y - x z

Another example:

Clear[w];
w[a_,b_,c_,d_]:=(a^2) (b^3) (c^4) (d^5)

differential[ w[x,y,z,t] ]

Absolute Error

Clear[absoluteError]; 
SetAttributes[absoluteError,HoldAll];
absoluteError[ fu_, xanddx__ ] := 
differential[ fu ] /.
((# /. List->Rule)& /@ ((Transpose /@ 
  Transpose[{Transpose[{(Hold[fu] /. 
   Hold[fu][[1,0]]->List)[[1]], delta[fu]}], 
   xanddx}]
   )//Flatten[#,1]&))

w[4,3,2,1]
absoluteError[ w[x,y,z,t], 
 {{4,.04},{3,.02},{2,.01},{1,.005}} ]

Thus the result is  w = 6900 ± 600  (in proper units)