Why this code says unrecognized function u?

I had a GA code which was generated by GA in earlier MATLAB version. But I had used there global variables. Later on, I replaced the global variables and passed them to function. All the three codes are in the attachments. You can run the m-file "main.m" and get the results. But now here in MATLAB 2022b, I run the code, and it gives me the following error:
Unrecognized function or variable 'u'.
Error in myGA1>@(b)Vectorized(b,u,Noise) (line 17)
ga(@(b)Vectorized(b,u,Noise),nvars,[],[],[],[],lb,ub,[],[],options);
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in makeState (line 58)
firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));
Error in galincon (line 24)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 416)
[x,fval,exitFlag,output,population,scores] = galincon(FitnessFcn,nvars, ...
Error in myGA1 (line 17)
ga(@(b)Vectorized(b,u,Noise),nvars,[],[],[],[],lb,ub,[],[],options);
Error in main (line 23)
[x,fval,time] = myGA1(nvars,lb,ub,PopulationSize_Data,MaxGenerations_Data,FunctionTolerance_Data,ConstraintTolerance_Data);
Caused by:
Failure in initial user-supplied fitness function evaluation. GA cannot continue.

 Accepted Answer

I see that you have defined u in main(), but I don't see that you pass it to myGA1(). So, when myGA1() tries to pass it to Vectorize(), it's undefined.

5 Comments

Thanks a lot dear the cyclist for your kind response. So how will we correct it?
I edited your files so that
  • the call to myGA1 from main passes u as a function argument
  • the function definition for myGA1 accepts that argument
Now when I run your code, I get a different error. (See the red text below.) I don't know how to solve that error.
main
'Noise' is used in the following examples:
ADC IBIS-AMI Model Based on COM
Architectural 112G PAM4 ADC-Based SerDes Model
Architectural 100G Dual-Summing-Node-DFE PAM4 SerDes Receiver Model

Error in myGA1>@(b)Vectorized(b,u,Noise) (line 17)
ga(@(b)Vectorized(b,u,Noise),nvars,[],[],[],[],lb,ub,[],[],options);

Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});

Error in makeState (line 58)
firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));

Error in galincon (line 24)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);

Error in ga (line 416)
[x,fval,exitFlag,output,population,scores] = galincon(FitnessFcn,nvars, ...

Error in myGA1 (line 17)
ga(@(b)Vectorized(b,u,Noise),nvars,[],[],[],[],lb,ub,[],[],options);

Error in main (line 23)
[x,fval,time] = myGA1(nvars,lb,ub,PopulationSize_Data,MaxGenerations_Data,FunctionTolerance_Data,ConstraintTolerance_Data,u);
Thanks a lot dear the cyclist for your kind response. Following your guidance, we need to pass the Noise also like u. So thank you very much dear the cyclist. I did it with your guidance. But now when I ran it, and then I typed the following lines in the command window to check whether the estimated vector 'two1' is nearly the same as is u or not?
[one1 ind]=sort(one,'descend');
[fitness,ind1]=min(one1)
two1=two(ind1,:)
u
I observed that the vector 'two1' and 'u' are not nearly same. Instead they are very much different. Actually I am estimating the vector 'two1' by GA which must have the same values as there are in u or if not the same values, then nearly the same values. But when I comment the following two lines inside myGA1.m, then they become nearly equal though still different but the difference is smaller now as compared to before. Why it is so? And what else can be done with myGA1.m options so that the vector two1 is nearly having the same values as there are in u?
I don't have the Optimization Toolbox, so I can't really help you debug this.
Since we solved your original question here, I suggest that you accept my answer to this question, and open a new question. (I also don't fully understand what you are asking, so maybe give a little more detail about why you expect to see something different than what you are seeing.)
Thanks a lot dear the cyclist for your kind response. Yes, you are right. I am gong to accept your answer.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!