Unable to call my function and get an error

1 次查看(过去 30 天)
function h = fplothFUNC(d,Kp,A,t)
%the function is used to plot the following equation:
% h*(t)=(d/Kp)*(1-exp(-(Kp/A)*t))
% call for function:
% h = fplothFUNC(1,[0.2 2 6 15 55 105],2,0:0.1:10)
LK=length(Kp);
Lt=length(t);
h=zeros(LK,Lt); %create a matrix of zeros for LK AND Lt columns
for i=1:LK
h(i,:)= (d/Kp(i))*(1-exp(-(Kp(i)/A)*t));
end
end
semilogy(t,h); xlabel('time (hours)'); ylabel('height difference (m)')
h = fplothFUNC(1,[0.2 2 6 15 55 105],2,0:0.1:10)
i get an error saying:
Error: File: fplothFUNC.m Line: 16 Column: 1
This statement is not inside any function.
(It follows the END that terminates the definition of the function "fplothFUNC".)

采纳的回答

Davide Masiello
Davide Masiello 2022-2-4
d = 1;
t = 0:0.1:10;
Kp = [0.2 2 6 15 55 105];
A = 2;
h = fplothFUNC(d,Kp,A,t);
semilogy(t,h); xlabel('time (hours)'); ylabel('height difference (m)')
function h = fplothFUNC(d,Kp,A,t)
LK=length(Kp);
Lt=length(t);
h=zeros(LK,Lt); %create a matrix of zeros for LK AND Lt columns
for i=1:LK
h(i,:)= (d/Kp(i))*(1-exp(-(Kp(i)/A)*t));
end
end
Result:
  2 个评论
Muhammad Choudhury
thanks but why can it not be in this form?:
h = fplothFUNC(1,[0.2 2 6 15 55 105],2,0:0.1:10)
Davide Masiello
Davide Masiello 2022-2-4
It can, I was just ordering the way I usually do.
The problem with your script was that the function was defined before the command calling it, and that is not how matlab works.
I hope this clarifies things. Please consider accepting the answer if it solved your issue, cheers!

请先登录,再进行评论。

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by