遗传算法代码出错原始数据X=[14.38,14,13.06,12.09,10.98,9.88,8.27];下面是一段代码function z1=minwucha(a,b,c)X=[14.38,14,13.06,12.09,10.98,9.88,8.27];X1(1)=X(1);for k=2:length(X)X1(k)=X(1)-(1-exp(a))*(c-b/a)*exp(-a*k)+c;endfor k=1:len
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/11 05:37:48
遗传算法代码出错原始数据X=[14.38,14,13.06,12.09,10.98,9.88,8.27];下面是一段代码function z1=minwucha(a,b,c)X=[14.38,14,13.06,12.09,10.98,9.88,8.27];X1(1)=X(1);for k=2:length(X)X1(k)=X(1)-(1-exp(a))*(c-b/a)*exp(-a*k)+c;endfor k=1:len
遗传算法代码出错
原始数据X=[14.38,14,13.06,12.09,10.98,9.88,8.27];
下面是一段代码
function z1=minwucha(a,b,c)
X=[14.38,14,13.06,12.09,10.98,9.88,8.27];
X1(1)=X(1);
for k=2:length(X)
X1(k)=X(1)-(1-exp(a))*(c-b/a)*exp(-a*k)+c;
end
for k=1:length(X)
z(k)=abs((X(k)-X1(k))/X(k));
end
z1=sum(z(:))/length(X)
X1第一个数据跟X第一个相等,其余数据由上面表达式生成,使得最终的平均相对误差最小
下面是命令行
>> options=gaoptimset('Generations',800,'StallGenLimit',300,'PlotFcns',@gaplotbestf);
>> [x,f]=ga(@minwhcha,3,options)
最终显示
Undefined function or method 'minwhcha' for input arguments of type 'double'.
Error in ==> validate>@(x)fitness(x,FitnessFcnArgs{:}) at 135
fitness = @(x) fitness(x,FitnessFcnArgs{:});
Error in ==> fcnvectorizer at 14
y(i,:) = feval(fun,(pop(i,:)));
Error in ==> makeState at 47
Score =
fcnvectorizer(state.Population(initScoreProvided+1:end,:),FitnessFcn,1,options.SerialUserFcn);
Error in ==> gaunc at 41
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ==> ga at 279
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,nvars,...
Caused by:
Failure in user-supplied fitness function evaluation.GA cannot continue.
求教哪里出错了
遗传算法代码出错原始数据X=[14.38,14,13.06,12.09,10.98,9.88,8.27];下面是一段代码function z1=minwucha(a,b,c)X=[14.38,14,13.06,12.09,10.98,9.88,8.27];X1(1)=X(1);for k=2:length(X)X1(k)=X(1)-(1-exp(a))*(c-b/a)*exp(-a*k)+c;endfor k=1:len
函数minwucha(a,b,c)的参数改为长度为3的向量,如minwucha(p),p为长度为3的向量.