Borrar filtros
Borrar filtros

Unable to call my function and get an error

1 visualización (últimos 30 días)
Muhammad Choudhury
Muhammad Choudhury el 4 de Feb. de 2022
Comentada: Davide Masiello el 4 de Feb. de 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".)

Respuesta aceptada

Davide Masiello
Davide Masiello el 4 de Feb. de 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 comentarios
Muhammad Choudhury
Muhammad Choudhury el 4 de Feb. de 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 el 4 de Feb. de 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!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by