how to solve two equations
3 次查看(过去 30 天)
显示 更早的评论
Hello
This is part of a code and I want to solve this part
thanks in advance
=========================================
clear
syms U10 U20
p=0
f =[2*U20 + sin(U20 - sin(p)) - 2*sin(p);2*U10 + sin(U10 - cos(p)) - 2*cos(p)]
s=subs(f,U10,x(1))
s1=subs(s,U20,x(2))
fun=@(x)[sin(x(2))+2*x(2); sin(x(1)-1)+2*(x(1)-1)];
fun=@(x)f;
[x,fval]=fsolve(fun,[0;0]);
U10=x(1)
U20=x(2)
2 个评论
Dyuman Joshi
2023-2-17
It's not clear as to what you are trying to do. I am confused as to why you are using syms variable and solving with fsolve?
What is the purpose of subsitution? And you are over-writing fun
fun=@(x)[sin(x(2))+2*x(2); sin(x(1)-1)+2*(x(1)-1)];
fun=@(x)f;
回答(2 个)
Sai
2023-2-20
Hi,
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1301060/image.png)
With Regards,
G. Saikumar
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);
fplot(fun)
yline(0)
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.
6 个评论
Torsten
2023-3-6
syms U10 U20
Afk=[2*U10 + sin(U10 - 1) - 2,2*U20 + sin(U20)]
[U10 U20] = vpasolve(Afk)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Linear Algebra 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!