solving multiple equations

3 views (last 30 days)
Tomas
Tomas on 2 Feb 2012
Edited: Matt J on 26 Sep 2013
This is rather simple. Im just begining to use matlab and I was trying to find the equation to a line that intersected at (1,4) and (2,2). this is easily done by hand but I wanted to save time. I entered the code: [m,b]=solve('m+b=4','2*m+b=2')
this gave me: m= 6 b= -2 . This is obviously wrong, if you plug in those answers in the second equation you get "10=2". I tried changing the code to [b,m]=solve('m+b=4','2*m+b=2'). (Notice I wrote [b,m] instead of [m,b]) And then I got the correct result: m=-2, b= 6. Why does this happen? What did I do wrong?

Answers (1)

Walter Roberson
Walter Roberson on 2 Feb 2012
This is expected. solve() does not look at the names of the output variables to determine the order in which to output the variables. When you do not specify the order of the variables in the solve() call, solve() uses symvar() to locate the variables. symvar() has an unusual ordering choice that happens to choose 'b' before 'm'
What you should do is specify the order of the variables:
[m,b] = solve('m+b=4','2*m+b=2','m','b');
  1 Comment
Tomas
Tomas on 2 Feb 2012
Still wont work, I copied your code and pasted it on matlab and it still tells me m=6 and b=-2.
Thanks anyways for taking the time to answer.
A different aproach I tried was:
EDU>>eq1 =
m+b=4
EDU>> eq2='m*2+b=2'
eq2 =
m*2+b=2
EDU>> syms m b
EDU>> solve(eq1,eq2)
%but it gave me this answer:
ans =
b: [1x1 sym]
m: [1x1 sym]

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!