# Partial derivative of defined function with vector input

Background: Have written a formula for the refractive index for a glass slab (n) that depends on the variables t,a and d. Now I want to calculate for example some sort of error and I'll be needing to use partial derivatives but I can't seem to make it work with the vectors I made for my values.

Current work:

t = {0.349, 0.698, 1.221};
a = 10^-3 * {0.935, 1.110, 1.260};
d = 1.96 * 10^-3 ;

n[t_, a_, d_] = Sqrt[a^2 * Sin[t]^2 + d^2 * Sin[2*t]^2] / a;


The output from this gives me the calculated values for n in a nice and tidy vector.

My problem begins now when I want to calculate some sort of error by error propagation (http://en.wikipedia.org/wiki/Propagation_of_uncertainty#Simplification).

My shot at it has been a series of tries all closely resembling something like this:

Sqrt[{D[n[t], {{t, a, d}}]*1.7*10^-3}^2 + {D[n[a], {{t, a ,d}}]*10^-3}^2 + {10^-3}]


All I want it to do is to take the partial derivative of my function with respect to one of the variables and have it calculated at the points specified by my vectors and hopefully present it as a vector just as neatly as the function did.

First time using Mathematica, please have mercy.

Well,

n[1, 1, 1]
(* {1.38996, 1.85383, 1.37325} *)

n[5, 5, 5]
(* {1.38996, 1.85383, 1.37325} *)


When you first define your variables and then assign n[...] to this expression, the expression will evaluate and then be stored as that value. Regardless of what values you pass to n it will always return the same thing. You can read more about how Mathematica stores assignments here. You should also read the documentation on Set and SetDelayed to understand the difference between normal assignment and delayed assignment.

If you restart your kernel and execute this:

n[t_, a_, d_] = Sqrt[a^2*Sin[t]^2 + d^2*Sin[2*t]^2]/a;

t = {0.349, 0.698, 1.221};
a = 10^-3*{0.935, 1.110, 1.260};
d = 1.96*10^-3;


and then repeat the experiment I started with you will now obtain

n[1, 1, 1]


n[5, 5, 5]


and if you give it undefined symbols they'll be substituted into the expression as well:

n[x, y, z]


You can't take the derivative of a constant vector like you were doing before, but you can take the derivative of an expression like this.

D[n[x, y, z], x]


The gradient you are looking for can then be calculated by D[n[x,y,z], {{x,y,z}}], but this will give you a symbolic expression. Then you have to substitute your values for x, y and z which you can do with a replacement rule. For example {x, y} /. {x -> 5, y -> 7} will give you {5,7}.

grad = D[n[x, y, z], {{x, y, z}}] /. {x -> t, y -> a, z -> d}


I'd probably write this formula as:

t = {0.349, 0.698, 1.221};
a = 10^-3*{0.935, 1.110, 1.260};
d = 1.96*10^-3;

n[t_, a_, d_] := Sqrt[a^2*Sin[t]^2 + d^2*Sin[2*t]^2]/a;

error[t_, a_, d_] := Module[{refr, grad},
refr = n[t, a, d];
grad = grad /. {v1 -> 1, v2 -> a, v3 -> d};
]

error[t, a, d]

(* {{1.91521, 1.4607, 1.1626}, {3457.36, 2375.13, 1788.53}, {1221.75,
996.403, 851.707}} *)


Because I used delayed assignment (:=) it doesn't matter if I define my variables before I define n.

(You might need to restart your kernel before you can try this piece of code, since we have defined n differently earlier on.)

• @Prickett What is happening to grad when we want the variables to be evaluated with vectors instead of single points? Because it doesn't seem to be behaving "like I want it". Grad gives us a 1x3 vector with numerical values that seem completely legit, but as soon as we use the evaluate thing I get output that I don't expect. For example, one of the outputs should be the equivalent of the output from in: grad = Grad[n[v1, v2, v3], {v1, v2, v3}] /. {v1 -> 1.221, v2 -> 1.260, v3 -> 1.96*10^-3} out: {0.342704, -8.47569*10^-7, 0.000544866} So I must be misunderstanding how that is working. – Ash Apr 18 '15 at 16:20
• @Ash Grad doesn't give you a numerical expression, it only becomes numerical after you substitute the numbers in. Are you saying that if you substitute in for example {1,2,3} for v then you get an unexpected result? – C. E. Apr 18 '15 at 16:25
• @Prickett No, sorry about that, it was just a matter of misunderstanding the matrix representation and subsequently the multiplications! And yes, "numerical" was the wrong word to use there. – Ash Apr 18 '15 at 16:39