Please help with fzero function . Thanks

11 views (last 30 days)
T_ad_n = fzero(@(a) LHS - (n(3) * (hf_0(3) + dh_CO2(a))) - (n(4) * (hf_0(4) + dh_H2O(a))),[298,6000]);
%% Where LHS = constant , n(1:5) and hf_0(1:4) already defined dh_CO2 & dh_H2O are functions in a type sym
I am getting this error
Error using fzero>localFirstFcnEvalError
FZERO cannot continue because user-supplied function_handle ==>
@(a)LHS-(n(3)*(hf_0(3)+dh_CO2(a)))-(n(4)*(hf_0(4)+dh_H2O(a))) failed with the error below.
Index exceeds the number of array elements. Index must not exceed 1.
Error in fzero (line 231)
localFirstFcnEvalError(FunFcn,FunFcnIn,ME);

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 4 Nov 2023
You need to define dh_CO2 and dh_H2O as symfun objects - symfun, i.e. both as symbolic functions of t.
Or if you can, define both as function handles of t. (or any other variable, the name of the independent variable does not matter, as long as it is a valid MATLAB name)
Right now, they are just symbolic variables, and when you use dh_CO2(a) and dh_H2O(a), MATLAB interprets it as indexing, which gives you the error, as they are scalar variables.

More Answers (1)

Torsten
Torsten on 4 Nov 2023
Convert dh_CO2 and dh_H2O from symbolic functions into function handles by using "matlabFunction".
After this, they can be used as you do in
@(a) LHS - (n(3) * (hf_0(3) + dh_CO2(a))) - (n(4) * (hf_0(4) + dh_H2O(a)))

Categories

Find more on Optimization in Help Center and File Exchange

Tags

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!