The solve function error

4 views (last 30 days)
When i try to run the following function i get the error '8 variables does not match 9 outputs'
[a,b,c,d,e,f,g,h,i] = solve('4*a + 1*b-1*d = 0',
'1*a-2*e = 0',
'6*a-1*h = 0',
'6*a-1*g = 0',
'1*b-1*f = 0',
'4*b + 4*c-4*d-12*e-4*f-3*g-2*h-1*i = 0',
'2*c-1*d-1*g-2*i = 0',
'1*c-1*d-3*e-1*f = 0',
'0*a + 0*b = 0');
But i have 9 variables and 9 equations although I added the last one

Accepted Answer

John D'Errico
John D'Errico on 31 Jan 2016
Edited: John D'Errico on 31 Jan 2016
The last equation is NOT an equation, since it is equivalent to 0==0. Pretending that it is one, is not going to make it so.
Next, using i as a variable here is a terrible thing to do, since that is already defined as sqrt(-1).
Next, learn how to define equations properly for solve to work. Once you do that, recognize that this is a linear homogeneous problem. So the solution is entirely 0. Thus a=b=c=d=e=f=g=h=ii=0 is the solution.
Oh. Perhaps you really wanted to see a solution that is non-zero. Inventing a non-equation will not do that. In fact, null would do the job very nicely, so you might want to learn something about linear algebra, and why null solves the problem. So, assuming I typed in your problem properly as a matrix of coefficients...
A = [4,1,0,-1,0,0,0,0,0;...
1,0,0,0,-2,0,0,0,0; ...
6,0,0,0,0,0,0,-1,0;...
6,0,0,0,0,0,-1,0,0;...
0,1,0,0,0,-1,0,0,0;...
0,4,4,-4,-12,-4,-3,-2,-1;...
0,0,2,-1,0,0,-1,0,-2;...
0,0,1,-1,-3,-1,0,0,0];
format rat
v = null(A)
v =
19/824
1159/4120
5792/8401
545/1459
19/1648
1159/4120
57/412
57/412
893/2060
Since a homogeneous linear system can be scaled by any constant, lets just scale it by the last element so that element is 1.
v./v(end)
ans =
5/94
61/94
299/188
81/94
5/188
61/94
15/47
15/47
1
null(sym(A))
ans =
5/94
61/94
299/188
81/94
5/188
61/94
15/47
15/47
1
If you absolutely insist on using solve here (effectively using a Mack truck to bring a pea to Boston) then add an equation that turns the homogeneous one into a valid one that has a non-zero solution. For example, we might try this:
syms a b c d e f g h ii
result = solve(4*a + 1*b - 1*d == 0,...
1*a -2*e == 0,...
6*a -1*h == 0,...
6*a -1*g == 0,...
1*b -1*f == 0,...
4*b +4*c -4*d -12*e -4*f -3*g -2*h -1*ii == 0,...
2*c -1*d -1*g -2*ii == 0,...
1*c -1*d -3*e -1*f == 0, ...
a^2 + b^2 + c^2 + d^2 + e^2 + f^2 + g^2 + h^2 + ii^2 == 1)
To compare...
result.a./result.ii
ans =
5/94
5/94
  2 Comments
Stephen23
Stephen23 on 31 Jan 2016
+1 Brilliant explanation
Bello Umar Muhamamd
Bello Umar Muhamamd on 31 Jan 2016
Thank you very much

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!