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 件のコメント

JL555
JL555 2015 年 9 月 9 日
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.
JL555
JL555 2015 年 9 月 9 日
i'm calling the first script in the last lines of the algorithm..is that the correct way to do it?
James Tursa
James Tursa 2015 年 9 月 9 日
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 2015 年 9 月 9 日
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?
JL555
JL555 2015 年 9 月 9 日
yes its in a file called FPAeld1.m...
James Tursa
James Tursa 2015 年 9 月 9 日
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 2015 年 9 月 9 日
ok thanks alot ...
JL555
JL555 2015 年 9 月 9 日
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 日

0 投票

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?
Walter Roberson
Walter Roberson 2015 年 9 月 9 日
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 2015 年 9 月 9 日
have u tried running the code?
JL555
JL555 2015 年 9 月 9 日
function z = Fun(U)
global objfun
z = feval(objfun, U);
end
this should replace the last line of the algorithm?
Walter Roberson
Walter Roberson 2015 年 9 月 9 日
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 2015 年 9 月 9 日
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
JL555
JL555 2015 年 9 月 9 日
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)))
Steven Lord
Steven Lord 2015 年 9 月 9 日
Poster extracted this error into a new Answer.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeEntering 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