Error using fzero on line 306 with function handles
Show older comments
I am trying to write a script that given a set of polynomials that correspond to Cp values of different molecular components you can find the Adiabatic flame temperature.
This is my code thus far:
%The components polynomials
p_CO2 = [7.469e-9,-3.501e-5,5.981e-2,22.26];
p_N2 = [-2.873e-9,0.8081e-5,-0.1571e-2,28.90];
p_H2O = [-3.595e-9,1.055e-5,0.1923e-2,32.24];
%If I am trying to find T do I need to evaluate the polynomials at a T?
%polyval(p_CO2, 1000);
%polyval(p_N2,1000);
%polyval(p_H2O,1000);
%Variables for equation
N_ethyl = 1;
hf0_ethyl = -277690;
N_h2o = 0.0919;
hf0_h2o = -241820;
N_co2 = 2;
hf0_co2 = -393520;
N_H2O = 3;
N_n2 = 11.28;
%Cp equations for the components
cp_CO2 = @(T) polyval(p_CO2, T);
cp_N2 = @(T) polyval(p_N2, T);
cp_H2O = @(T) polyval(p_H2O, T);
%Delta h equations from reference temp(298K) to an adiabatic flame temp.
delta_h_CO2 = @(T) integral(cp_CO2,298,T);
delta_h_N2 = @(T) integral(cp_N2,298,T);
delta_h_H2O = @(T) integral(cp_H2O,298,T);
f = @(T) (N_co2(hf0_co2+(delta_h_CO2)) + N_H2O(hf0_h2o+(delta_h_H2O)) + N_n2(delta_h_N2)) - ((N_ethyl*(hf0_ethyl)) + (N_h2o*(hf0_h2o)));
fzero( f, 2500)
%Since the goal of this problem is to find the T that is the upper limit of
%the integral, I'm not sure what the issue would be.
And I keep receiving this error message, any assistance would be apprecaited! Still a bit rusty on MATLAB trying to learn more applications for it.
Error using fzero (line 306)
FZERO cannot continue because user-supplied function_handle ==>
@(T)(N_co2(hf0_co2+(delta_h_CO2))+N_H2O(hf0_h2o+(delta_h_H2O))+N_n2(delta_h_N2))-((N_ethyl*(hf0_ethyl))+(N_h2o*(hf0_h2o))) failed with the
error below.
Undefined function 'plus' for input arguments of type 'function_handle'.
Error in ThermoScript (line 33)
fzero( f, 2500)
Answers (2)
f was not using T at all,
f = @(T) (N_co2(hf0_co2+(delta_h_CO2(T))) + N_H2O(hf0_h2o+(delta_h_H2O(T))) + N_n2(delta_h_N2(T))) - ((N_ethyl*(hf0_ethyl)) + (N_h2o*(hf0_h2o)));
1 Comment
Torsten
on 2 May 2019
f = @(T) (N_co2*(hf0_co2+delta_h_CO2(T)) + N_H2O*(hf0_h2o+delta_h_H2O(T)) + N_n2*delta_h_N2(T)) - (N_ethyl*hf0_ethyl + N_h2o*hf0_h2o);
Steven Lord
on 2 May 2019
The error is correct, you cannot add function handles.
f1 = @(x) sin(x);
f2 = @(x) cos(x);
f3 = @(x) f1 + f2;
f3(pi/4) % Throws this error
What you can do is add the values returned by evaluating those function handles.
f1 = @(x) sin(x);
f2 = @(x) cos(x);
f3 = @(x) f1(x) + f2(x);
f3(pi/4) % Works and returns sin(pi/4)+cos(pi/4)
2 Comments
Christian Knutson
on 2 May 2019
Matt J
on 2 May 2019
@Christian,
Please Accept-click answers when they fulfill what you want.
Categories
Find more on Animation 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!