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?

 채택된 답변

James Tursa
James Tursa 2015년 9월 9일

0 개 추천

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

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 2015년 9월 9일
편집: James Tursa 2015년 9월 9일
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
James Tursa
James Tursa 2015년 9월 9일
편집: James Tursa 2015년 9월 9일
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,:));
ok thanks alot ...
and how do i code such the the first script takes the data from the second script?
Stephen23
Stephen23 2015년 9월 9일
편집: Stephen23 2015년 9월 9일
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 2015년 9월 9일
편집: JL555 2015년 9월 9일
@Stephen-would really help if you give an example with that second script

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2015년 9월 9일
At the bottom you have
function z=fun(U)
z='FPAeld1';
end
Change that to
function z = Fun(U)
z = FPAeld1(U);
end

댓글 수: 8

JL555
JL555 2015년 9월 9일
편집: JL555 2015년 9월 9일
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Entering Commands에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

질문:

2015년 9월 9일

편집:

2016년 5월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by