qjjqh
2009-09-30, 09:36
在编写一个模拟退火算法时,有一个函数调用时报错如下"??? Attempt to execute SCRIPT an11 as a function.
Error in ==> sa_0930 at 6
f1=an11(x1);"
两函数如下:
这是an11.m函数
functon y=aneal_f1(x)
y=100*(x(1)^2-x(2))^2+(1-x(1))^2;
下面是主函数
x1=[1 1];
T=10000;
a=0.9;
N=1;
while T>1
f1=an11(x1);
x2(1)=x1(1)+0.2*(rand-0.5);
x2(2)=x1(2)+0.2*(rand-0.5);
f2=an11(x2);
if f2-f1<0
T=T*a;
x1=x2;
cc(N)=f2;
N=N+1;
elseif exp((f1-f2)/T)>rand
T=T*a;
x1=x2;
cc(N)=f2;
N=N+1;
end
end
f1=an11(x1);
disp('The result are:')
disp('x(1) x(2)')
disp(x1)
disp('The objective fuction is:')
disp(f1)
tt=1:N-1;
plot(tt,cc)
time1=datestr(now)
mm=1;
while mm<=1000
aneal_f1;
opf(mm)=f1;
mm=mm+1;
end
time2=datestr(now)
min(opf)
max(opf)
mean(opf)
Error in ==> sa_0930 at 6
f1=an11(x1);"
两函数如下:
这是an11.m函数
functon y=aneal_f1(x)
y=100*(x(1)^2-x(2))^2+(1-x(1))^2;
下面是主函数
x1=[1 1];
T=10000;
a=0.9;
N=1;
while T>1
f1=an11(x1);
x2(1)=x1(1)+0.2*(rand-0.5);
x2(2)=x1(2)+0.2*(rand-0.5);
f2=an11(x2);
if f2-f1<0
T=T*a;
x1=x2;
cc(N)=f2;
N=N+1;
elseif exp((f1-f2)/T)>rand
T=T*a;
x1=x2;
cc(N)=f2;
N=N+1;
end
end
f1=an11(x1);
disp('The result are:')
disp('x(1) x(2)')
disp(x1)
disp('The objective fuction is:')
disp(f1)
tt=1:N-1;
plot(tt,cc)
time1=datestr(now)
mm=1;
while mm<=1000
aneal_f1;
opf(mm)=f1;
mm=mm+1;
end
time2=datestr(now)
min(opf)
max(opf)
mean(opf)