Solve multiple non-linear equations with vector variables
8 次查看(过去 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!