Hi everyone, I am trying to solve a complex ode equation with if statement meanwhile some expression is a function of a and T. However Matlab doesn't seem to like my expression in the if statement. Can I know what is wrong with my expression? Thank you for everyone's help!
T_g =@(a,T) ((lambda_Tg_grind.*a(T_g1_grind - T_g0_grind))./(1 - (1 - lambda_Tg_grind).*a)); + T_g0_grind;
delta_Tg = @(a,T) T - T_g;
% Find Activation energy for k1 and k2
mER1_g = @(a,T) -((T_g.^2).*(c1./c2)).*8.314;
mER2_g = @(a,T) -((c1.*c2.*(T_g + delta_Tg).^2)./(c2 + delta_Tg).^2).*8.314;
k1_grindling = @(a,T) A1_grind*exp(mER1_f/T); % are they different?
k2_grindling = @(a,T) A2_grind*exp(mER2_f/T);
% T_g conditions
Condition = @(a,T) T_g + delta_Tg;
if isoTemp > Condition % THIS LINE
k_2d = @(a,T) k_2d.*exp(mER2_g.*((1./T) - (1./(T_g - delta_tg))));
elseif T_g <= isoTemp && isoTemp <= Condition
k_2d = @(a,T) k_2d_tg.*exp((c1.*(T - T_g))./(c2 + (T - T_g)));
elseif isoTemp < T_g
k_2d = @(a,T) k_2d_tg.*exp(mER1_g.*(1./T - 1./T_g));
end
To explain a bit about my code and problem:
a is waiting to be solved in later lines by ode15i where T is the input. And the error that I have is: (highlighted in above %THIS LINE)
Undefined operator '>' for input arguments of type 'function_handle'.
Error in BlackBox (line 323)
if isoTemp > Condition

 Réponse acceptée

OCDER
OCDER le 17 Juil 2018
Modifié(e) : OCDER le 17 Juil 2018
You cannot use an inequality for a function handle, as it makes no sense.
Condition = @(a,T) T_g + delta_Tg;
if isoTemp > Condition % THIS LINE MAKES NO SENSE. isoTemp > @(a, T) T_g + delta_Tg ????
end
Perhaps you mean to get the value returns from a function handle?
if isoTemp > Condition(1, 10) % OK. Value of the function handle at a = 1, T = 10.
end
But careful, your T_g and delta_Tg are also function handles, so you would need to do something like :
Condition = @(a,T) T_g(a, T) + delta_Tg(a, T);

8 commentaires

