MATLAB运行.m文件没有问题,但是在labview应用脚本MATLAB script 运行报错,下标索引必须为正整数类型或逻辑类型。急!
时间:10-02
整理:3721RD
点击:
%------初始格式化--------------------------------------------------
clear all;
clc;
format long;
%eq=inline('(3./(4.*pi.*a(1).*(x.^2+16).^2.5)).*(1768.7.*a(2).*(x.^2+16).^0.5.*cos(pi).*a(3).*(x.^2+16).^0.5)+(1768.7./(4.*pi.*a(4).*(x.^2+16).^0.5))','a','x');%迭代次数为4
%给定初始化条件
c1=1.4962; %学习因子1
c2=1.4962; %学习因子2
w=0.7298; %惯性权重
maxdt=1000; %最大迭代次数
D=4;%搜索空间维数
N=37;%初始化群体个数
eps=10.^(-6);%设置精度
%初始化种群的个体
for i=1:N%随机初始化位置
for j=1:D
A(i,j)=randn;%随机初始化位置
v(i,j)=randn;%随机初始化速度
end
end
%计算各个粒子的适应度,并初始化pi,pg
for i=1:N
p(i)=fitne(A(i,:),D);
y(i,:)=A(i,:);
end
pg=A(1,:);%pg为全局最优。
for i=2:N
if fitne(A(i,:),D)<fitne(pg,D)
pg=A(i,:);
end
end
%进入主要循环,按照公式依次迭代,直到满族精度要求
for t=1:maxdt
for i=1:N
v(i,:)=w.*v(i,:)+c1.*rand.*(y(i,:)-A(i,:))+c2.*rand.*(pg-A(i,:));
x(i,:)=A(i,:)+v(i,:);
if fitne(A(i,:),D)<p(i)
p(i)=fitne(A(i,:),D);
y(i,:)=A(i,:);
end
if p(i)<fitne(pg,D)
pg=y(i,:);
end
end
pbest(t)=fitne(pg,D);
end
Solution=pg'
function result=fitne(A,D)
M=1768.7;
x=[-9:0.5:9];
h=[49871.93,49871.90,49871.89,49871.82,49871.69,49871.49,49871.35,49871.32,49871.03,49870.78,49869.43,49868.44,49866.19,49865.96,49864.77,49863.55,49863.21,49862.23,49860.77,49862.26,49863.13,49864.33,49864.68,49865.76,49866.76,49868.11,49869.41,49870.52,49871.02,49871.27,49871.36,49871.51,49871.73,49871.90,49871.92,49871.94,49871.96];
H=h-49860.77;
%for l=1:D
for i=1:D
s=(3./(4.*pi.*A(i).*(x.^2+16).^2.5)).*(1768.7.*A(i).*(x.^2+16).^0.5.*cos(pi).*A(i).*(x.^2+16).^0.5)+(1768.7./(4.*pi.*A(i).*(x.^2+16).^0.5));
end
mins=sum((H-s).^2);
result=mins;
clear all;
clc;
format long;
%eq=inline('(3./(4.*pi.*a(1).*(x.^2+16).^2.5)).*(1768.7.*a(2).*(x.^2+16).^0.5.*cos(pi).*a(3).*(x.^2+16).^0.5)+(1768.7./(4.*pi.*a(4).*(x.^2+16).^0.5))','a','x');%迭代次数为4
%给定初始化条件
c1=1.4962; %学习因子1
c2=1.4962; %学习因子2
w=0.7298; %惯性权重
maxdt=1000; %最大迭代次数
D=4;%搜索空间维数
N=37;%初始化群体个数
eps=10.^(-6);%设置精度
%初始化种群的个体
for i=1:N%随机初始化位置
for j=1:D
A(i,j)=randn;%随机初始化位置
v(i,j)=randn;%随机初始化速度
end
end
%计算各个粒子的适应度,并初始化pi,pg
for i=1:N
p(i)=fitne(A(i,:),D);
y(i,:)=A(i,:);
end
pg=A(1,:);%pg为全局最优。
for i=2:N
if fitne(A(i,:),D)<fitne(pg,D)
pg=A(i,:);
end
end
%进入主要循环,按照公式依次迭代,直到满族精度要求
for t=1:maxdt
for i=1:N
v(i,:)=w.*v(i,:)+c1.*rand.*(y(i,:)-A(i,:))+c2.*rand.*(pg-A(i,:));
x(i,:)=A(i,:)+v(i,:);
if fitne(A(i,:),D)<p(i)
p(i)=fitne(A(i,:),D);
y(i,:)=A(i,:);
end
if p(i)<fitne(pg,D)
pg=y(i,:);
end
end
pbest(t)=fitne(pg,D);
end
Solution=pg'
function result=fitne(A,D)
M=1768.7;
x=[-9:0.5:9];
h=[49871.93,49871.90,49871.89,49871.82,49871.69,49871.49,49871.35,49871.32,49871.03,49870.78,49869.43,49868.44,49866.19,49865.96,49864.77,49863.55,49863.21,49862.23,49860.77,49862.26,49863.13,49864.33,49864.68,49865.76,49866.76,49868.11,49869.41,49870.52,49871.02,49871.27,49871.36,49871.51,49871.73,49871.90,49871.92,49871.94,49871.96];
H=h-49860.77;
%for l=1:D
for i=1:D
s=(3./(4.*pi.*A(i).*(x.^2+16).^2.5)).*(1768.7.*A(i).*(x.^2+16).^0.5.*cos(pi).*A(i).*(x.^2+16).^0.5)+(1768.7./(4.*pi.*A(i).*(x.^2+16).^0.5));
end
mins=sum((H-s).^2);
result=mins;
已退回10积分