Solve multiple non-linear equations with vector variables
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Torsten
le 8 Jan 2022
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.
Réponse acceptée
Matt J
le 8 Jan 2022
Modifié(e) : Matt J
le 8 Jan 2022
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;
Plus de réponses (1)
Matt J
le 7 Jan 2022
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 commentaires
Voir également
Catégories
En savoir plus sur Linear Algebra dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!