Why this gives error?

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{:})

Risposte (1)

Jan
Jan il 22 Nov 2022
Modificato: Jan il 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 Commenti

Thank you very much dear Jan for your kind response. Yes you are right that I still have used the slower version. But that doesn't matter. I will replace it once it gets correct.
I did these changes:
Replaced the function definition line by this:
function [x,fval,gfx,output]=hPSO1(fitnessfun,nvars,hPSOoptions,varargin)
Further changed the main file as:
dim=4;
function [x,fval,gfx,output] = main(4)%main(varargin)
[x,fval,gfx,output] = hPSO1(@main3, dim, options, varargin{:})
end
And ran the main, but now aagain it gives this error:
File: main.m Line: 2 Column: 37
Invalid expression. Check for missing multiplication operator, missing or unbalanced
delimiters, or other syntax error. To construct matrices, use brackets instead of
parentheses.
Jan
Jan il 22 Nov 2022
Modificato: Jan il 22 Nov 2022
I told you already in my answer, that it is not meaningful to use a constant in the definition of a function. Now you do this again:
function [x,fval,gfx,output] = main(4)
% ^ ???
What should be set to the value 4 here?
Which Matlab version are you using? In older versions you cannot insert the definition of a function inside a script.
Did you solve the Onramp tutorial already? It is explained there exhaustively, how inputs arguments are defined for functions.
Sadiq Akbar
Sadiq Akbar il 22 Nov 2022
Thanks a lot for your kind response dear Jan. Actually if you look inside the "myfitness", I have a desired vector u. I want to estimate its value by using this hPSO1 algorithm. But how? That's the problem. When I run it, it gives me the error which I can't rectify. If you can, please rectify it, then guide me how to do it. The algorithm should return a solution vector having similar value or nearly similar value to u. You can ignore main.m. For this someone has advised me to do so.
I repeat: Just take the three functions namely "hPSO1", "hPSOoptions" and "myfitness". Now you have to run the algorithm hPSO1 which will call "myfitness" function and will use "hPSOoptions" also to find the solution vector. It should return a solution vector which must have the same value or nealry the same value as is u. Regards,

Accedi per commentare.

Categorie

Scopri di più su Get Started with MATLAB in Centro assistenza e File Exchange

Richiesto:

il 22 Nov 2022

Commentato:

il 22 Nov 2022

Community Treasure Hunt

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

Start Hunting!

Translated by