Filter löschen
Filter löschen

Unable to call my function and get an error

1 Ansicht (letzte 30 Tage)
Muhammad Choudhury
Muhammad Choudhury am 4 Feb. 2022
Kommentiert: Davide Masiello am 4 Feb. 2022
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".)

Akzeptierte Antwort

Davide Masiello
Davide Masiello am 4 Feb. 2022
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 Kommentare
Muhammad Choudhury
Muhammad Choudhury am 4 Feb. 2022
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 am 4 Feb. 2022
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!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by