Ho Nam Ernest Yim
Ho Nam Ernest Yim le 17 Juil 2018
Hi OCDER, thank you for your answer! I do understand what you mean. However, for my situation: a is waiting to be solved in later lines (ode15i), which means its still an unknown. So I guess my logic in the if statement is wrong now.
Guillaume
Guillaume le 17 Juil 2018
Unfortunately, computers are yet to be able to predict the future, so yes, your logic is wrong.
Moreover, as OCDER said comparing a value to a function handle makes no sense. It's like comparing a particular value to the square root function. It doesn't mean anything.
Perhaps you should explain in more details what you were trying to achieve by that comparison. Are you trying to solve different differential equations depending on the temperature? If so, this part goes into the function that you pass to the ODE solver.
Try this. That way, as your function is searching for a and T, it'll still pass it through and evaluate it. At least now, it's clear that you want the value of Condition at a = a, T = T, instead of a function handle.
if isoTemp > Condition(a, T)
end
Ho Nam Ernest Yim
Ho Nam Ernest Yim le 17 Juil 2018
Hi Guilaume! YES you are semi-correct! I tried to cut out the critical part of issues from my code, just for an easier understanding. But I can describe more in here actually.
Basically, I am trying to solve my ode equation (one equation) under isothermal test with a for loop (let's say 5 temperature) and I have all those parameters (only a is unknown). However, before passing it to the ode15i solver, I have these if statement which is my conditions.
for isT=2:6
db{isT-1}.isoTemp = 70 + (isT-1)*5 + 273;
isoTemp = db{isT-1}.isoTemp;
tot_time = 4500;
t1 = linspace(0,tot_time,npt);
T1 = linspace(isoTemp,isoTemp,npt);
T = interp1(t1,T1,tot_time);
% c1 c2 k2dtg
param_grindling = [0.5 -15 220 4.5*10^6 1.15 1.3*10^6 1.3 1.20 0.6 0.5 2000]; % +/- 5~10% and compare
lambda_Tg_grind = param_grindling(1); T_g0_grind = param_grindling(2);
T_g1_grind = param_grindling(3); A1_grind = param_grindling(4);
n1_grind = param_grindling(5); A2_grind = param_grindling(6);
n2 = param_grindling(7); m_grind = param_grindling(8);
c1 = param_grindling(9); c2 = param_grindling(10);
k_2d_tg = param_grindling(11);
% Find Tg
T_g =@(a,T) ((lambda_Tg_grind.*a(T_g1_grind - T_g0_grind))./(1 - (1 - lambda_Tg_grind).*a)); + T_g0_grind;
delta_Tg = @(a,T) T - T_g;
% Find Activation energy for k1 and k2
mER1_g = @(a,T) -((T_g.^2).*(c1./c2)).*8.314;
mER2_g = @(a,T) -((c1.*c2.*(T_g + delta_Tg).^2)./(c2 + delta_Tg).^2).*8.314;
k1_grindling = @(a,T) A1_grind*exp(mER1_f/T); % are they different?
k2_grindling = @(a,T) A2_grind*exp(mER2_f/T);
% T_g conditions
Condition = @(a,T) T_g(a,T) + delta_Tg(a,T);
if isoTemp > Condition(a,T) % how to define @(a,T) in if condi
k_2d = @(a,T) k_2d.*exp(mER2_g.*((1./T) - (1./(T_g - delta_tg))));
elseif T_g <= isoTemp && isoTemp <= Condition(a,T)
k_2d = @(a,T) k_2d_tg.*exp((c1.*(T - T_g))./(c2 + (T - T_g)));
elseif isoTemp < T_g
k_2d = @(a,T) k_2d_tg.*exp(mER1_g.*(1./T - 1./T_g));
end
% Switiching in reaction rate constant - k_eff
k_eff = @(a,T) 1./((1./k_2d) + (1./k2_grindling(a,T)));
dadt_grindling = @(a,T) k1_grindling(a,T)*(1 - a).^n1_grind + k_eff*a.^m_grind*(1 - a).^n2;
[y0_grindling,yp0_grindling] = decic(@grindling_solve,0,y0,[0 tot_time],yp0,[],[]); % Calculate the actual inital defree of cure that satisfied the
%cure kinetics equation (now yp and yp0 are consistent)
[db{isT-1}.Time_grindling, db{isT-1}.Cure_grindling] = ode15i(@grindling_solve, [0 tot_time], y0_grindling, yp0_grindling,[]); % Solve the ODE and record results in the
end
N.B Above shows a detail version of my code. I can confirm my @grindling_solve is correct. And the critical bit which I am wrong is the if statement.
Ho Nam Ernest Yim
Ho Nam Ernest Yim le 17 Juil 2018
Modifié(e) : Ho Nam Ernest Yim le 17 Juil 2018
Hi @OCDER, I have actually tried that before. And it said undefined function or variable 'a' which I don't get it also, cause I thought it will pass it to the ode15i as a function of a and T
OCDER
OCDER le 17 Juil 2018
Well, the if statement is BEFORE your ode15i, so no a value is being passed on to Condition. you'll have to redesign your script here... Did you want a main function to switch the function handles WHILE doing the ode15i? You'll have to rewrite this script then.
Ho Nam Ernest Yim
Ho Nam Ernest Yim le 17 Juil 2018
Once again, thank you for all the help guys!! lots of appreciate!! I am trying to fix the logic of the code now. Thank you

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by