2
$\begingroup$

(This is the function that I programmed)

Zm2[Z_, nk_] := (zm2 = 0;
   For[i = 0, i < Length[Z], i++, zm2 += nk[[i]] *Z[[i]]* Z[[i]];];
   Return[zm2];);

(These are the input data)

z = List[82, 57, 40, 22, 41, 8];
n = List[0.1973, 0.002, 0.1068, 0.0930, 0.002, 0.5989];

(Hear I evaluate the function in the input data)

Zm2[z, n]

(This is the result)

1552.4 + List^3

I need to remove the +List^3; the value 1552.4 is correct, but I can't use the function for further calculations with the +List^3.

PLEASE IF SOMEONE CAN HELPE ME WITH THAT, THIS IS FOR MY THESIS WORK.

$\endgroup$
  • 1
    $\begingroup$ Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory Tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. $\endgroup$ – bbgodfrey May 25 '15 at 19:14
4
$\begingroup$

First problem, subscripts start with 1 in Mathematica, not 0 like in many other languages.

One alternative

z = List[82, 57, 40, 22, 41, 8];
n = List[0.1973, 0.002, 0.1068, 0.0930, 0.002, 0.5989]; 
Zm2[Z_, nk_] := Sum[nk[[i]]*Z[[i]]^2, {i, 1, Length[Z]}];
Zm2[z, n]

which gives 1590.73

Or perhaps just using

Total[n*z^2]

which gives the same result, but that uses different features of Mathematica

$\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.