FUN must be a function, a valid character vector expression, or an inline function object. | fzero

4 次查看(过去 30 天)
function T = FA(y)
for i = 1:length(y)
if y(i) >= 12.5
N1(i)= 0
N2(i)= 8
N3(i)= 9
N4(i)=3.76*y(i)
N5(i)= y(i)-12.5
else
N1(i)= 2*(12.5 -y(i))
N2(i)= 2*(y(i) - 8.5)
N3(i)= 0
N4(i)=3.76*y(i)
N5(i)= 0
end
f{i}=@(T)-5078905.27 + N1(i)*(297334.224)+ N1(i)*((299180+37.85*(T)+(-4571.9)*log(T))-294170.430054)+N2(i)*((56835+66.27*(T)+(-11634)*log(T))-24557.403501)+N3(i)*((88923+49.36*(T)+(-7940.8)*log(T))-69574.657860)+N4(i)*((31317+37.46*(T)+(-4559.3)*log(T))-26135.539906)+N5(i)*((43388+42.27*(T)+(-6635.4)*log(T))-27886.197583)
fzero(f,2500)
end
I know that if I change f{i} to f(i) I'll be able to run the script and input whatever values I want. However, I need this script tor work in conjuction with this one
clc; clear; close all;
r = .7:.1:2.0;
y = 12.5*r
T = FA2(y);
I eventually want to plot the r values vs. the T values (fzero outputs). If I change f{i} to f(i) I get a different error code on the second script, "Nonscalar arrays of function handles are not allowed; use cell arrays instead."
What do I need to do to make these work together? Thank you
  1 个评论
Stephen23
Stephen23 2018-10-16
"I know that if I change f{i} to f(i) I'll be able to run the script and input whatever values I want."
No, actually you will get an error (because function handles cannot be put into an array).

请先登录,再进行评论。

回答(1 个)

Stephen23
Stephen23 2018-10-16
编辑:Stephen23 2018-10-16
You don't need to store the functions in any array, you only need to store the output values:
function out = FA(y)
out = nan(size(y));
for k = 1:numel(y)
...
fun = @(T)...;
out(k) = fzero(fun,2500);
end
end
  2 个评论
Vickson Leji
Vickson Leji 2018-10-16
When I try to run this script
clc; clear; close all;
r = .7:.1:2.0;
y = 12.5*r
out(k) = FA2(y);
plot(r,T)
title('Adiabatic Flame Temperature vs. Y/Ycc')
xlabel('Y/Ycc')
ylabel('Adiabatic Flame Temperature, Tp')
I'm getting this error
Operands to the || and && operators must be convertible to
logical scalar values.
Error in fzero (line 322)
elseif ~isfinite(fx) || ~isreal(fx)
Error in FA2 (line 24)
out(k) = fzero(f,2500);
Error in FA2run (line 5)
out(k) = FA2(y);
Stephen23
Stephen23 2018-10-17
@Vickson Leji: as the fzero help clearly states, the function must accept and return a scalar value. So you need to fix your function so that it returns a scalar output value.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by