Please help with fzero function . Thanks

25 Ansichten (letzte 30 Tage)
Jai
Jai am 4 Nov. 2023
Beantwortet: Torsten am 4 Nov. 2023
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);

Akzeptierte Antwort

Dyuman Joshi
Dyuman Joshi am 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.

Weitere Antworten (1)

Torsten
Torsten am 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)))

Kategorien

Mehr zu Optimization finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by