how to solve two equations

5 views (last 30 days)
Adham Ahmed
Adham Ahmed on 17 Feb 2023
Commented: Torsten on 6 Mar 2023
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 Comments
Dyuman Joshi
Dyuman Joshi on 17 Feb 2023
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;
Adham Ahmed
Adham Ahmed on 23 Feb 2023
my friend I want solve these equations with the same variables U10 and U20 because they are part of other code
2*U20 + sin(U20)
2*U10 + sin(U10 - 1) - 2

Sign in to comment.

Answers (2)

Sai
Sai on 20 Feb 2023
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
Please refer to the fsolve documentation for future reference.
With Regards,
G. Saikumar
MathWorks Technical Support
  1 Comment
Adham Ahmed
Adham Ahmed on 23 Feb 2023
my friend I want solve these equations with the same variables U10 and U20 because they are part of other code
2*U20 + sin(U20)
2*U10 + sin(U10 - 1) - 2

Sign in to comment.


John D'Errico
John D'Errico on 20 Feb 2023
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
ans = 
2*U10 + sin(U10 - cos(p)) - 2*cos(p) == 0
ans = 
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 Comments
Adham Ahmed
Adham Ahmed on 6 Mar 2023
Thank you
can you help me to run this code ?
syms U10 U20
Afk=[f1,f2]
Afk=[2*U10 + sin(U10 - 1) - 2,2*U20 + sin(U20)]
[U10 U20] = vpasolve([f1==0,f2==0],[U10 U20])
Torsten
Torsten on 6 Mar 2023
syms U10 U20
Afk=[2*U10 + sin(U10 - 1) - 2,2*U20 + sin(U20)]
Afk = 
[U10 U20] = vpasolve(Afk)
U10 = 
1.0
U20 = 
0

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!