Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am trying to get an equation from summing other equations but it doesn't result in a correct answer. Is there another way of doing this?

n = 4; xList = {1, 2, 3, 4, 5}; yList = {4, 5, 6, 8, 7};
equation[x_] = a*(x^4) + b*(x^3) + c*(x^2) + d*(x^1) + e*(x^0);
eqSummer = 0;

Do[
  eqSummer = eqSummer +  equation[xList[[i]]];
  , {i, 1, Length[xList]}
];

eqSum
36768/25 + (512 c)/5 + 10 e
share|improve this question
    
What are n and yList for? I assume eqSummer and eqSum are the same thing and the code has been edited? –  Ymareth Jan 14 at 9:11

2 Answers 2

up vote 2 down vote accepted
equation[x_] = a*(x^4) + b*(x^3) + c*(x^2) + d*(x^1) + e*(x^0);

and

Total[Map[equation, {1, 2, 3, 4, 5}]]

gives 979 a + 225 b + 55 c + 15 d + 5 e

Is this the expected answer?

share|improve this answer
    
It is, thank you –  Teodor Kostadinov Jan 14 at 9:35

The following might also be helpful for adding equations rather than expressions. http://www.mathconsult.ch/math/stuff/equalthread.html

The code boils down to:

listableQ[f_] := MemberQ[Attributes[f], Listable]
protected = Unprotect[Equal]

Equal/: lhs:f_Symbol?listableQ[___, _Equal, ___] :=
    Thread[ Unevaluated[lhs], Equal ]

Protect[Evaluate[protected]]
share|improve this answer
    
Although you've obviously included proper attribution of this code in your post, I just want to add here for everyone to see--this code is copyright of Roman Maeder, who requires the following notice to be provided with it: "© 1997 by MathConsult Dr. R. Mäder. Permission is granted to use and distribute this file for any purpose except for inclusion in commercial software or program collections. This copyright notice must remain intact." I strongly recommend that people who might want to do this operation in a commercial application/package DO NOT look at this post. –  Oleksandr R. Jan 14 at 18:15
    
My apologies. One could consider rewriting this code without too much trouble. –  jlperla Jan 14 at 20:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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