# how to solve two equations

3 visualizaciones (últimos 30 días)
Adham Ahmed el 17 de Feb. de 2023
Comentada: Torsten el 6 de Mzo. de 2023
Hello
This is part of a code and I want to solve this part
=========================================
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 comentariosMostrar NingunoOcultar Ninguno
Dyuman Joshi el 17 de Feb. de 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 el 23 de Feb. de 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

Iniciar sesión para comentar.

### Respuestas (2)

Sai el 20 de Feb. de 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 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Adham Ahmed el 23 de Feb. de 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

Iniciar sesión para comentar.

John D'Errico el 20 de Feb. de 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 comentariosMostrar 4 comentarios más antiguosOcultar 4 comentarios más antiguos
Adham Ahmed el 6 de Mzo. de 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 el 6 de Mzo. de 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

Iniciar sesión para comentar.

### Categorías

Más información sobre Linear Algebra en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by