用matlab遗传算法拟合曲线怎么拟合? 两组数据,必须用遗传算法,最好有程序或者详细的过程,急
来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/05/02 00:34:05
用matlab遗传算法拟合曲线怎么拟合? 两组数据,必须用遗传算法,最好有程序或者详细的过程,急
例如:
已知数据队列 buf=【5410.】
x取值 1:n n是队列长度
函数 f(x)=a+b*sin(c*x+d) .
avg 是队列平均值
a b c d 为参数 a范围 (2/3,1)*avg
b范围 (0,1/3)*avg
c的范围 (0,24*pi)
d (0,2*pi)
1、首先定义目标函数
function y=ga_curfit(x)
global ydata n
t=1:n;
y=0;
for i=1:n
y=y+(ydata(i)-(x(:,1)+x(:,2).*sin(x(:,3).*t(i)+x(:,4)))).^2/n;
end
y=sqrt(y);
end
2、把数据b.txt放在工作空间目录中
然后再命令窗口中输入
clear
global ydata n
format long g
load b.txt
ydata=b';
n=length(ydata);
avg=sum(ydata)/n;
LB=[2/3*avg 0 0 0];
UB=[1*avg 1/3*avg 24*pi 2*pi];
nvars=4;
options=gaoptimset;
options=gaoptimset(options,'PopulationSize',300);
options=gaoptimset(options,'CrossoverFraction',0.8);
options=gaoptimset(options,'MigrationFraction',0.1);
options=gaoptimset(options,'Generations',500);
options = gaoptimset(options,'TolFun',1e-50);
%options = gaoptimset(options,'InitialPopulation',final_pop);
options = gaoptimset(options,'Display','final');
options = gaoptimset(options,'PopInitRange',[LB;UB]);
options = gaoptimset(options,'PlotFcns',@gaplotbestf);
options=gaoptimset(options,'Vectorize','on');%目标函数向量化
[x,fval,exitflag,output,final_pop,scores]=ga(@ga_curfit,nvars,[],[],[],[],LB,UB,[],options);
t=1:n;
plot(t,ydata,'r*');
hold on
plot(t,x(1)+x(2)*sin(x(3)*t+x(4)))
legend('数据','拟合')
更多详情可以参考我在matlab中文论坛回的贴
再问: 不知道目标函数,用遗传算法进行交叉变异应该怎么办呢?
再答: 你想自己写遗传算法呀,这难度还真大,matlab自带的很好的为啥不用了。 不管是什么拟合,除了黑箱模型,其他都得知道目标函数,像最小二乘可以用多项式等等 那你用多项式的形式作为目标函数试试。
已知数据队列 buf=【5410.】
x取值 1:n n是队列长度
函数 f(x)=a+b*sin(c*x+d) .
avg 是队列平均值
a b c d 为参数 a范围 (2/3,1)*avg
b范围 (0,1/3)*avg
c的范围 (0,24*pi)
d (0,2*pi)
1、首先定义目标函数
function y=ga_curfit(x)
global ydata n
t=1:n;
y=0;
for i=1:n
y=y+(ydata(i)-(x(:,1)+x(:,2).*sin(x(:,3).*t(i)+x(:,4)))).^2/n;
end
y=sqrt(y);
end
2、把数据b.txt放在工作空间目录中
然后再命令窗口中输入
clear
global ydata n
format long g
load b.txt
ydata=b';
n=length(ydata);
avg=sum(ydata)/n;
LB=[2/3*avg 0 0 0];
UB=[1*avg 1/3*avg 24*pi 2*pi];
nvars=4;
options=gaoptimset;
options=gaoptimset(options,'PopulationSize',300);
options=gaoptimset(options,'CrossoverFraction',0.8);
options=gaoptimset(options,'MigrationFraction',0.1);
options=gaoptimset(options,'Generations',500);
options = gaoptimset(options,'TolFun',1e-50);
%options = gaoptimset(options,'InitialPopulation',final_pop);
options = gaoptimset(options,'Display','final');
options = gaoptimset(options,'PopInitRange',[LB;UB]);
options = gaoptimset(options,'PlotFcns',@gaplotbestf);
options=gaoptimset(options,'Vectorize','on');%目标函数向量化
[x,fval,exitflag,output,final_pop,scores]=ga(@ga_curfit,nvars,[],[],[],[],LB,UB,[],options);
t=1:n;
plot(t,ydata,'r*');
hold on
plot(t,x(1)+x(2)*sin(x(3)*t+x(4)))
legend('数据','拟合')
更多详情可以参考我在matlab中文论坛回的贴
再问: 不知道目标函数,用遗传算法进行交叉变异应该怎么办呢?
再答: 你想自己写遗传算法呀,这难度还真大,matlab自带的很好的为啥不用了。 不管是什么拟合,除了黑箱模型,其他都得知道目标函数,像最小二乘可以用多项式等等 那你用多项式的形式作为目标函数试试。
用matlab遗传算法拟合曲线怎么拟合? 两组数据,必须用遗传算法,最好有程序或者详细的过程,急
matlab的遗传算法程序
用Matlab写拟合曲线的程序
matlab遗传算法工具箱里用的是哪种遗传算法
matlab程序请教这个运算过程如何通过matlab用遗传算法的思路实现,
请问怎么不使用matlab的遗传算法工具箱用matlab遗传算法求极值?
请高手将下面的遗传算法的matlab程序用并行算法实现(实现一个即可) 遗传算法程序:说明:fga.m 为遗传算
matlab 遗传算法工具箱怎么用?
遗传算法求最短路径的matlab程序,
求遗传算法的matlab程序
matlab遗传算法程序出错
matlab遗传算法程序解读?