Using Syms command with for loop

1 view (last 30 days)
GAGANDEEP KAUR
GAGANDEEP KAUR on 8 Feb 2021
Commented: GAGANDEEP KAUR on 8 Feb 2021
I need to solve some equations using syms command over the length of x1. On solving it even as such(without loop) it is showing error as given below
Error in syms (line 201)
toDefine = sym(zeros(1, 0));
Error in Speciation(line 1)
syms a b c d
If I could please get some suggestion for this error and applying for loop in code?
syms a b c d
x1= [0.5096 0.5092 0.5087] ;
x2= [0.0963 0.0964 0.0965] ;
x3 = [.3941 0.3944 0.3948] ;
T= [394.15 399.15 404.15] ;
K1=exp((-228.838)+((12587.48)/T)+(40.68593*(log(T)))+(-0.09838*(T)))
K2=exp((-2.326+(-1233.57/T)))
K4=exp((-936.28)+((40216.27)/T)+(151.983*(log(T)))+(-0.1675*(T)))
K5=exp((1044.78)+(-45171.42/T)+(-165.20*log(T)+(0.1511*(T))))
S=solve(((b*3*b)/((a-b)*(x3)))==K5,(a*(a-b))/(((x1-d-a))*(x2-d-2*c))==K4,(d*d)/((x1-d-a)*(x2-d-2*c))==K2,(c*c)/((x2-d-2*c))==K1)
S.a=double(S.a);
S.b=double(S.b);
S.c=double(S.c);
S.d=double(S.d);
S.a(S.a<0)=[];
S.b(S.b<0)=[];
S.c(S.c<0)=[];
S.d(S.d<0)=[];
m = min(S.a)
n=min(S.b)
o=min(S.c)
p=min(S.d)

Answers (1)

Walter Roberson
Walter Roberson on 8 Feb 2021
syms a b c d
x1= [0.5096 0.5092 0.5087] ;
x2= [0.0963 0.0964 0.0965] ;
x3 = [.3941 0.3944 0.3948] ;
T= [394.15 399.15 404.15] ;
K1=exp((-228.838)+((12587.48)./T)+(40.68593*(log(T)))+(-0.09838*(T)))
K1 = 1×3
1.7903 1.2255 0.8418
K2=exp((-2.326+(-1233.57./T)))
K2 = 1×3
0.0043 0.0044 0.0046
K4=exp((-936.28)+((40216.27)./T)+(151.983*(log(T)))+(-0.1675*(T)))
K4 = 1×3
3.2760 2.6828 2.2141
K5=exp((1044.78)+(-45171.42./T)+(-165.20*log(T)+(0.1511*(T))))
K5 = 1×3
10.7550 11.9893 13.2375
eqn1 = ((b*3*b)./((a-b)*(x3)))==K5
eqn1 = 
eqn2 = (a*(a-b))./(((x1-d-a)).*(x2-d-2*c))==K4
eqn2 = 
eqn3 = (d*d)./((x1-d-a).*(x2-d-2*c))==K2
eqn3 = 
eqn4 = (c*c)./((x2-d-2*c))==K1
eqn4 = 
S = solve([eqn1, eqn2, eqn3, eqn4])
S = struct with fields:
a: [0×1 sym] b: [0×1 sym] c: [0×1 sym] d: [0×1 sym]
S.a=double(S.a);
S.b=double(S.b);
S.c=double(S.c);
S.d=double(S.d);
S.a(S.a<0)=[];
S.b(S.b<0)=[];
S.c(S.c<0)=[];
S.d(S.d<0)=[];
m = min(S.a)
m = 0×1 empty double column vector
n=min(S.b)
n = 0×1 empty double column vector
o=min(S.c)
o = 0×1 empty double column vector
p=min(S.d)
p = 0×1 empty double column vector
  2 Comments
Walter Roberson
Walter Roberson on 8 Feb 2021
Notice that your x1, x2, x3 are all vectors of length 3, and because your T is length 3, your K1, K2, and K3, K4 and K5 come out as vectors of length 3 as well. What you have written as a single equation involving K5 is therefore a system of 3 equations; likewise what you wrote as a single equation involving K4 is really a system of 3 equations. Each of your four equations that look like single equations are instead systems of 3 equations. Therefore you are asking to solve() a system of 4*3 = 12 equations in 4 variables.
Note that when you ask to solve more equations than there are variables, MATLAB tries to find values of variables that solve all of the equations at the same time. MATLAB will not look for an a, b, c, d that solves the first out of each group of 3, and then look for an a, b, c, d that solves the second group of 3 independently, and so on. If you want to do that kind of independent solving, you either need to use different variables (e.g., a1 a2 a3, b1 b2 b3) or you need to loop doing the solve() with only the required information. Looping is more efficient: it saves MATLAB trying to figure out what the possible connections there might be between variables you might intend to be independent.
GAGANDEEP KAUR
GAGANDEEP KAUR on 8 Feb 2021
Thank you for this response
Actually, I have tried it with single enteries of x1, x2, x3 and T. From that I was getting that error.
I just gave these vectors(here) to get the idea that how loop will be written for this code as I was having doubt in generating matrix using syms and ultimately what further changes has to make in commands(to display all the desired values as some constraints are provided)

Sign in to comment.

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!