Solve multiple non-linear equations with vector variables
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
James Tursa
le 7 Jan 2022
You have all the A and B values and you are trying to find the X and Y values? If so, why not just use a little algebra to solve for them directly?
Yang Li
le 7 Jan 2022
Yang Li
le 7 Jan 2022
James Tursa
le 7 Jan 2022
Modifié(e) : James Tursa
le 7 Jan 2022
I still don't get it. If you have these equations:
x.^2 + y.^2 = A
x.^2 - y.^2 = B
Just add them to get
2*x.^2 = A+B
and solve for x.^2 and y.^2
x.^2 = (A+B)/2
y.^2 = x.^2 - B = (A-B)/2
From that you can sqrt( ) to get x and y, and maybe pick signs as appropriate to your problem.
What am I missing here? If this isn't your actual problem with actual equations, then please post your actual problem with the actual equations.
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.
Yang Li
le 9 Jan 2022
Réponse acceptée
Plus de réponses (1)
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
Catégories
En savoir plus sur Linear Algebra dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!