Undefined function 'Fun" for input argument of type 'double'?

function z=fun(U)
z='FPAeld1';
end
i need to call the first script from this algorithm but i'm getting error "undefined function 'Fun'.." why?

 Respuesta aceptada

James Tursa
James Tursa el 9 de Sept. de 2015
I see a "fun" but I don't see a "Fun" anywhere. Do you have a file Fun.m that you are trying to call? Or are you trying to call "fun"?

12 comentarios

no there's no Fun file ...Fun comes in here
% Initialize the population/solutions
for i=1:n,
Sol(i,:)=Lb+(Ub-Lb).*rand(1,d);
Fitness(i)=Fun(Sol(i,:));
James Tursa
James Tursa el 9 de Sept. de 2015
Editada: James Tursa el 9 de Sept. de 2015
Yes, I see the call to Fun ... what I don't see is a function called Fun anywhere. The only thing I see is lower case fun.
JL555
JL555 el 9 de Sept. de 2015
i'm calling the first script in the last lines of the algorithm..is that the correct way to do it?
Exactly what code are you trying to run when you call Fun? Please repeat the first few lines in your response so we know exactly. That code needs to be in a file (e.g. Fun.m) or a sub-function ... and I don't see that.
JL555
JL555 el 9 de Sept. de 2015
i'm not trying to call any Fun file. All i need is to call the first file which is a function file..FPAeld1.m is the name of the file
James Tursa
James Tursa el 9 de Sept. de 2015
Editada: James Tursa el 9 de Sept. de 2015
If you are trying to call FPAeld1, then why does your code call Fun? Why aren't you calling FPAeld1?
JL555
JL555 el 9 de Sept. de 2015
yes its in a file called FPAeld1.m...
Which line do you want to call FPAeld1? If it is this line:
Fitness(i)=Fun(Sol(i,:));
Then change it to this:
Fitness(i)=FPAeld1(Sol(i,:));
JL555
JL555 el 9 de Sept. de 2015
ok thanks alot ...
JL555
JL555 el 9 de Sept. de 2015
and how do i code such the the first script takes the data from the second script?
Stephen23
Stephen23 el 9 de Sept. de 2015
Editada: Stephen23 el 9 de Sept. de 2015
The answer to this is very simple: stop writing scripts and write functions instead. While scripts are great for playing around with some data and getting things going, functions offer many many advantages over scripts, including their own name spaces, algorithm abstraction, the ability to pass variables in and out, and lots of other handy stuff.
Turn your scripts into functions and pass that data as an output.
JL555
JL555 el 9 de Sept. de 2015
Editada: JL555 el 9 de Sept. de 2015
@Stephen-would really help if you give an example with that second script

Iniciar sesión para comentar.

Más respuestas (1)

At the bottom you have
function z=fun(U)
z='FPAeld1';
end
Change that to
function z = Fun(U)
z = FPAeld1(U);
end

8 comentarios

JL555
JL555 el 9 de Sept. de 2015
Editada: JL555 el 9 de Sept. de 2015
and how do i code such that the first script takes the data from the second script?
function z = Fun(U)
global objfun
z = feval(objfun, U);
end
Better yet, do not use global variables. For example in your second script, do not have global objfun and instead have
objfun = @FPAeld1
and then change your
function [best,fmin,N_iter]=fpa_demo(para)
to
function [best,fmin,N_iter]=fpa_demo(para, Fun)
and do not have that "function Fun" at the end of the code.
JL555
JL555 el 9 de Sept. de 2015
have u tried running the code?
function z = Fun(U)
global objfun
z = feval(objfun, U);
end
this should replace the last line of the algorithm?
I have not tried to run the code. You have not shown what should be passed to fpa_demo() . Your script does not call fpa_demo()
Yes, the lines
function z = Fun(U)
global objfun
z = feval(objfun, U);
end
would replace what you had as
function z=fun(U)
z='FPAeld1';
end
JL555
JL555 el 9 de Sept. de 2015
From this algorithm script all i want to do is call the first script which is a function script. Is that possible ?? if not then what should i do because i'm really confused with all this
Did what u said but now this error comes
Error using *
Inner matrix dimensions must agree.
Error in FPAeld1 (line 35)
F=Pi.*Pi*a1+Pi*b1+c1+mod(ei*sin.*(fi*(pmin-Pi)))
Poster extracted this error into a new Answer.

Iniciar sesión para comentar.

Categorías

Más información sobre Entering Commands en Centro de ayuda y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Preguntada:

el 9 de Sept. de 2015

Editada:

el 3 de Mayo de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by