Solve multiple non-linear equations with vector variables
이전 댓글 표시
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
James Tursa
2022년 1월 7일
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
2022년 1월 7일
Yang Li
2022년 1월 7일
James Tursa
2022년 1월 7일
편집: James Tursa
2022년 1월 7일
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
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.
Yang Li
2022년 1월 9일
채택된 답변
추가 답변 (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
카테고리
도움말 센터 및 File Exchange에서 Linear Algebra에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!