Solve multiple non-linear equations with vector variables
Mostra commenti meno recenti
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 Commenti
James Tursa
il 7 Gen 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
il 7 Gen 2022
Yang Li
il 7 Gen 2022
James Tursa
il 7 Gen 2022
Modificato: James Tursa
il 7 Gen 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
il 8 Gen 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
il 9 Gen 2022
Risposta accettata
Più risposte (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
Categorie
Scopri di più su Linear Algebra in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!