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?

 Accepted Answer

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 Comments

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,:));
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.
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.
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
If you are trying to call FPAeld1, then why does your code call Fun? Why aren't you calling FPAeld1?
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,:));
and how do i code such the the first script takes the data from the second script?
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.
@Stephen-would really help if you give an example with that second script

Sign in to comment.

More Answers (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 Comments

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.
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
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.

Sign in to comment.

Categories

Tags

No tags entered yet.

Asked:

on 9 Sep 2015

Edited:

on 3 May 2016

Community Treasure Hunt

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

Start Hunting!