0
$\begingroup$

I'm new to Mathematica. I want to built a function like that;

DeltaA1[PavP_,t1_,Jp_,A0_,tau1_,tau2] := AbsorbanceA1[0,t1,Jp,A0,tau1,tau2] - 
  AbsorbanceA1[PavP,t1,Jp,A0,tau1,tau2]

all of the t1,Jp etc are constants and they were defined. Only PavP is a list of 10 numbers. So want to do this calculation for ten times. I wrote this for loop, maybe I'm missing something.

For[j = 1, j <= Length[PavP], j++, 
 TdeltaA[PavP[[j_]], t1_, Jp_, A0_, tau1_, tau2_] := 
  AbsorbanceA1[0, t1, Jp, A0, tau1, tau2] - 
   AbsorbanceA1[PavP[[j]], t1, Jp, A0, tau1, tau2]]
$\endgroup$
  • $\begingroup$ Take a look at Map and Table instead :) $\endgroup$ – Öskå Dec 8 '14 at 12:20
  • $\begingroup$ Also look at the difference between Set (=) and SetDelayed (:=) $\endgroup$ – MikeLimaOscar Dec 8 '14 at 12:24
  • $\begingroup$ I don't think this is going to work. In your first definition the underscore is missing after tau2. In the loop you seem to want to define 10 functions, but in this case j in PavP[[j_]] should not have an underscore. Anyway, I don't see the necessity for defining 10 functions here. Just introduce a variable for the first position in the function and call the function with Pavp[[relevant value]] in the first position when needed. $\endgroup$ – Sjoerd C. de Vries Dec 8 '14 at 12:24
  • $\begingroup$ Maybe combine the above mentioned Map with an overload like Delta[Pav_?ListQ,...]:=Delta[#,...]&\@Pav and Delta[Pav_?NumberQ,...]:=Absorbance[...] $\endgroup$ – mikuszefski Dec 8 '14 at 13:30
1
$\begingroup$

I presume that you wish to calculate TdeltaA for all of the values of PavP at whatever the other constants are set to. If so, then there is a lot here that is incorrect. First of all, statements of the form

f[x_] := x^2

define a function, or more correctly a transformation rule. The left-hand side is a pattern that the execution engine matches. For example, from above, f[2] would return 4, f[a] would return a^2, but f[3, 4] would return f[3,4] because no rule has been set up for a two argument form of f.

Second, as pointed out in the comments, there is a difference between := (SetDelayed) and = (Set). Set will evaluate the right-hand side before registering the pattern, while SetDelayed does so after the pattern is matched. For example,

a = 5;
g[x_] = x^2 - a^2;
h[x_] := x^2 - a^2;
h[5] == g[5]
(* True *)

But, h is susceptible to changes in a:

a = 6;
h[5]
(* -11 *)

while g is not

g[5]
(* 0 *) 

For a more complete explanation, you should look up DownValues and OwnValues, and how they relate to each other.

Thirdly, the patterns on the left-hand side set up local variables on the right hand side in the function definition. If Set is used (not that I recommend it here), then right hand side is evaluated before the variables become localized. (They are localized on the left-hand side, though.) This means any global values would be used, if they existed. For example,

x = 5;
q[x_] = x^2 - a^2
(* - 11 *)

q[28]
(* -11 *)

the value of q is fixed. But, with SetDelayed, the named patterns (x in f, g, h, and q) become localized, and do not take on global values. For example, this

r[x_] := x^2 - a^2

acts identically to h, above, despite x having a global value.

To wrap up, what you are doing is creating a function, not a list of values, which is what I suspect you want. The For loop, in fact, is just overwriting the definition a bunch of times, and not generating anything useful. To generate your list, I would do the following:

DeltaA1 = AbsorbanceA1[0,t1,Jp,A0,tau1,tau2] - 
 AbsorbanceA1[#,t1,Jp,A0,tau1,tau2]& /@ PavP

which evaluates (see Map)

AbsorbanceA1[0,t1,Jp,A0,tau1,tau2] - AbsorbanceA1[#,t1,Jp,A0,tau1,tau2]

for each value in PavP by replacing # with that value. This results in a List which is accessed by DeltaA1.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.