Solve multiple non-linear equations with vector variables
5 ビュー (過去 30 日間)
古いコメントを表示
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
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.
採用された回答
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 件)
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);
X=XY(:,1), Y=XY(:,2),fval
function F=Equations(XY, A,B)
X=XY(:,1); Y=XY(:,2);
F=[X.^2+Y.^2-A(:); X.^2-Y.^2-B(:)];
end
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Systems of Nonlinear Equations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!