GAGANDEEP KAUR
GAGANDEEP KAUR

Reputation: 21

How should I use syms with solve command in MATLAB?

I am having trouble solving this set of simultaneous nonlinear equations to determine the unknown variables a,b,c,d,e.

It seems like my code enters in an infinite loop and never stops.

syms a b c d e;
K1=zeros(1,5);
K2=zeros(1,5);
K3=zeros(1,5);
K4=zeros(1,5);
K5=zeros(1,5);

for i=1:9
    x1=[0.5096 0.5092 0.5087 0.4852 0.4847 0.4834 0.4804 0.4805 0.4803];
    x2=[0.0963 0.0964 0.0965 0.1163 0.1161 0.1158 0.1275 0.1266 0.1253];
    x3=[0.3941 0.3944 0.3948 0.3985 0.3992 0.4008 0.3921 0.3929 0.3943];   
    
    T=[394.15 399.15 404.15 375.15 390.15 405.15 374.15 392.15 406.15]; 
    
    K1(i)=exp((-8.549)+(6692/T(i)))
    K2(i)=100
    K3(i)=exp((16.93565)+((1250)/T(i))+(-2.575*log(T(i))))
    K4(i)=exp((-936.28)+((40216.27)/T(i))+(151.983*(log(T(i))))+(-0.1675*(T(i))))
    K5(i)=exp((1044.78)+(-45171.42/T(i))+(-165.20*log(T(i)))+(0.1511*(T(i))))
    
    eqns=[((d*(c-e))/((x1(i)-a-b-c-d)^5)*(x2(i)-d-b-c))==K1(i),...
       ((a+b+c)*a)/((x1(i)-a-b-c-d)^2)==K2(i),...
       ((a+b+c)*(d+b))/((x1(i)-a-b-c-d)*(x2(i)-d-b-c-e))==K3(i),...
       ((a+b+c)*(c-e))/((x1(i)-a-b-c-d)^4*(x2(i)-d-b-c-e))==K4(i),...
       (((e)*(x1(i)-a-b-c-d)^3)/((c-e)*(x3(i)-e)))==K5(i)];

    S=solve(eqns,[a,b,c,d,e]);
    S.a(S.a<0)=[];
    S.b(S.b<0)=[];
    S.c(S.c<0)=[];
    S.d(S.d<0)=[];
    S.e(S.e<0)=[];
    S.a=double(S.a)
    S.b=double(S.b)
    S.c=double(S.c)
    S.d=double(S.d)
    S.e=double(S.e)
end

Upvotes: 1

Views: 113

Answers (1)

juju89
juju89

Reputation: 549

The issue is not the for loop but the line

S=solve(eqns,[a,b,c,d,e]);

which takes too long to solve the equations.

This could be due to:

  1. The system of equations being too complex and Matlab's symbolic toolbox cannot handle it.
  2. Your system of equations does not have an analytical solution, so you should first consider trying to solve the equations numerically using one of Matlab's ODE solvers, such as ode45.

Upvotes: 1

Related Questions