MATLAB Answers

## what is tje problem whit the function "solve"?

Joaquin Sanchez

### Joaquin Sanchez (view profile)

さんによって質問されました 2019 年 4 月 4 日

### John D'Errico (view profile)

さんによって コメントされました 2019 年 4 月 5 日
John D'Errico

### John D'Errico (view profile)

さんの 回答が採用されました
Hi everyone, I have a problem with de function "solve", I have two programs very similar.
the first works excellent, it's the following:
clear all;
clc;
syms i1 i2 i3 i4 i5 i6 i7 v1 v2 v3 v4 v5 v6 v7 e1 e2 e3 e4 e5 R1 R2 R3 R4 A va vb;
%KCL
eq0=i1-i2;
eq1=i2+i3;
eq2=i4-i5;
eq3=i5-i6;
eq4=-i3+i7;
%KVL
eq5= v1-e1;
eq6= v2+e2-e1;
eq7= v3+e2-e5;
eq8= v4-e3;
eq9= v5+e4-e3;
eq10= v6-e4;
eq11= v7-e5;
%BR
eq12= v1-va;
eq13= v2-i2*R1;
eq14= v3-i3*R2;
eq15= v4-vb;
eq16= v5-i5*R3;
eq17= v6-i6*R4;
eq18= v7-A*e4+A*e2;
S=solve([eq0,eq1,eq2,eq3,eq4,eq5,eq6,eq7,eq8,eq9,eq10,eq11,eq12,eq13,eq14, eq15, eq16, eq17, eq18], [i1,i2,i3,i4,i5,i6,i7,v1,v2,v3,v4,v5,v6,v7,e1,e2,e3,e4,e5]);
sol= S.i2
the second is the following:
clear all;
clc;
syms i3 i4 i5 i6 i7 i8 i9 i10 i11 i12 v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 e3 e4 e5 e6 e7 e8 e9 R1 R2 R3 Rg A;
%KCL
eq0= i3+i12-i4;
eq1= i4-i5;
eq2= i5+i6;
eq3= i11-i6-i8;
eq4= i7-i3;
eq5= i8-i10;
eq6= i9-i7;
%KVL
eq7= v3-e7+e3;
eq8= v4-e3+e4;
eq9= v5-e4+e5;
eq10= v6-e6+e5;
eq11= v7-e9+e7;
eq12= v8-e6+e8;
eq13= v9-e9;
eq14= v10-e8;
eq15= v11-e6;
eq16= v12-e3;
%BR
eq17= v3-R2*i3;
eq18= v4-R1*i4;
eq19= v5-Rg*i5;
eq20= v6-R1*i6;
eq21= v7-R3*i7;
eq22= v8-R2*i8;
eq23= v9-A*e8+A*e7;
eq24= v10-R3*i10;
eq25= v11-A*v2+A*e5;
eq26= v12-A*v1+A*e3;
S=solve(eq0,eq1,eq2,eq3,eq4,eq5,eq6,eq7,eq8,eq9,eq10,eq11,eq12,eq13,eq14,eq15,eq16,eq17,eq18,eq19,eq20,eq21,eq22,eq23,eq24,eq25,eq26,'i3','i4','i5','i6','i7','i8','i9','i10','i11','i12','v1','v2','v3','v4','v5','v6','v7','v8','v9','v10','v11','v12','e3','e4','e5','e6','e7','e8','e9');
sol=S.e9
I don't know what is the problem because I always get zero.
I hope you can help me, thanks

#### 0 件のコメント

サインイン to comment.

R2017b

## 1 件の回答 ### John D'Errico (view profile)

2019 年 4 月 4 日
採用された回答

