你好,请教一下matlab coder相关问题 调用了曲线拟合函数lsqcurvefit不能被编译器编译,希望不吝赐教

2025-03-23 06:09:05
推荐回答(1个)
回答1:

我只是将读取数据改成随机生成20个,结果
function quxiannihe
z=rand(20,1);
aa=size(z,1);
bb=0.001*aa;
cc=0.001:0.001:bb;
x=cc';
y=z;
g=@(a,x)(a(1).*sin(a(2).*x+a(3))+a(4));
a0=[0.012,250,-2,511];
a=lsqcurvefit(g,a0,x,y);
picture=plot(x,y,'k.',x,g(a,x),'r');
set(picture,'LineWidth',1.5);
legend('原始数据','拟合数据');
print(1,'-djpeg','C:\Users\Administrator\Desktop\拟合图像1.jpeg');
disp(a)
结果只是有警告,拟合效果不是很好,循环400次后,只是说效果不好(早熟),但是有值。说明程序没有问题。

Solver stopped prematurely.

lsqcurvefit stopped because it exceeded the function evaluation limit,
options.MaxFunEvals = 400 (the default value).

-0.6868 -12.1817 28.8048 0.2286

所以只可能是读取出错。看你的代码应该是下面第一种,我建议用第二种方法试一下(因为我不确定'M:M'是不是会智能到没有数字的时候结束,我一般都是指定从哪个单元格到哪个单元格),指明是第几个sheet。如果还不行,打开excel看看数据对不对。
num = xlsread(filename,xlRange)
num = xlsread(filename,sheet,xlRange)