How to solve this multi equation using matlab program
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
DoinK
el 9 de Nov. de 2022
Editada: John D'Errico
el 9 de Nov. de 2022
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.
0 comentarios
Respuesta aceptada
John D'Errico
el 9 de Nov. de 2022
Editada: John D'Errico
el 9 de Nov. de 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
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]
[A,Z]=equationsToMatrix(y,p)
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)
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)
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
Which is not at all zero, as you claim. Instead, we see that
A'*pclaim
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/sum(palt)
Which DOES do what you seem to want. Alternatively, you could have used lsqlin. Thus...
lsqlin(double(A)',double(Z),[],[],[1 1 1],1)
0 comentarios
Más respuestas (1)
Torsten
el 9 de Nov. de 2022
Editada: Torsten
el 9 de Nov. de 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)
sol = A\Z
0 comentarios
Ver también
Categorías
Más información sobre Linear Least Squares en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!