フィルターのクリア

How to solve this multi equation using matlab program

1 回表示 (過去 30 日間)
DoinK
DoinK 2022 年 11 月 9 日
編集済み: John D'Errico 2022 年 11 月 9 日
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.

採用された回答

John D'Errico
John D'Errico 2022 年 11 月 9 日
編集済み: John D'Errico 2022 年 11 月 9 日
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

その他の回答 (1 件)

Torsten
Torsten 2022 年 11 月 9 日
編集済み: Torsten 2022 年 11 月 9 日
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 = 

カテゴリ

Help Center および File ExchangeLinear Least Squares についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by