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.

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?

share|improve this question
1  
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. –  Artes Feb 5 '13 at 13:08
1  
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. –  whuber Feb 5 '13 at 13:17
add comment

1 Answer

up vote 2 down vote accepted

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!

share|improve this answer
add comment

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.