How to work with cell arrays?
Afficher commentaires plus anciens
Hello Matlab forums. I am in a pickle about how to work with cell arrays. For my code, it is telling me that I cannot work with "nonscalar arrays of function handles" and to use "cell arrays" instead. I have never worked with cell arrays and I made an attempt to change my "Cp" functions (Lines 80 to 108) to cell arrays so that they can be integrated (Lines 125 and 127). However, doing so would prevent me from utilizing ode45 (or should I stick with another ode solver) and would produce another error message on Line 277 about "operators not being able to be used with function handles", which makes sense to me, but I just don't know how to work around the cell arrays. Attached is my script file (without any cell arrays, because I want to go back to the beginning of where I was without the cell arrays), but I want to highlight the areas that I am being challenged on below.
Any advice and suggestions are much appreciated. Thank you for your time.
% Line 78 begins below
f = @(T,A) (A(1)+(A(2).*T)+(A(3).*T.^2)+(A(4).*T.^3)+(A(5)./T.^2))./1000;
g = @(T,thetav,i) (R1.*((thetav(i)./T).^2).*(exp(thetav(i)./T)./(exp(thetav(i)./T)-1).^2))./1000; % Statistical Cp
Cp(1) = @(T) f(T,ABCDE1); Cp(2) = @(T) f(T,ABCDE2); % Change Cp formulas to cell arrays?
Cp(3) = @(T) (0);
for i = 1:n3
Cp(3) = @(T) (Cp(3)+(4*R1/(1000*n3))+g(T,thetav3,i)); % Change Cp formulas to cell arrays?
end
Cp(4) = @(T) f(T,ABCDE4); Cp(5) = @(T) f(T,ABCDE5); % Change Cp formulas to cell arrays?
Cp(6) = @(T) (0);
for i = 1:n6
Cp(6) = @(T) (Cp(6)+(3.5*R1/(1000*n6))+g(T,thetav6,i)); % Change Cp formulas to cell arrays?
end
Cp(7) = @(T) (0);
for i = 1:n7
Cp(7) = @(T) (Cp(7)+(3.5*R1/(1000*n7))+g(T,thetav7,i)); % Change Cp formulas to cell arrays?
end
Cp(8) = @(T) f(T,ABCDE8); % Change Cp formulas to cell arrays?
Cp(9) = @(T) (0);
for i = 1:n9
Cp(9) = @(T) (Cp(9)+(3.5*R1/(1000*n9))+g(T,thetav9,i)); % Change Cp formulas to cell arrays?
end
Cp(10) = @(T) (0);
for i = 1:n10
Cp(10) = @(T) (Cp(10)+(4*R1/(1000*n10))+g(T,thetav10,i)); % Change Cp formulas to cell arrays?
end
Cp(11) = @(T) f(T,ABCDE11); % Change Cp formulas to cell arrays?
% Specific heat capacity for entropy, Cps = Cp/T
Cps = zeros(11);
for i = 1:11
Cps(i) = @(T) (1000.*Cp(i)./T); % Change Cp formulas to cell arrays?
end
% Line 108 ends above
% Line 123 begins below
Hf = zeros(11); Hfa = zeros(11); Sf = zeros(11); Sfa = zeros(11);
for i = 1:11
Hf(i) = (Hfo(i)+(integral(Cp(i),298.15,T))); % Enthalpies of formation at temperature, T in kJ/mol INTEGRAL
Hfa(i) = (Hf(i)+BE(i)); % Enthalpies of formation of adsorbed species at temperature, T in kJ/mol
Sf(i) = (Sfo(i)+(integral(Cps(i),298.15,T))); % Entropies of formation at temperature, T in J/(mol*K) INTEGRAL
Sfa(i) = 0.5*(Sf(i)-St(i)); % Entropies of formation of adsorbed species, T in J/(mol*K)
end
% Line 129 ends above
% Line 271 begins below
SumCp = Vr.*((CCO.*Cp(6))+(CCO2.*Cp(8))+(CCH4.*Cp(3))+(CH2O.*Cp(10))+(CH2.*Cp(9)));
% Sum of Heat Transfer of all species, CpT
CpT = F.*(T-Tin).*(CCOin+CCO2in+CCH4in+CH2Oin+CH2in);
% Heat Generation/Consumption from Reactions I, II, and III, Qrx (W)
Qrx = Vr.*((-HrxI(1).*-rI(1))+(-HrxI(2).*-rI(2))+(-HrxI(3).*-rI(3)));
% Temperature Rate, dTdt
dTdt = (Q-CpT-Qrx)./SumCp; % Equation 6 (Cannot use "./" operator when SumCp = Function Handle)
r = [dTdt; dCCOdt; dCCO2dt; dCCH4dt; dCH2Odt; dCH2dt];
end
% Line 280 ends above
3 commentaires
Stephen23
le 16 Fév 2021
Original question and context:
Walter Roberson
le 16 Fév 2021
You assign to Srxi(8) twice but do not assign to Srxi(7)
Walter Roberson
le 16 Fév 2021
Please recheck
% Calculated Forward Activation Energy for Reactions 1-2 and 9-10, Eafi (kJ/mol)
You assign
Eari(4) = Eafi(4)-abs(Hrxi(4));
but that is in the middle of a stretch that is assigning to Eafi() in terms of Eari() so it is not clear if that is deliberate or a mistake. If it is deliberate, move it so it is not mixed in to the assignments to Eafi.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Data Type Identification dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!