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

4 views (last 30 days)
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 Comment
Stephen23
Stephen23 on 16 Oct 2018
"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).

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 16 Oct 2018
Edited: Stephen23 on 16 Oct 2018
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 Comments
Vickson Leji
Vickson Leji on 16 Oct 2018
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 on 17 Oct 2018
@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.

Sign in to comment.

Categories

Find more on Function Creation in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!