How to solve this multi equation using matlab program

2 views (last 30 days)
I have this ,
syms p1 p2 p3
pi =[p1 p2 p3];
and this equation,
y=[-2*p1 + 2*p2 , -1*p2 + 1*p3, 3/2*p1+3/2*p2-3*p3]
[A,Z]=equationsToMatrix(y,pi)
so i get matrix A,
matrix A must fulfill this equation to get pi unique, that is d
d=p1+p2+p3==1
[B,Y]=equationsToMatrix(d,pi)
after that, i dont know how to get pi value
if i calculate manually, i must get p1=3/19 ;p2=12/19 ;p3=4/19 ---> the sum of pi=1 that is the same as d
if i dont use d, the value of pi all equal to zero.
i already use this,
sol = lsqlin(double(A),double(Z),[],[],double(B),double(Y))
sum(sol)
i got this,
sol =
0.333333333333333
0.333333333333333
0.333333333333333
ans =
1
the sum of sol(that is pi) equal to 1, but the pi is not same as i calculate manually.

Accepted Answer

John D'Errico
John D'Errico on 9 Nov 2022
Edited: John D'Errico on 9 Nov 2022
It is a really, really, really bad idea to name a variable with the name pi.
Why? Because as soon as you do, when you actually want to use the NUMBER pi, as previously defined in MATLAB as
pi
ans = 3.1416
when you overwrite pi with your variable, now the number pi disapperars from view. DON'T DO THAT!
syms p1 p2 p3
p =[p1 p2 p3];
y=[-2*p1 + 2*p2 , -1*p2 + 1*p3, 3/2*p1+3/2*p2-3*p3]
y = 
[A,Z]=equationsToMatrix(y,p)
A = 
Z = 
So you have what is called a homogeneous linear system of equations. That is, the right hand side vector Z is all zero. If the matrix A is of full rank, then only one solution can possibly exist: the all zero solution.
rank(A)
ans = 2
Your matrix A has only rank 2 though, so a non-trivial solution exists. You find that by the use of null.
p = null(A)
p = 
Or you can normalize it, by dividing by 3 if you wish, as scaling the homogeneous solution does not change it from being a solution.
Now the question remains, is the solution you claim to be a solution, truly one? NO. You claim this is the solution:
p1=3/19 ;p2=12/19 ;p3=4/19
We can try it. For example:
pclaim = [3/19;12/19;4/19];
A*pclaim
ans = 
Which is not at all zero, as you claim. Instead, we see that
A'*pclaim
ans = 
So what you solved is in fact the vector such that x*A = 0. But what you passed to solve to lsqlin was the transpose of what you apparently wanted to solve. That is, if we transpose your problem to one that lsqlin can use, we will have a problem of the form A'*x=0. That transpose is crucial.
format rat
palt = null(A')
palt = 
palt/sum(palt)
ans = 
Which DOES do what you seem to want. Alternatively, you could have used lsqlin. Thus...
lsqlin(double(A)',double(Z),[],[],[1 1 1],1)
Minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
ans =
3/19 12/19 4/19

More Answers (1)

Torsten
Torsten on 9 Nov 2022
Edited: Torsten on 9 Nov 2022
According to your y-vector, you want to have
-2*p1 + 2*p2 = 0
-1*p2 + 1*p3 = 0
3/2*p1+3/2*p2-3*p3 = 0
Insert
p1 = p2 = p3 = 1/3
and your solution
p1=3/19 p2=12/19 p3=4/19
and check which one is correct.
syms p1 p2 p3
pi =[p1 p2 p3];
y=[-2*p1 + 2*p2==0 , -1*p2 + 1*p3==0, 3/2*p1+3/2*p2-3*p3==0,p1+p2+p3==1];
[A,Z]=equationsToMatrix(y,pi)
A = 
Z = 
sol = A\Z
sol = 

Community Treasure Hunt

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

Start Hunting!