yyphin 发表于 2012-4-14 14:53:04

求助:曲线拟合的问题

M文件:
function f=examfun(p,x)
f=p(1)*exp(-((x-p(2))/p(3))^2) + p(4)*exp(-((x-p(5))/p(6))^2)+p(7)*exp(-((x-p(8))/p(9))^2) + p(10)*exp(-((x-p(11))/p(12))^2)+p(13)*exp(-((x-p(14))/p(15))^2);
程序入口:
x0=
xdata=[...](略)
ydata=[...]
fun=@examfun
p=lsqcurvefit(fun,x0,xdata,ydata)

错误提示:
??? Error using ==> optim\private\lsqncommon
User supplied function ==> examfun
failed with the following error:
Error using ==> mpower
Matrix must be square.
Error in ==> lsqcurvefit at 165
= ...

请问:我的代码哪里出错了?应该怎么修改?谢谢!

mhkmars 发表于 2012-4-14 19:52:03

用点乘试试

wyzheng352 发表于 2012-4-14 20:43:03

至少要给个数据啊。
function f=examfun(p,x)
f=p(1)*exp(-((x-p(2))/p(3)).^2) + p(4)*exp(-((x-p(5))/p(6)).^2)+...
   p(7)*exp(-((x-p(8))/p(9)).^2) + p(10)*exp(-((x-p(11))/p(12)).^2)+...
   p(13)*exp(-((x-p(14))/p(15)).^2);
函数定义中变量之间要用点乘或者点除
clear;clc;
x0=;
xdata=0:0.01:2*pi;
ydata=sin(xdata);
fun=@examfun
p=lsqcurvefit(fun,x0,xdata,ydata)
我给xdata ydata任意赋下值
??? Error using ==> snls at 259
lsqcurvefit cannot continue: user function is returning Inf or NaN values.

Error in ==> lsqncommon at 149
    =...

Error in ==> lsqcurvefit at 258
= ...

其他我就找不出来了
页: [1]
查看完整版本: 求助:曲线拟合的问题