FUN must be a function, a valid character vector expression, or an inline function object. | fzero
显示 更早的评论
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
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 个)
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
类别
在 帮助中心 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!