How to work with cell arrays?

4 views (last 30 days)
masterm667
masterm667 on 16 Feb 2021
Edited: Walter Roberson on 17 Feb 2021
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 Comments
Walter Roberson
Walter Roberson on 16 Feb 2021
You assign to Srxi(8) twice but do not assign to Srxi(7)
Walter Roberson
Walter Roberson on 16 Feb 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.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 16 Feb 2021
Your code has
rI(1) = ((kfI(1)*(R2*T)^1.5)/((DENC^2)*(CH2^2.5)))*((CH2O*CCH4/(R2*T)^2)-(KeqI(1)*CCO*CH2^3));
where CH2 = p(6). But your your initial p(6) is 0, so the (CH2^2.5) is 0, so you are multiplying DENC^2 by 0, getting a result of 0. Which you then divide by, giving a NAN result.
That NAN result the poisons the rest of the calculations.
Code attached. You need to check the details.
  2 Comments
masterm667
masterm667 on 16 Feb 2021
Thank you for your advice and help Walter. I noticed that issues that you found and went and corrected them. A few were mistakes made during the editing process of my code from the previous post I had made.
As for the issue with CH2, I was provided that the initial condition was zero, but I will change it so that it would be a small value close to 0 to get a good enough estimation.
I do have a quick question about this portion of the code:
% Line 98 begins below
Cp{3} = @(T) zeros(size(T)); % Why can "zeros" be used?
for i = 1:n3
Cp{3} = @(T) (Cp{3}(T)+(4*R1/(1000*n3))+g(T,thetav3,i));
end
% Line 101 ends above
How are you able to use "zeros" despite this being a cell array. I thought that I had to use the "cell" function to preallocate my array.
Walter Roberson
Walter Roberson on 17 Feb 2021
Edited: Walter Roberson on 17 Feb 2021
Notice that the zeros() is inside the anonymous function. The anonymous function receives a numeric value, and returns zeros the same size as the numeric value. The function handle is then stored into a cell entry. You are not initializing a cell entity with zeros().
Your code had @(T) 0 at that point. @(T) 0 takes in a parameter of any size and ignores its content and emits a scalar 0. That is a problem for integral(), as integral() requires that the function returns the same size of output as its input.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming 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!