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.

Using the function 'Solve' I can do the following:

Solve[a x + y == 7 && b x - y == 1, {x, y}]

I get the following output:

enter image description here

I want to get the same output using 'LinearSolve' instead. How is this possible and also what's the difference between the two?

share|improve this question

closed as off-topic by Öskå, m_goldberg, rasher, Mike Honeychurch, Jens Jun 16 '14 at 5:18

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Öskå, m_goldberg, rasher, Mike Honeychurch, Jens
If this question can be reworded to fit the rules in the help center, please edit the question.

Open up the documentation and check out the differences between the two :) –  Sektor Jun 15 '14 at 11:35
Would you just answer the question properly, or don't answer at all please if you don't know it. –  user152356 Jun 15 '14 at 11:38
@user152356: a kind hint from Sektor does not seem to motivate you to press F1, and learn that the analogous result can be found using LinearSolve[{{a, 1}, {b, -1}}, {7, 1}] –  Wouter Jun 15 '14 at 11:49
It doesn't assign 'x' and 'y' to the result, it just gives a vector of a solution. –  user152356 Jun 15 '14 at 12:05
would you care to elaborate on your motivation? Right now I interpret your question to read, "Solve does what I want it to, but I want LinearSolve to do what Solve does." –  bobthechemist Jun 15 '14 at 12:29

1 Answer 1

s = {x, y} /. Solve[a x + y == 7 && b x - y == 1, {x, y}][[1]]

{8/(a + b), -((a - 7 b)/(a + b))}

lsa = LinearSolve[{{a, 1}, {b, -1}}, {7, 1}]

{8/(a + b), (-a + 7 b)/(a + b)}

f = LinearSolve[{{a, 1}, {b, -1}}];

lsb = f[{7, 1}] // Simplify

{8/(a + b), -((a - 7 b)/(a + b))}

s == lsa == lsb // Simplify


Solve can handle a broader class of problems, can accept constraints and has more options available. However, given a problem appropriate for LinearSolve, it should be much faster.

Comparing the timings

n = 200;

coef = RandomInteger[{-20, 20}, {n, n}];

b = RandomInteger[{-20, 20}, n];

var = Array[x, n];

eqns = coef.var == b;

t1 = Timing[sol1 = var /. Solve[eqns, var][[1]];][[1]]


t2 = Timing[sol2 = LinearSolve[coef, b];][[1]]


{sol1 == sol2, t1/t2}

{True, 2.0058}

LinearSolve is about twice as fast as Solve for this example (200 equations with 200 unknowns).

share|improve this answer

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