Using Syms command with for loop
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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)
0 commentaires
Réponses (1)
Walter Roberson
le 8 Fév 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)))
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))))
eqn1 = ((b*3*b)./((a-b)*(x3)))==K5
eqn2 = (a*(a-b))./(((x1-d-a)).*(x2-d-2*c))==K4
eqn3 = (d*d)./((x1-d-a).*(x2-d-2*c))==K2
eqn4 = (c*c)./((x2-d-2*c))==K1
S = solve([eqn1, eqn2, eqn3, eqn4])
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)
2 commentaires
Walter Roberson
le 8 Fév 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.
Voir également
Catégories
En savoir plus sur Symbolic Math Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!