matlab:出现错误Function definitions are not permitted in this context怎么回事?坐标轮换法的matlab程序[转]用坐标轮换法求最优解:y=x1^2+x2^2-x1*x2-10*x1-4*x2+60的最优解,初始点为:[0;0],精度为0.01%总程序x0=[0,0]

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/30 19:58:21
matlab:出现错误Function definitions are not permitted in this context怎么回事?坐标轮换法的matlab程序[转]用坐标轮换法求最优解:y=x1^2+x2^2-x1*x2-10*x1-4*x2+60的最优解,初始点为:[0;0],精度为0.01%总程序x0=[0,0]

matlab:出现错误Function definitions are not permitted in this context怎么回事?坐标轮换法的matlab程序[转]用坐标轮换法求最优解:y=x1^2+x2^2-x1*x2-10*x1-4*x2+60的最优解,初始点为:[0;0],精度为0.01%总程序x0=[0,0]
matlab:出现错误Function definitions are not permitted in this context怎么回事?
坐标轮换法的matlab程序[转]用坐标轮换法求最优解:y=x1^2+x2^2-x1*x2-10*x1-4*x2+60的最优解,初始点为:[0;0],精度为0.01
%总程序
x0=[0,0];
x=getx(x0);
s0=gets(x,x0);
s=s0;
while s>0.01
x0=x;
s0=s;
x=getx(x0);
s=gets(x,x0);
end
fprintf('最优解为x1=%d,x2=%d,目标函数f=%d',x0(1),x0(2),getf(x0));
function x=getx(x0)
%--------------------------------------------------
%用成功--失败法求一维搜索的最小值
%--------------------------------------------------
%计算第一个变量x1方向
h=3;
x1=x0;
f0=getf(x0);
while abs(h)>1e-5
x=[x1(1)+h,x1(2)];
f=getf(x);
if f<f0
h=h*2;x1=x;f0=f;
else
h=-h/4;
end
end
%计算第二个变量x2方向
h=3;
x2=x1;
f1=getf(x2);
while abs(h)>1e-5
x=[x2(1),x2(2)+h];
f=getf(x);
if f<f1
h=h*2;x2=x;f1=f;
else
h=-h/4;
end
end
%返回结果
x=x2;
function s=gets(x,x0)
%------------------------------
%计算最近最优解和上一个最优解的距离
%------------------------------
s=sqrt((x(1)-x0(1))^2+(x(2)-x0(2))^2);
end
function f=getf(x);
%------------------------------
%计算函数值
%------------------------------
x1=x(1);
x2=x(2);
f=x1^2+x2^2-x1*x2-10*x1-4*x2+60;
end
这个第15行出现错误,..

matlab:出现错误Function definitions are not permitted in this context怎么回事?坐标轮换法的matlab程序[转]用坐标轮换法求最优解:y=x1^2+x2^2-x1*x2-10*x1-4*x2+60的最优解,初始点为:[0;0],精度为0.01%总程序x0=[0,0]
把函数写到另一个文件里就行了~