fengjohn 发表于 2009-3-24 18:54

m函数simulink以及遗传算法混用的问题

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;
= 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;
= 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
       = gaunc(FitnessFcn,GenomeLength, ...



请高手给与指点,不知道是错在哪个地方,我认真检查了,原本以为x1,x2两个变量在m文件与simulink间不能顺利传递值,这一疑问我通过一个更简单的例子已经得到解决,由于接触遗传算法不久,并且我是想通过遗传算法对我simulink中的的相关参数进行优化求解,但遗传算法又必须有适应度函数,所以我就想到这样做!请高手给与指点哈!期待!!!!!

yufeng 发表于 2009-3-25 07:54

workspace的数据格式是什么

fengjohn 发表于 2009-3-25 08:20

谢谢yufeng的回答,workspace 的数据格式都是数值形式的变量!yufeng有什么建议吗?

jiaofirefly 发表于 2012-7-16 10:43

{:{23}:}
页: [1]
查看完整版本: m函数simulink以及遗传算法混用的问题