1
$\begingroup$

I intend to sum a series over two lists which I generate myself. However, my code is not working. What is the best way to do this? Thanks. Find my code below

w = {5, 1, 2, 2, 8}; 
x = {0.5, 0.8, 4.6, 3.2, 5}; 
r = 1; 
mysum = Sum[
  w[i]*Exp[10^(((-Sqrt[2])*2*x[i] - 2*r)/10)], {i, 1, Length[w]}]

Edit 1 Further to this question, Let's suppose r is a list. How do I sum over to this list to my results equal to the length of r.

The code is below

w = {5, 1, 2, 2, 8};
x = {0.5, 0.8, 4.6, 3.2, 5};
r = {1, 2, 3, 4, 5};
mysum = Map[Total[w[*Exp[10^(((-Sqrt[2])*2*x - 2*r)/10)]], r]
$\endgroup$
  • 1
    $\begingroup$ replace w[i] with w[[i]] and x[i] with x[[i]] $\endgroup$ – kglr Jul 3 '18 at 7:28
  • 1
    $\begingroup$ You can also use the Dot function, as w . Exp[10^(((-Sqrt[2])*2*x - 2*r)/10)] $\endgroup$ – LouisB Jul 3 '18 at 7:57
1
$\begingroup$

Here is an alternative approach. Consider that many numeric Mathematica functions are Listable. Look at the following

w = {5, 1, 2, 2, 8};
x = {0.5, 0.8, 4.6, 3.2, 5};
r = 1;

Sqrt[w]
(* {Sqrt[5], 1, Sqrt[2], Sqrt[2], 2 Sqrt[2]} *)

You provide a list to Sqrt and it knows that it needs to be applied on each element. If you know this, you can use exactly the same in your expression and then you sum all elements together using Total

Total[w*Exp[10^(((-Sqrt[2])*2*x - 2*r)/10)]]
(* 21.7645 *)
$\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.