Function inside a for loop

Hello everyone,
As a beginner I have the following code:
% a1_CO2 to b2_CO2 are scalar values.
s_CO2= @(T) (R_u)*(-a1_CO2*T^(-2)/2-a2_CO2/T+a3_CO2*log(T)+a4_CO2*T+a5_CO2*(T^2)/2+a6_CO2*(T^3)/3+a7_CO2*(T^4)/4+b2_CO2)
y41_CO2 = [0.0449 0.06 0.0665 0.0687 0.071....] %This is 1x21 array
for i=1:21
s41_0(i) = @(T) (y41_CO2(i)*s_CO2(T)
end
I want to define s41_0 as a (T) dependent function and recall it as recalling s_CO2(T) but don't know the syntax.
For example how can i obtain s41_0(5) for T dependent function and define T later for a scalar value.
Thanks in advance.

回答 (1 件)

Torsten
Torsten 2022 年 1 月 10 日
編集済み: Torsten 2022 年 1 月 10 日

1 投票

An array of function handles is not possible.
Instead, you should define
s41_0 = @(a,T) a.*s_CO2(T)
and evaluate it as
val41_0i = s41_0(y41_CO2(i),T)
or
val41_0 = s41_0(y41_CO2,T)
for a numerical value for T.

1 件のコメント

Süleyman Kagan AYAZ
Süleyman Kagan AYAZ 2022 年 1 月 22 日
Thx for the answer.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2022 年 1 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by