How can I create a script that solves a function for temperatures over a length?

12 vues (au cours des 30 derniers jours)
Jenna Broders
Jenna Broders le 19 Avr 2022
Commenté : Jenna Broders le 20 Avr 2022
Below is the function code, followed by the script and obtained error. I'm looking to create a script that generates a vector for 'Ts' and 'Tg' that contains different temperatures along a given z length (say 0 - 0.42m). This ideally would be kept in a function code as this will be built on to give concentration profiles using rate of rxn equations which depend on temperature values.
FUNCTION CODE:
function temp = temperature(z, T)
Ts = T(1);
Tg = T(2);
% temperature profiles
Ts1 = ((-3508440.83).*(z.^5))+((3221859.34).*(z.^4))-((1089760.82).*(z.^3))+((165361.13).*(z.^2))-((12015.96).*z)+(1179.21);
Ts2 = ((-1906981.2).*(z.^3))+((2073664.15).*(z.^2))-((754331.65).*(z))+(92096.47);
Tg1 = ((2796881.8).*(z.^5))-((2553240.18).*(z.^4))+((852324.51).*(z.^3))-((124780.34).*(z.^2))+((6092.92).*(z))+(889.33);
Tg2 = ((-1567143.11).*(z.^3))+((1751141.7).*(z.^2))-((653704.46).*(z))+(81812.04);
% Solid Temperature Loop
if z >=0 && z<=0.32
Ts = Ts1;
elseif z<=0.407
Ts = Ts2;
end
% Gas Temperature Loop
if z >=0 && z<=0.32
Tg = Tg1;
elseif z<=0.407
Tg = Tg2;
end
temp = [Ts, Tg];
end
SCRIPT
z0 = [0 0.415];
T0 = [0 0];
temp = feval(@temperature,z0,T0);
Ts = temp(1)
Tg = temp(2)
ERROR obtained:
Operands to the logical and (&&) and or (||) operators must be convertible to logical scalar values.
Error in temperature (line 13)
if z >=0 && z<=0.32
Error in Tempprofile (line 4)
temp = feval(@temperature,z0,T0);

Réponses (1)

Torsten
Torsten le 19 Avr 2022
Modifié(e) : Torsten le 19 Avr 2022
z is a vector.
If you use short-circuit AND (&&) , each expression in the if condition must evaluate to a scalar. This is not the case here. So using && will result in an error.
By the way:
What do you want to test with
if z>= 0 && z <=0.32
for z being a vector ?
  5 commentaires
Torsten
Torsten le 20 Avr 2022
Modifié(e) : Torsten le 20 Avr 2022
z0 = [0 0.415];
[Ts,Tg] = feval(@temperature,z0);
Ts
Tg
function [Ts,Tg] = temperature(z)
Ts = zeros(size(z));
Tg = zeros(size(z));
idx1 = z>=0 & z <=0.32;
idx2 = z>0.32;
z1 = z(idx1);
z2 = z(idx2);
Ts1 = ((-3508440.83).*(z1.^5))+((3221859.34).*(z1.^4))-((1089760.82).*(z1.^3))+((165361.13).*(z1.^2))-((12015.96).*z1)+(1179.21);
Ts2 = ((-1906981.2).*(z2.^3))+((2073664.15).*(z2.^2))-((754331.65).*(z2))+(92096.47);
Tg1 = ((2796881.8).*(z1.^5))-((2553240.18).*(z1.^4))+((852324.51).*(z1.^3))-((124780.34).*(z1.^2))+((6092.92).*(z1))+(889.33);
Tg2 = ((-1567143.11).*(z2.^3))+((1751141.7).*(z2.^2))-((653704.46).*(z2))+(81812.04);
Ts(idx1) = Ts1;
Ts(idx2) = Ts2;
Tg(idx1) = Tg1;
Tg(idx2) = Tg2;
end

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by