Solve multiple non-linear equations with vector variables

5 ビュー (過去 30 日間)
Yang Li
Yang Li 2022 年 1 月 7 日
コメント済み: Yang Li 2022 年 1 月 9 日
Hi all,
How can I solve multiple equations with vector variables? Say I have two vectors X and Y: X=[x1,x2], Y =[y1,y2], and two equations: X.^2+Y.^2=A, X.^2-Y.^2=B, where A=[20,5], and B =[12,3]. How can I solve this problem using "fsolve"?
In the real case, my equations are more complicated and I have 50,000 rows for vectors X and Y. Instead of looping each row and solve X(n)^2+Y(n)^2=A(n),X(n)^2-Y(n)^2=B(n), I wonder if there is a more effecient way. Thanks!
  8 件のコメント
Torsten
Torsten 2022 年 1 月 8 日
But it's not difficult to solve this 2-equation system for X and Y. I thought your equations were much harder.
And once you have solved for X and Y, you don't need to loop, but you can instantly insert the complete 50000 element vectors A,B,C and D to get back the 50000 element vectors X and Y.
Yang Li
Yang Li 2022 年 1 月 9 日
Hi Torsten, Matt gives detailed explanation of how to reduce the complicated equations to a linear system. I accepted his answer. Thank you very much for your help.

サインインしてコメントする。

採用された回答

Matt J
Matt J 2022 年 1 月 8 日
編集済み: Matt J 2022 年 1 月 8 日
But it's not difficult to solve this 2-equation system for X and Y. I thought your equations were much harder.
And in fact, the equations can be reduced to a 2x2 linear system. When D=0, the equations reduce to linear equations in X and Y, with a simple solution.
X=A./C;
Y=1-((A+B)./C);
When D is not 0, you can make the change of variables P=(1-X-Y) and Q=(1-D*X). The equations then become,
eqn1 = C/D*(1-Q) + C*D*(P^2/Q) ==A;
eqn2 = C*P/Q==B
The second equation can be used to simplify the second term in the first equation,
eqn1 = C/D*(1-Q) + D*B*P == A;
which is a linear eqaution and the second equation can also be rearranged as linear,
eqn2 = C*P-B*Q==0
Simplifying everything leads to the linear matrix equations
[ D*B -C/D;
C -B ]*[P;Q] = [ A-C/D; 0]
whos analytical (and vectorized) solution is,
d=C.^2./D-D*B.^2; %determinant
P = -B.*(A-C./D)./d;
Q = -C.*(A-C./D)./d;
X=(1-Q)./D;
Y=1-X-P;
  1 件のコメント
Yang Li
Yang Li 2022 年 1 月 9 日
This helps a lot. Thanks Matt!

サインインしてコメントする。

その他の回答 (1 件)

Matt J
Matt J 2022 年 1 月 7 日
A=[20,5]; B =[12,3];
XY0=ones(2); %initial guess
[XY,fval]=fsolve(@(XY) Equations(XY, A,B) , XY0);
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
X=XY(:,1), Y=XY(:,2),fval
X = 2×1
4.0000 2.0000
Y = 2×1
2.0000 1.0000
fval = 4×1
1.0e+-12 * 0.3730 0.3713 -0.3730 0.3713
function F=Equations(XY, A,B)
X=XY(:,1); Y=XY(:,2);
F=[X.^2+Y.^2-A(:); X.^2-Y.^2-B(:)];
end

カテゴリ

Help Center および File ExchangeSystems of Nonlinear Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by