Why this gives error?
1 view (last 30 days)
Show older comments
I downloaded the attached codes " hPSO1.m", and "hPSOoptions.m" and tried to run it with my fitness function"myfitness.m" (also in the attachment) but it gives the following error:
Unrecognized function or variable 'options'.
Error in main (line 3)
[x,fval,gfx,output] = hPSO1(@main3, 4, options, varargin{:})
0 Comments
Answers (1)
Jan
on 22 Nov 2022
Edited: Jan
on 22 Nov 2022
This is the failing function:
function [x,fval,gfx,output] = main(varargin)
[x,fval,gfx,output] = hPSO1(@main3, 4, options, varargin{:})
end
As the error message tells clearly, the variable options is not defined. What do you expect it to be?
Of course you can use defined variables only. Because this is such fundamental for programming, I recommend to read the tutorial again: https://www.mathworks.com/learn/tutorials/matlab-onramp.html
In the next step the function hPSO1 will fail:
function [x,fval,gfx,output]=hPSO1(main3,4,hPSOoptions,varargin)
% ^ ???
Providing a constant in the list of inputs is not meaningful. How should Matlab handle this? It could consider the value 4 as defined?
In the function myfitness a comment character is missing in line 6:
K=3; 3rd constant
You see a corresponding warning in the editor. Consider and fix these warnings.
Although you got instructions how to improve this in two other of your questions already, you still use the slow and complicated form:
abc=0.0;
for m1=1:M*N
abc=abc+(abs(yo(m1,1)-ye(m1,1))).^2;
end
abc=abc/(M*N);
e=abc;
% Faster:
e = mean((yo - ye).^2);
3 Comments
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!