|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
m函数simulink以及遗传算法混用的问题下面一例是matlab帮助文档中关于遗传算法的一个例子那个运算
%函数
function y = simple_fitness(x)
y = 100 * (x(1)^2 - x(2)) ^2 + (1 - x(1))^2;
%函数
%脚本
FitnessFunction = @simple_fitness;
numberOfVariables = 2;
[x,fval] = ga(FitnessFunction,numberOfVariables)
%脚本
我用函数在simulink中建立模型
存储为my_sim_2名称
编写函数如下
function y=my_sim_m(x)
global x1 ;
global x2;
assignin('base','x1',x(1));
assignin('base','x2',x(2));
sim('my_sim_2');
assignin('base','zz',zz);
y=zz;
再编写脚本调用该函数用遗传算法进行相关计算如下:
FitnessFunction = @my_sim_m;
numberOfVariables = 2;
[x,fval] = ga(FitnessFunction,numberOfVariables);
结果运算出错!
错如下:
??? Error using ==> gads\private\makeState
GA cannot continue because user supplied fitness function failed with the following error:
Error using ==> sim
--> Invalid matrix-format variable specified as workspace input in 'my_sim_2/From Workspace'. The matrix must have two dimensions and at least two columns. Complex signals of any data type and non-double real signals must be in structure format. The first column must contain time values and the remaining columns the data values.
--> Invalid matrix-format variable specified as workspace input in 'my_sim_2/From Workspace1'. The matrix must have two dimensions and at least two columns. Complex signals of any data type and non-double real signals must be in structure format. The first column must contain time values and the remaining columns the data values.
Error in ==> gads\private\gaunc at 44
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ==> ga at 384
[x,fval,exitFlag,output,population,scores] = gaunc(FitnessFcn,GenomeLength, ...
请高手给与指点,不知道是错在哪个地方,我认真检查了,原本以为x1,x2两个变量在m文件与simulink间不能顺利传递值,这一疑问我通过一个更简单的例子已经得到解决,由于接触遗传算法不久,并且我是想通过遗传算法对我simulink中的的相关参数进行优化求解,但遗传算法又必须有适应度函数,所以我就想到这样做!请高手给与指点哈!期待!!!!! |
|