Is zero a valid solution? It appears to be so. (By the way, you don't put the names of the variables in quotes to call solve. But you knew that from the first time you used solve, in the case that you claim did work.)
Your system appears to be linear. It has no right hand side. Thus it is effectively a linear system with no constant right hand side. zero is avalid solution. Even if I missed a product in there between two of the unknowns, it is still one with a zero right hand side as far as I can see.
A valid solution to any such system is ZERO.
Worse, it looks like you have 27 equations, and 29 unknowns? I'm actually surprised that solve gave you any solution at all, since the system is under-determined. So there will be infinitely many solutions.
So, next, lets see what equationsToMatrix tells us about your linear system.
[M2,rhs2] = equationsToMatrix(eq0,eq1,eq2,eq3,eq4,eq5,eq6,eq7,eq8,eq9,eq10,eq11,eq12,eq13,eq14,eq15,eq16,eq17,eq18,eq19,eq20,eq21,eq22,eq23,eq24,eq25,eq26,[i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,e3,e4,e5,e6,e7,e8,e9]);
As I thought, the system is homogeneous, rhs is identically zero.
rhs2'
ans =
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
We see the system is indeed 27x29, so it is underdetermined.
size(M2)
ans =
27 29
There are infinitely many solutions, one of which is the all zero solution. Of course this must be true, because it is trivially true that M2*zeros(29,1)=0.
To go back and answer your question, why did the solve work the firt time you used solve?
[M1,rhs1] = equationsToMatrix([eq0,eq1,eq2,eq3,eq4,eq5,eq6,eq7,eq8,eq9,eq10,eq11,eq12,eq13,eq14, eq15, eq16, eq17, eq18], [i1,i2,i3,i4,i5,i6,i7,v1,v2,v3,v4,v5,v6,v7,e1,e2,e3,e4,e5]);
It worked because your system is not homogeneous there.
rhs1
rhs1 =
0
0
0
0
0
0
0
0
0
0
0
0
va
0
0
vb
0
0
0
As you can see, the right hand side is NOT identically zero.
So why did you get zero the second time? Linear Algebra 101.
Does a non-zero solution to the second problem exist? Yes, infinitely many of them, and no great way to choose which solution you might like.
So, any linear combination of the columns of
null(M2)
ans =
[ -1/R3, (A + 1)/(A*R3)]
[ 0, -(R2 + R3 + A*R2)/(A*R3*(2*R1 + Rg))]
[ 0, -(R2 + R3 + A*R2)/(A*R3*(2*R1 + Rg))]
[ 0, (R2 + R3 + A*R2)/(A*R3*(2*R1 + Rg))]
[ -1/R3, (A + 1)/(A*R3)]
[ 1/R3, 0]
[ -1/R3, (A + 1)/(A*R3)]
[ 1/R3, 0]
[ 1/R3, (R2 + R3 + A*R2)/(A*R3*(2*R1 + Rg))]
[ 1/R3, -(2*R1 + R2 + R3 + Rg + 2*A*R1 + A*R2 + A*Rg)/(A*R3*(2*R1 + Rg))]
[ (R2 + R3 + A*R2 + A*R3)/(A*R3), -((A + 1)*(R2 + R3 + A*R2))/(A^2*R3)]
[ (R2 + R3 + A*R2 + A*R3)/(A*R3), -(R1*(R2 + R3 + A*R2))/(A*R3*(2*R1 + Rg))]
[ -R2/R3, (R2 + A*R2)/(A*R3)]
[ 0, -(R1*(R2 + R3 + A*R2))/(A*R3*(2*R1 + Rg))]
[ 0, -(R2*Rg + R3*Rg + A*R2*Rg)/(A*R3*(2*R1 + Rg))]
[ 0, (R1*(R2 + R3 + A*R2))/(A*R3*(2*R1 + Rg))]
[ -1, (A + 1)/A]
[ R2/R3, 0]
[ 0, 1]
[ 1, 0]
[ (R2 + R3)/R3, 0]
[ (R2 + R3)/R3, -(R2 + R3 + A*R2)/(A*R3)]
[ (R2 + R3)/R3, -(R2 + R3 + A*R2)/(A*R3)]
[ (R2 + R3)/R3, -((R1 + Rg)*(R2 + R3 + A*R2))/(A*R3*(2*R1 + Rg))]
[ (R2 + R3)/R3, -(R1*(R2 + R3 + A*R2))/(A*R3*(2*R1 + Rg))]
[ (R2 + R3)/R3, 0]
[ 1, -1/A]
[ 1, 0]
[ 0, 1]
will yield a valid solution to your problem.
Again, linear algebra. It is a good class to take, if you will then be using linear algebra.

John D'Errico

### John D'Errico (view profile)

2019 年 4 月 5 日
Zero IS a completely valid solution to the matrix problem you posted online. Is there a way that I could know that you did not post the problem you wanted to solve? I'll try the MATLAB tarot cards next time.
Joaquin Sanchez

### Joaquin Sanchez (view profile)

2019 年 4 月 5 日
What I mean is that zero could not be a valid result for the problem I was trying to represent in MATLAB, and that was due to my wrong approach to the equations and I thought it was because of the "solve" function but I was wrong
John D'Errico

### John D'Errico (view profile)

2019 年 4 月 5 日
Yes. And that is why when you see that something strange happened, that you check your equations FIRST. Check your problem set up. Only when you exhaust the obvious do you go beyond that point. Assume immediately "Probable user error". Assume that the software gave you the correct answer to the problem it was supplied. The immediate conclusion should be to check the problem formulation. If you assume that solve did something strange, then you give up, you stop thinking.
This is why the very first thing I did was to look at your equations. I assumed probable user error. Looking at the result from solve, what did it imply about the problem? All zero implies a homogeneous problem is very likely the case.

サインイン to comment.