how to solve two equations
3 ビュー (過去 30 日間)
Adham Ahmed 2023 年 2 月 17 日
This is part of a code and I want to solve this part
thanks in advance
syms U10 U20
f =[2*U20 + sin(U20 - sin(p)) - 2*sin(p);2*U10 + sin(U10 - cos(p)) - 2*cos(p)]
回答 (2 件)
Sai 2023 年 2 月 20 日
I understand that you are trying to solve two nonlinear equations depending on two variables.
As the equations are not clear in the data provided, let me simplify and finalize the equations to be solved.
First equation: 2*x(1) + sin(x1)
Second equation: 2*x(2) + sin(x(2)-1) - 2
Now the equations are solved using fsolve command as shown.
Following code is placed in eq_solve.m file
function f = eq_solve(x)
f(1) = 2*x(1) + sin(x(1));
f(2) = 2*x(2) + sin(x(2)-1) - 2;
Following code is placed in eq_solve_test.m file
fun = @eq_solve;
x0 = [0;0];
x = fsolve(fun,x0)
Now the eq_solve_test.m file has been run to get the values
Please refer to the fsolve documentation for future reference.
MathWorks Technical Support
John D'Errico 2023 年 2 月 20 日
Note that the two equations are fully uncoupled. This means U20 appeared only in one equation. It has absolutely no impact on the other equation, and U10 has no impact on equation 1. Do you see that?
syms p U20 U10
2*U20 + sin(U20 - sin(p)) - 2*sin(p) == 0
2*U10 + sin(U10 - cos(p)) - 2*cos(p) == 0
the value of p does have an impact on these equations, but only in a very minor way. In fact, once you solve one of the equations, you could compute the solution to the other equation.
In this case, of course, with p==0, the problem becomes even simpler. The first equation reduces to
fun = @(U20) 2*U20 + sin(U20);
That problem provably has a solution only at U20==0, and the solution will be unique for real values of U20. We don't even need to use a tool like solve or fzero or fsolve to solve the problem.
Similarly, when p==0, the second equation reduces to
2*(U10-1) + sin(U10 - 1) == 0
My point is, this equation is identically the same as the first equation, but with a transformation appied
U20 == U10 -1
So if we have U20==0 as a solution to the first equation, then trivially we know that U10==1.
If p weret o take on some other values, then the problem is no more difficult, since your system of two equations is not only an uncoupled system, but once you solve the problem once, you can solve the second problem directly. There are no rootfinders ever needed in the end.
Find more on Surrogate Optimization 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!