2
$\begingroup$

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
$\endgroup$
1
  • $\begingroup$ What are n and yList for? I assume eqSummer and eqSum are the same thing and the code has been edited? $\endgroup$
    – Ymareth
    Commented Jan 14, 2014 at 9:11

2 Answers 2

3
$\begingroup$
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?

$\endgroup$
1
  • $\begingroup$ It is, thank you $\endgroup$ Commented Jan 14, 2014 at 9:35
1
$\begingroup$

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]]
$\endgroup$
2
  • 1
    $\begingroup$ 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. $\endgroup$ Commented Jan 14, 2014 at 18:15
  • 1
    $\begingroup$ My apologies. One could consider rewriting this code without too much trouble. $\endgroup$
    – jlperla
    Commented Jan 14, 2014 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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