matlab求解优化问题 采用非线性最小二乘法 lsqnonlin函数 编程遇到问题
求解一个优化问题采用lsqnonlin函数编写的一个函数m文件如下:functionF=myfun3(a)i=1:5;mi=[0.0686,1.0714,0.0714,3...
求解一个优化问题 采用lsqnonlin函数
编写的一个函数m文件如下:
function F=myfun3(a)
i=1:5;
mi=[0.0686,1.0714,0.0714,3.2445,-0.0798];
syms x;
t=a(1)*x+a(2)*x^2+a(3)*x^3+a(4)*x^4+a(5)*x^5;
f=(1+t+t^2/2+t^3/6+t^4/24+t^5/120);
F=1-int(x.^i*f,x,-3,3)/mi*(int(f,x,-3,3));
然后再命令窗口输入 如下命令:
>> a0=[0,0,0,0,0];
>> [a,resnorm]=lsqnonlin(@myfun3,a0)
然后运行
结果出现一堆看不懂的问题:
好几个warning 如 Warning: System is inconsistent. Solution does not exist.
> In sym.mldivide at 32
In sym.mrdivide at 27
In myfun3 at 7
In optim\private\lsqncommon at 88
In lsqnonlin at 163
还有如下错误 ??? Function 'lt' is not defined for values of class 'sym'.
Error in ==> optim\private\nlsq at 329 if (GradF'*SD) < tolFun && ...
Error in ==> optim\private\lsqncommon at 216 [x,FVAL,JACOB,EXITFLAG,OUTPUT,msg] = ...
Error in ==> lsqnonlin at 163[x,Resnorm,FVAL,EXITFLAG,OUTPUT,LAMBDA,JACOB] = ...
matlab不是很懂,麻烦高人看看我的程序怎么改,谢谢 展开
编写的一个函数m文件如下:
function F=myfun3(a)
i=1:5;
mi=[0.0686,1.0714,0.0714,3.2445,-0.0798];
syms x;
t=a(1)*x+a(2)*x^2+a(3)*x^3+a(4)*x^4+a(5)*x^5;
f=(1+t+t^2/2+t^3/6+t^4/24+t^5/120);
F=1-int(x.^i*f,x,-3,3)/mi*(int(f,x,-3,3));
然后再命令窗口输入 如下命令:
>> a0=[0,0,0,0,0];
>> [a,resnorm]=lsqnonlin(@myfun3,a0)
然后运行
结果出现一堆看不懂的问题:
好几个warning 如 Warning: System is inconsistent. Solution does not exist.
> In sym.mldivide at 32
In sym.mrdivide at 27
In myfun3 at 7
In optim\private\lsqncommon at 88
In lsqnonlin at 163
还有如下错误 ??? Function 'lt' is not defined for values of class 'sym'.
Error in ==> optim\private\nlsq at 329 if (GradF'*SD) < tolFun && ...
Error in ==> optim\private\lsqncommon at 216 [x,FVAL,JACOB,EXITFLAG,OUTPUT,msg] = ...
Error in ==> lsqnonlin at 163[x,Resnorm,FVAL,EXITFLAG,OUTPUT,LAMBDA,JACOB] = ...
matlab不是很懂,麻烦高人看看我的程序怎么改,谢谢 展开
1个回答
展开全部
这是我修改以后的程序
%%%%%%%%%
function F = myfun3(x)
p=1:5;
m=[0.0686,1.0714,0.0714,3.2445,-0.0798];
syms k t f;
t=x(1)*k+x(2)*k^2+x(3)*k^3+x(4)*k^4+x(5)*k^5;
f=1+t+t^2/2+t^3/6+t^4/24+t^5/120;
F=1-eval(int(k.^p*f,k,-3,3))./m(p)*(eval(int(f,k,-3,3)));
end
%%保存为M文件
%%%%%%%%%
%%命令窗口中输入
clc
tic
x0=[0,0,0,0,0];
[x,resnorm]=lsqnonlin(@myfun3,x0)
toc
%%结果为
x =
0.0493 -0.9723 -0.0156 0.0932 0.0012
resnorm =
2.4422e-027
Elapsed time is 123.737320 seconds.
%%程序运行了123秒啊,我还是第一次见过这么长的运算时间!
%%%%%%%%%
function F = myfun3(x)
p=1:5;
m=[0.0686,1.0714,0.0714,3.2445,-0.0798];
syms k t f;
t=x(1)*k+x(2)*k^2+x(3)*k^3+x(4)*k^4+x(5)*k^5;
f=1+t+t^2/2+t^3/6+t^4/24+t^5/120;
F=1-eval(int(k.^p*f,k,-3,3))./m(p)*(eval(int(f,k,-3,3)));
end
%%保存为M文件
%%%%%%%%%
%%命令窗口中输入
clc
tic
x0=[0,0,0,0,0];
[x,resnorm]=lsqnonlin(@myfun3,x0)
toc
%%结果为
x =
0.0493 -0.9723 -0.0156 0.0932 0.0012
resnorm =
2.4422e-027
Elapsed time is 123.737320 seconds.
%%程序运行了123秒啊,我还是第一次见过这么长的运算时间!
追问
首先十分感谢你的关注!但我运行的结果和你的不一样,我的结果如下:
>> x0=[0,0,0,0,0];
>> [x,resnorm]=lsqnonlin(@myfun3,x0)
toc
Maximum number of function evaluations exceeded;
increase options.MaxFunEvals
x =
0.0007 -0.3921 0.0002 0.0096 -0.0000
resnorm =
20.0631
我感觉我运行的结果不对,resnorm太大了,请问怎么回事?分给你了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询