Why does my function keep returning zero?

In the following code, my objective is to plug different values of T and alpha into the functon and calculate y for two situations: when alpha < = 0.3 and when alpha > 0.3.
However, when I call the function in the command window, I keep getting y = 0. In fact, for whatever value of T and alpha I choose (in the context of the problem I'm trying to solve, T = 350 and alpha = 0.3), I get y = 0. If I remove y from the code and try to calculate just K1, K2, K3 by plugging different values of T into the function, I still get 0. What is wrong with the code?
function [y] = cure(T, alpha)
A1 = 2.101*10^8;
A2 = -2.014*10^9;
A3 = 1.96*10^5;
E1 = 8.07*10^4;
E2 = 7.78*10^4;
E3 = 5.66*10^4;
B = 0.47;
R = 8.31; %Universal gas constant
K1 = A1*exp(-(E1/R*T));
K2 = A2*exp(-(E2/R*T));
K3 = A3*exp(-(E3/R*T));
if alpha <= 0.3
y = (K1 + K2*alpha)*(1 - alpha)*(B - alpha);
elseif alpha < 0.3
y = K3(1 - alpha);
end
end
Here is how I tried to execute the function in the Command window,
[y] = cure(350, 0.3)

3 comentarios

Shubham Gupta
Shubham Gupta el 3 de Oct. de 2019
Editada: Shubham Gupta el 3 de Oct. de 2019
Do you want to write
or ?
if answer to above question is 1st eqaution then replace
K1 = A1*exp(-(E1/R*T));
by
K1 = A1*exp(-(E1/(R*T)));
ans subsequently for K2 & K3 too
Prithvi Shams
Prithvi Shams el 3 de Oct. de 2019
Silly mistake on my part! Thanks a lot, solved the problem.
Rik
Rik el 3 de Oct. de 2019
Your conditionals almost overlap entirely.
In general it is a good idea to have an else statement with an error if you don't expect the else to ever be triggered. (same goes for switch and otherwise)

Iniciar sesión para comentar.

 Respuesta aceptada

Shubham Gupta
Shubham Gupta el 3 de Oct. de 2019
Replace
K1 = A1*exp(-(E1/R*T));
by
K1 = A1*exp(-(E1/(R*T)));
ans subsequently for K2 & K3 too.

Más respuestas (0)

Categorías

Productos

Versión

R2017b

Etiquetas

Preguntada:

el 3 de Oct. de 2019

Comentada:

Rik
el 3 de Oct. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by