老师参与

关于非线性拟合设置不显示中间结果

澄碧钓徒 发表于2018年04月09日
<p>非线性方程求解跟最优化问题不显示中间结果都是调用optimset函数,将Display选项设为off,怎么以同样的方法用在lsqcurvefit和lsqnonlin的命令中就报错了?<br ></p><p>&gt;&gt; f=@(x,tdata)x(1)+x(2)*exp(-0.02*x(3)*tdata);</p><p>tdata=100:100:1000</p><p>cdata=1e-03*[4.54,4.99,5.35,5.65,5.90,6.10,6.26,6.39,6.50,6.59];</p><p>x0=[0.2,0.05,0.05];</p><p>option=optimset('Display','off');</p><p>x=lsqcurvefit(f,x0,tdata,cdata,option)</p><p><br ></p><p>tdata =</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;100 &nbsp; &nbsp; &nbsp; &nbsp; 200 &nbsp; &nbsp; &nbsp; &nbsp; 300 &nbsp; &nbsp; &nbsp; &nbsp; 400 &nbsp; &nbsp; &nbsp; &nbsp; 500 &nbsp; &nbsp; &nbsp; &nbsp; 600 &nbsp; &nbsp; &nbsp; &nbsp; 700 &nbsp; &nbsp; &nbsp; &nbsp; 800 &nbsp; &nbsp; &nbsp; &nbsp; 900 &nbsp; &nbsp; &nbsp; &nbsp;1000</p><p>错误使用 lsqcurvefit (line 185)</p><p>LSQCURVEFIT requires the following inputs to be of data type double: 'LB'.</p><p>&nbsp;</p><p>&gt;&gt; f=@(x)x(1)+x(2)*exp(-0.02*x(3)*tdata)-cdata</p><p>tdata=100:100:1000</p><p>cdata=1e-03*[4.54,4.99,5.35,5.65,5.90,6.10,6.26,6.39,6.50,6.59];</p><p>x0=[0.2,0.05,0.05];</p><p>x=lsqnonlin(f,x0,optimset('Display','off'))</p><p><br ></p><p>f =</p><p>&nbsp; 包含以下值的 function_handle:</p><p>&nbsp; &nbsp; @(x)x(1)+x(2)*exp(-0.02*x(3)*tdata)-cdata</p><p>tdata =</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;100 &nbsp; &nbsp; &nbsp; &nbsp; 200 &nbsp; &nbsp; &nbsp; &nbsp; 300 &nbsp; &nbsp; &nbsp; &nbsp; 400 &nbsp; &nbsp; &nbsp; &nbsp; 500 &nbsp; &nbsp; &nbsp; &nbsp; 600 &nbsp; &nbsp; &nbsp; &nbsp; 700 &nbsp; &nbsp; &nbsp; &nbsp; 800 &nbsp; &nbsp; &nbsp; &nbsp; 900 &nbsp; &nbsp; &nbsp; &nbsp;1000</p><p>错误使用 lsqnonlin (line 180)</p><p>LSQNONLIN requires the following inputs to be of data type double: 'LB'.</p><p>另外,不带option运行出来的结果和PPT上的不一样是怎么回事?保险起见数据都是从PPT上粘贴过去的:</p><p>&gt;&gt; f=@(x,tdata)x(1)+x(2)*exp(-0.02*x(3)*tdata);</p><p>tdata=100:100:1000</p><p>cdata=1e-03*[4.54,4.99,5.35,5.65,5.90,6.10,6.26,6.39,6.50,6.59];</p><p>x0=[0.2,0.05,0.05];</p><p>x=lsqcurvefit(f,x0,tdata,cdata)</p><p><br ></p><p>x =</p><p>&nbsp; &nbsp; 0.0069 &nbsp; -0.0029 &nbsp; &nbsp;0.0809</p><p><br ></p><p>&gt;&gt; f=@(x)x(1)+x(2)*exp(-0.02*x(3)*tdata)-cdata</p><p>tdata=100:100:1000</p><p>cdata=1e-03*[4.54,4.99,5.35,5.65,5.90,6.10,6.26,6.39,6.50,6.59];</p><p>x0=[0.2,0.05,0.05];</p><p>x=lsqnonlin(f,x0)</p><p><br ></p><p>x =</p><p>&nbsp; &nbsp; 0.0069 &nbsp; -0.0029 &nbsp; &nbsp;0.0809</p>
2 回复

    1楼

  • 龚劬 发表于2018年04月09日
    1 | 1 | 举报
    <p>请注意lsqcurvefit的调用格式,X = lsqcurvefit(FUN,X0,XDATA,YDATA,LB,UB,OPTIONS) ,第5输入是x的下界,options是第7输入。同样lsqnonlin的调用格式为:X = lsqnonlin(FUN,X0,LB,UB,OPTIONS) 。</p><p>lsqcurvefit和lsqnonlin的输入修改一下就可以了,如下</p><p>&gt;&gt;f=@(x,tdata)x(1)+x(2)*exp(-0.02*x(3)*tdata);</p><p>tdata=100:100:1000;</p><p>cdata=1e-03*[4.54,4.99,5.35,5.65,5.90,6.10,6.26,6.39,6.50,6.59];</p><p>x0=[0.2,0.05,0.05];</p><p>option=optimset('Display','off');</p><p>x=lsqcurvefit(f,x0,tdata,cdata,[],[],option)</p><p><br ></p><p>x =</p><p>&nbsp; &nbsp; 0.0069 &nbsp; -0.0029 &nbsp; &nbsp;0.0809</p><p><br ></p><p>&gt;&gt; f=@(x)x(1)+x(2)*exp(-0.02*x(3)*tdata)-cdata;</p><p>tdata=100:100:1000;</p><p>cdata=1e-03*[4.54,4.99,5.35,5.65,5.90,6.10,6.26,6.39,6.50,6.59];</p><p>x0=[0.2,0.05,0.05];</p><p>x=lsqnonlin(f,x0,[],[],optimset('Display','off'))</p><p><br ></p><p>x =</p><p>&nbsp; &nbsp; 0.0069 &nbsp; -0.0029 &nbsp; &nbsp;0.0809</p><p><br ></p><p>你的运行结果和PPT不同,是因为两者使用的MATLAB版本不同,不同版本这些函数采用的方法也可能会不同,求解非线性规划的方法很多。</p><p><br ></p>
    龚劬 发表于2018年04月09日
    • 龚劬 2018年04月11日
      0 | 举报
      <p>现在该讲义的版本已经换为 MATLAB 2016b版本的了。</p>
      龚劬 发表于2018年04月11日
      0 | 举报
    添加评论
  • 2楼

  • 重庆大学何博 发表于2018年04月10日
    1 | 0 | 举报
    <p>运行结果确实和ppt&nbsp;不一样,但是PPT上的代码是可以运行出来的。<br >这上面的代码报错是因为</p><p><img src="//nos.netease.com/edu-image/756cf268-93d7-47b0-b7c2-a58a7281cecf.png" /></p><p>&nbsp;应该是一个和x一样大小的向量或矩阵,如果要用options的话,前面还有两个输入参数<br /></p><p>&nbsp;x=lsqcurvefit(f,x0,tdata,cdata,[],[],option);<br />这样就对了&nbsp;</p>
    重庆大学何博 发表于2018年04月10日
    添加评论