lando 发表于 2007-11-22 10:44

lsqcurvefit 运行时后出错求助

lsqcurvefit 拟合出错,怎么办呢?

m文件如下:
function y=curvefit123(beta,x)
D=beta(1);
L=beta(2);
Lg=beta(3);
h=beta(4);
tilt=beta(5);

y=1/8000000*D^3/tilt*(-2/(1-(-1+2*(x+Lg*tilt/(1+tilt^2)^(1/2))/(1+tilt^2)^(1/2)/D)^2)^(1/2)*(-1+2*(x+Lg*tilt/(1+tilt^2)^(1/2))/(1+tilt^2)^(1/2)/D)/(1+tilt^2)^(1/2)/D-2/(1+tilt^2)^(1/2)/D/(1-(-1+2*(x+Lg*tilt/(1+tilt^2)^(1/2))/(1+tilt^2)^(1/2)/D)^2)^(1/2)*(1-2*(x+Lg*tilt/(1+tilt^2)^(1/2))/(1+tilt^2)^(1/2)/D)+2*(pi-acos(-1+2*(x+Lg*tilt/(1+tilt^2)^(1/2))/(1+tilt^2)^(1/2)/D))/(1+tilt^2)^(1/2)/D+2*(1-(-1+2*(x+Lg*tilt/(1+tilt^2)^(1/2))/(1+tilt^2)^(1/2)/D)^2)^(1/2)*(-1+2*(x+Lg*tilt/(1+tilt^2)^(1/2))/(1+tilt^2)^(1/2)/D)/(1+tilt^2)^(1/2)/D+2/(1-(-1+2*(x-(L-Lg)*tilt/(1+tilt^2)^(1/2))/(1+tilt^2)^(1/2)/D)^2)^(1/2)*(-1+2*(x-(L-Lg)*tilt/(1+tilt^2)^(1/2))/(1+tilt^2)^(1/2)/D)/(1+tilt^2)^(1/2)/D+2/(1+tilt^2)^(1/2)/D/(1-(-1+2*(x-(L-Lg)*tilt/(1+tilt^2)^(1/2))/(1+tilt^2)^(1/2)/D)^2)^(1/2)*(1-2*(x-(L-Lg)*tilt/(1+tilt^2)^(1/2))/(1+tilt^2)^(1/2)/D)-2*(pi-acos(-1+2*(x-(L-Lg)*tilt/(1+tilt^2)^(1/2))/(1+tilt^2)^(1/2)/D))/(1+tilt^2)^(1/2)/D-2*(1-(-1+2*(x-(L-Lg)*tilt/(1+tilt^2)^(1/2))/(1+tilt^2)^(1/2)/D)^2)^(1/2)*(-1+2*(x-(L-Lg)*tilt/(1+tilt^2)^(1/2))/(1+tilt^2)^(1/2)/D)/(1+tilt^2)^(1/2)/D)+1/3000000*pi*h*(x+Lg*tilt/(1+tilt^2)^(1/2))/(1+tilt^2)*(3-2*(x+Lg*tilt/(1+tilt^2)^(1/2))/(1+tilt^2)^(1/2)/D)-1/3000000*pi*h*(x+Lg*tilt/(1+tilt^2)^(1/2))^2/(1+tilt^2)^(3/2)/D+1/3000000*pi*h*(x-(L-Lg)*tilt/(1+tilt^2)^(1/2))/(1+tilt^2)*(3-2*(x-(L-Lg)*tilt/(1+tilt^2)^(1/2))/(1+tilt^2)^(1/2)/D)-1/3000000*pi*h*(x-(L-Lg)*tilt/(1+tilt^2)^(1/2))^2/(1+tilt^2)^(3/2)/D;

end

那个y是对一个式子求导数得到的,这么长我也不知道怎么处理?
命令行:
>> load 1.txt;
>> load 2.txt;
>> lsqcurvefit(curvefit123, , X1, X2);
出错信息:
??? Error using ==> lsqncommon at 101
LSQCURVEFIT cannot continue because user supplied objective function failed
with the following error:
Error using ==> mpower
Matrix must be square.

Error in ==> lsqcurvefit at 182
= ...
我想大概是点乘引起的 但是不知道怎么解决。

我换成不是向量的乘法...
n= length(x)
for k=i:n
...x(k)....
Function value and YDATA sizes are incommensurate.
也不行
谢谢大家啊 ,上面那个长式子似乎也不能化简呢



[ 本帖最后由 lando 于 2007-11-22 11:35 编辑 ]

花如月 发表于 2007-11-22 11:01

按照出错的提示不是因为式子长,而是你的表达式或者用法有问题

而且既然是拟合,函数类型和走势大致一致就行了

弄这么长的式子似乎意义不大,建议你还用曲线拟合工具箱cftool

另外本版有例程,讲得很详细。自己找找看

[ 本帖最后由 花如月 于 2007-11-22 11:03 编辑 ]

lando 发表于 2007-11-22 11:05

那个式子不是想当然弄出来的 是求导啊
本身就有那么复杂,关键是那么长的表达式没有法子加点乘啊
然后 我不用向量为什么还出错呢

[ 本帖最后由 eight 于 2007-11-22 11:34 编辑 ]

lando 发表于 2007-11-22 15:30

换成标量的乘法再稍微改一下可以的
举个例子:
可以这样做:
function F= test_myfun(beta,x)
n=length(x)
for k=1:n
    f(k)= beta(1)*sin(x(k))+beta(2);
end
F=f';% Function valuesize changed
end

要是谁帮我用点乘的方法把那个长式子弄出来就好了 谢谢

[ 本帖最后由 lando 于 2007-11-22 15:33 编辑 ]
页: [1]
查看完整版本: lsqcurvefit 运行时后出错求助