数学建模社区-数学中国

标题: 关于利用matlab非线性拟合的简单问题,求解,毕设急用,在线等 [打印本页]

作者: kuanglei    时间: 2014-5-21 00:42
标题: 关于利用matlab非线性拟合的简单问题,求解,毕设急用,在线等
【培训通知】2013年MCM优秀论文精读培训
中国数模界最第一大专业科技
www.madio.net)数学中国
数学建模思想方法大全精讲专题
第数学建模思想方法大全精讲专题
数学建模精品资源课程系列
国内专业的数学建模实训

如题,利用lsqcurvefit和lsqnonlin两个函数进行非线性拟合,总是提前收敛,求大神解释为什么?是否是更改里面的options选项,具体怎么改?在线等,万谢。。。
1、程序代码如下:
1)编写M-文件 curvefun1.m
function f=curvefun1(x,tdata)
f=1-exp(-6.908*(((tdata-x(1))./x(2)).^x(3)));
%其中 x(1)=cb; x(2)=cb-cc;x(3)=m+1;
2)输入命令
tdata=[………];
ydata=[………];
x0=[-10,55,2];
[x,resnorm] =lsqcurvefit('curvefun1',x0,tdata,ydata)
f= curvefun1(x,tdata);
3) 结果
Local minimum possible.
lsqcurvefit stopped because the finalchange in the sum of squares relative to its initial value is less than the defaultvalue of the function tolerance.
x =
Columns 1 through 2
-3.59020088832205 - 0.62244270778384i 53.9542595923814 + 0.14425192684233i
Column 3
2.69114674932468 + 0.0818892331391404i
resnorm =
0.00627209364358831
2、函数介绍如下:
1)函数 lsqcurvefit
2)格式 x = lsqcurvefit(fun,x0,xdata,ydata)
x =lsqcurvefit(fun,x0,xdata,ydata,lb,ub)
x =lsqcurvefit(fun,x0,xdata,ydata,lb,ub,options)
[x,resnorm] = lsqcurvefit(…)
[x,resnorm,residual] = lsqcurvefit(…)
[x,resnorm,residual,exitflag] =lsqcurvefit(…)
[x,resnorm,residual,exitflag,output]= lsqcurvefit(…)
[x,resnorm,residual,exitflag,output,lambda]= lsqcurvefit(…)
[x,resnorm,residual,exitflag,output,lambda,jacobian]=lsqcurvefit(…)
3)参数说明:
x0为初始解向量;xdata,ydata为满足关系ydata=F(x, xdata)的数据;
lb、ub为解向量的下界和上界,若没有指定界,则lb=[ ],ub=[ ];
options为指定的优化参数;
fun为拟合函数,其定义方式为:x = lsqcurvefit(@myfun,x0,xdata,ydata),
其中myfun已定义为 function F = myfun(x,xdata)
F = … % 计算x处拟合函数值fun的用法与前面相同;
resnorm=sum ((fun(x,xdata)-ydata).^2),即在x处残差的平方和;
residual=fun(x,xdata)-ydata,即在x处的残差;
exitflag为终止迭代的条件;
output为输出的优化信息;
lambda为解x处的Lagrange乘子;
jacobian为解x处拟合函数fun的jacobian矩阵。

作者: 平凡之不凡    时间: 2014-5-21 14:23

lsqnonlin函数用来求平方和形式的函数的最小值,lsqcurvefit函数用来求解目标函数的最小二乘解,其目标函数就是做数据拟合时构造的平方和形式的目标函数,lsqcurvefit函数和lsqnonlin函数采用相同的算法,只是lsqcurvefit函数可以直接做数据拟合,而用lsqnonlin函数做数据拟合的话还需要自己构造目标函数 .
作者: 平凡之不凡    时间: 2014-5-21 14:23
  Examples
      FUN can be specified using @:
         xdata = [5;4;6];          % example xdata
         ydata = 3*sin([5;4;6])+6; % example ydata
         x = lsqcurvefit(@myfun, [2 7], xdata, ydata)

    where myfun is a MATLAB function such as:

        function F = myfun(x,xdata)
        F = x(1)*sin(xdata)+x(2);

    FUN can also be an anonymous function:
        x = lsqcurvefit(@(x,xdata) x(1)*sin(xdata)+x(2),[2 7],xdata,ydata)

    If FUN is parameterized, you can use anonymous functions to capture the
    problem-dependent parameters. Suppose you want to solve the
    curve-fitting problem given in the function myfun, which is
    parameterized by its second argument c. Here myfun is a MATLAB file
    function such as

        function F = myfun(x,xdata,c)
        F = x(1)*exp(c*xdata)+x(2);

    To solve the curve-fitting problem for a specific value of c, first
    assign the value to c. Then create a two-argument anonymous function
    that captures that value of c and calls myfun with three arguments.
    Finally, pass this anonymous function to lsqcurvefit:

        xdata = [3; 1; 4];           % example xdata
        ydata = 6*exp(-1.5*xdata)+3; % example ydata     
        c = -1.5;                    % define parameter
        x = lsqcurvefit(@(x,xdata) myfun(x,xdata,c),[5;1],xdata,ydata)
作者: 平凡之不凡    时间: 2014-5-21 14:24
Examples
      FUN can be specified using @:
         x = lsqnonlin(@myfun,[2 3 4])

    where myfun is a MATLAB function such as:

        function F = myfun(x)
        F = sin(x);

    FUN can also be an anonymous function:

        x = lsqnonlin(@(x) sin(3*x),[1 4])

    If FUN is parameterized, you can use anonymous functions to capture the
    problem-dependent parameters. Suppose you want to solve the non-linear
    least squares problem given in the function myfun, which is
    parameterized by its second argument c. Here myfun is a MATLAB file
    function such as

        function F = myfun(x,c)
        F = [ 2*x(1) - exp(c*x(1))
              -x(1) - exp(c*x(2))
              x(1) - x(2) ];

    To solve the least squares problem for a specific value of c, first
    assign the value to c. Then create a one-argument anonymous function
    that captures that value of c and calls myfun with two arguments.
    Finally, pass this anonymous function to lsqnonlin:

        c = -1; % define parameter first
        x = lsqnonlin(@(x) myfun(x,c),[1;1])
作者: kuanglei    时间: 2014-5-21 17:24
平凡之不凡 发表于 2014-5-21 14:24
Examples
      FUN can be specified using @:
         x = lsqnonlin(@myfun,[2 3 4])

您说的我知道,函数形式,数据点我都有,程序代码也没错,只是它提前收敛了,没有运行出最优解(我的是实际问题,它的结果居然是复数),我想知道怎么调才能让它运行出正确结果。函数调用有个options选项,我不会用,不知道是不是可以在里面调。
我是新手,没怎么逛过数学中国的论坛,只因为之前参加过数模比赛,接触过matlab并且知道数模大神多,才来这里发帖求助。
另外,我的英语很龊,能加QQ单独质询吗?我的QQ894407008,万谢。。。。
作者: kuanglei    时间: 2014-5-21 17:25
来大神啊。。。。
作者: wujianjack2    时间: 2014-5-21 19:17
   看你的拟合方程式非线性程度很高啊!如果实在要得到答案的话尝试下1stOpt吧!




欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5