solving multiple equations

조회 수: 6 (최근 30일)
Tomas
Tomas 2012년 2월 2일
편집: Matt J 2013년 9월 26일
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?

답변 (1개)

Walter Roberson
Walter Roberson 2012년 2월 2일
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
Tomas
Tomas 2012년 2월 2일
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]

댓글을 달려면 로그인하십시오.

제품

Community Treasure Hunt

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

Start Hunting!

Translated by