Solve system of 3 variable equations

62 次查看(过去 30 天)
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.

请先登录,再进行评论。

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by