Solve system of 3 variable equations

조회 수: 155 (최근 30일)
Abdullah Azzam
Abdullah Azzam 2019년 1월 21일
편집: Stephan 2019년 1월 21일
Hi guys and thanks in advance. I am working on matlab code to solve me a system of 3 variables (a, b and c) and print them out. Here is my code:
X1=input('X1');
X2=input('X2');
X3=input('X3');
syms a b c
eq1= a+b;
eq2=a+b*exp(-105*c);
eq3=a+b*exp(-180*c);
eqns=[eq1==X1, eq2==X2, eq3==X3];
S = solve(eqns, [a b c])
S.a
S.b
S.c
However, when I run to check it the solver keep running forever. I also tried to write the full equations like this
eqns=[a+b==X1, a+b*exp(-105*c)==X2, a+b*exp(-180*c)==X3];
But still didn't work either.
I appreciate if someone can tell me where my mistake is and help correct it.
Again Thanks in advance.
  댓글 수: 3
Torsten
Torsten 2019년 1월 21일
편집: Torsten 2019년 1월 21일
An analytical solution for a, b and c does not exist. You will have to specify values for X1, X2 and X3 to get a numerical solution.
Abdullah Azzam
Abdullah Azzam 2019년 1월 21일
X1 X2 and X3 are user inputs based on their value the program find the appropriate value for a b and c

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

답변 (1개)

Stephan
Stephan 2019년 1월 21일
Hi,
since you want to calculate values, follow Torstens suggestion and use fsolve to solve your nonlinear system:
X1 = input('X1');
X2 = input('X2');
X3 = input('X3');
options = optimoptions('fsolve','display','off');
x = fsolve(@(x)eqs(x,X1,X2,X3),ones(1,3),options);
fprintf('a = %.5f\nb = %.5f\nc = %.5f',x(1),x(2),x(3));
function F = eqs(x,X1,X2,X3)
a=x(1);
b=x(2);
c=x(3);
F(1)= a+b-X1;
F(2)= a+b.*exp(-105.*c)-X2;
F(3)=a+b.*exp(-180.*c)-X3;
end
Best regards
Stephan
  댓글 수: 2
madhan ravi
madhan ravi 2019년 1월 21일
It gives No solution found. , what values did you give to X1,X2 and X3 Stephen?
Stephan
Stephan 2019년 1월 21일
편집: Stephan 2019년 1월 21일
I think for this system there are a lot of combinations that will not have a solution. For X1=3, X2=X3=1 it works for example.

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

카테고리

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

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by