Why do I receive sym/subsasgn error?
2 次查看(过去 30 天)
显示 更早的评论
Faridatul Ain Binti Mohd Rosdan
2021-11-8
评论: Faridatul Ain Binti Mohd Rosdan
2021-11-14
The code that I use:
clc;
clear;
close all;
%parameters
R = 8.314;
Tp = 298.15;
Nl = 18.015;
A = 18.3036;
B = 3816.44;
C = -46.13;
Tps = 298.15;
Dp = 0.00152;
rp = Dp/2;
vp = 0.75;
cpp = 4009;
rhod = 1052;
rhog = 0.9995;
ug = 2.087*10^-5;
Dv = 2.931*10^-5;
Cvi = 0.0196;
kg = 0.03;
Tg = 343.15;
hfg = 2.33*10^6;
rhol = 997;
Dls = 1.5*10^-10;
%Algebra eqns
psatv =exp (A-B/(Tp+C))*133.322;
Cvs = (psatv*Nl)/(1000*R*Tp);
Re = (Dp*vp*rhog)/ug;
Sc = ug/(rhog*Dv);
Pr = cpp*ug/kg;
kc = (2+0.6*Re^0.5*Sc^1/3)*Dv/Dp;
alpha = (2+0.6*Re^0.5*Pr^1/3)*kg/Dp;
mp = rhod*(4*pi*rp^3)/3;
syms ml(t) rp(t) Tp(t)
ode1 = diff(ml,t) == -4*pi*(rp)^2*kc*(Cvs-Cvi);
ode2 = diff(rp,t) == -4*pi*(rp)^2*kc*(Cvs-Cvi)./(4*pi*rhol*(rp)^2);
ode3 = diff (Tp,t) == -4*pi*(rp)^2*alpha*(t)*(Tp-Tg)-hfg*(-4*pi*(rp)^2*kc*(Cvs-Cvi))/(mp*cpp);
odes = [ode1 ode2 ode3];
S = dsolve(odes);
mlSol(t) = S.ml;
rpSol(t) = S.rp;
TpSol(t) = S.Tp;
[mlSol(t), rpSol(t), TpSol(t)] = dsolve(odes);
% time discretization
dt = 0.01;
t0 = 0;
tf = 100;
t = t0:dt:tf;
tspan=[t0 tf];
%Initial conditions
cond1 = ml(0) == 1.93*10^-6;
cond2 = rp(0) == 0.00076;
cond3 = Tp(0) == 298.15;
conds = [cond1 cond2 cond3];
[mlSol(t), rpSol(t), TpSol(t)] = dsolve(odes, conds);
fplot(mlSol, 'r', 'linewidth', 2)
title('Droplet mass versus Time')
xlabel('Time,t')
ylabel('Mass, mg')
grid on
fplot(rpSol, 'g', 'linewidth', 2)
title('Droplet size versus time')
xlabel('Time,t')
ylabel('Droplet size, mm')
grid on
fplot(TpSol, 'b', 'linewidth', 2)
title('Temperature versus time')
xlabel('time,t')
ylabel('Temperature, K')
grid on
0 个评论
采纳的回答
Navya Singam
2021-11-12
Hi,
The error is because of the following lines,
t = t0:dt:tf; %%from time discretization section
[mlSol(t), rpSol(t), TpSol(t)] = dsolve(odes, conds); %%from initial conditions section
"dsolve" returns the symbolic variables as the output. As the time discretization section is written above the initial conditions section, 't' is no longer a symbolic variable, it is now a vector of double. So, mlSol(t) is trying to index the mlSol symbolic variable with the vector 't' and assign the output value, which is not possible as the values of t are 0,0.01,0.02,...etc. It is an invalid indexing.
It can be solved by, moving the time discretization section next to the initial condtions section, so 't' would be a symbolic variable during the execution of the initial conditions section and it would assign the results to mlSol(t) (i.e to a symbolic variable). Refer to this documentation for more information on the output arguments of the "dsolve" function.
%Initial conditions
cond1 = ml(0) == 1.93*10^-6;
cond2 = rp(0) == 0.00076;
cond3 = Tp(0) == 298.15;
conds = [cond1 cond2 cond3];
[mlSol(t), rpSol(t), TpSol(t)] = dsolve(odes, conds);
% time discretization
dt = 0.01;
t0 = 0;
tf = 100;
t = t0:dt:tf;
tspan=[t0 tf];
fplot(mlSol, 'r', 'linewidth', 2)
title('Droplet mass versus Time')
xlabel('Time,t')
ylabel('Mass, mg')
grid on
fplot(rpSol, 'g', 'linewidth', 2)
title('Droplet size versus time')
xlabel('Time,t')
ylabel('Droplet size, mm')
grid on
fplot(TpSol, 'b', 'linewidth', 2)
title('Temperature versus time')
xlabel('time,t')
ylabel('Temperature, K')
grid on
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Equation Solving 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!