how to solve two equations

조회 수: 3 (최근 30일)
Adham Ahmed
Adham Ahmed 2023년 2월 17일
댓글: Torsten 2023년 3월 6일
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
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;
Adham Ahmed
Adham Ahmed 2023년 2월 23일
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

댓글을 달려면 로그인하십시오.

답변 (2개)

Sai
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
Please refer to the fsolve documentation for future reference.
With Regards,
G. Saikumar
MathWorks Technical Support
  댓글 수: 1
Adham Ahmed
Adham Ahmed 2023년 2월 23일
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

댓글을 달려면 로그인하십시오.


John D'Errico
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
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
Adham Ahmed
Adham Ahmed 2023년 3월 6일
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 2023년 3월 6일
syms U10 U20
Afk=[2*U10 + sin(U10 - 1) - 2,2*U20 + sin(U20)]
Afk = 
[U10 U20] = vpasolve(Afk)
U10 = 
1.0
U20 = 
0

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by