2
$\begingroup$

Say I have an equation:

eq = ax + by + c == 0

and I want to run NSolve on a set of equations of this form, for example:

NSolve[{5x + 7y - 6, 9x - 2y + 3}]

How can I use the single definition of eq above to do this, by replacing a, b and c with the required values? It is ok to write it manually in NSolve for a simple example as above, but the real equation I will be doing this with has many more terms, and I will be solving a set of 8 equations!

Overall it'd be great if I could take a single equation like eq above, and then a list of the values that I want to be used (eg. a list of values for a, a list of values for b and a list of values for c) and then generate a list to pass to NSolve.

This is probably a fairly simple question, but I'm a beginner with Mathematica (although an experienced programmer in many other languages), so I'm not sure how to approach it. I suspect there may be a built-in function that will do this (or something like it) - is that the case?

$\endgroup$
2
  • 1
    $\begingroup$ There must be spaces between a and x, b and c defining eq. eq = a x + b y + c == 0; then better use Solve rather than NSolve : Solve[eq /. {{a -> 5, b -> 7, c -> -3}, {a -> 9, b -> -2, c -> 3}}, {x, y}]. /. is a shorthand for ReplaceAll. $\endgroup$
    – Artes
    Feb 5, 2013 at 13:08
  • 1
    $\begingroup$ Those lists of values of a and b, when assembled into a list, form a matrix which you can pass directly to LinearSolve along with the list of c values. When you have more equations than variables, though, you need to use a least squares approach: look to LeastSquares and its kin. $\endgroup$
    – whuber
    Feb 5, 2013 at 13:17

1 Answer 1

3
$\begingroup$

the way you input values (without actually replacing the parameter with a number) is as follows:

eq/.{a->1,b->2,c->3}

this will result in:

x+2y+3.

you can put this in a loop (or something more clever, I'm new to mathematica as well) and output your results into an array or whatever you like.

hope this was helpful. Good Luck!

$\endgroup$

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.