Solve multiple non-linear equations with vector variables
Show older comments
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 Comments
James Tursa
on 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
on 7 Jan 2022
Yang Li
on 7 Jan 2022
James Tursa
on 7 Jan 2022
Edited: James Tursa
on 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
on 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
on 9 Jan 2022
Accepted Answer
More Answers (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
Categories
Find more on Linear Algebra in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!