필터 지우기
필터 지우기

Unable to call my function and get an error

조회 수: 1 (최근 30일)
Muhammad Choudhury
Muhammad Choudhury 2022년 2월 4일
댓글: Davide Masiello 2022년 2월 4일
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
Muhammad Choudhury 2022년 2월 4일
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