作业帮 > 综合 > 作业

如何用matlab进行多元非线性拟合

来源:学生作业帮 编辑:神马作文网作业帮 分类:综合作业 时间:2024/11/10 11:26:55
如何用matlab进行多元非线性拟合
自定义函数
y=A*x1/(1+B*x2+C*x3)+D
其中
x1=[0.25,0.278,0.3125,0.357,0.4167,0.3,0.3333,0.375,0.7286,0.5];
x2=[0,0.1111,0.125,0.14286,0.1667,0,0.1111,0.125,0.14286,0.1667];
x3=[0,0,0.125,0.2857,0.5,0,0,0.125,0.2857,0.5];
y=[0.7572,0.6559,0.6383,0.5636,0.4884,0.9783,0.7489,0.686,0.6288,0.4934];
求可执行代码!求出回归系数A、B、C、D!
如何用matlab进行多元非线性拟合
function zd487022570
x1=[0.25,0.278,0.3125,0.357,0.4167,0.3,0.3333,0.375,0.7286,0.5];
x2=[0,0.1111,0.125,0.14286,0.1667,0,0.1111,0.125,0.14286,0.1667];
x3=[0,0,0.125,0.2857,0.5,0,0,0.125,0.2857,0.5];
y=[0.7572,0.6559,0.6383,0.5636,0.4884,0.9783,0.7489,0.686,0.6288,0.4934];
x = [x1; x2; x3];
% 使用最小二乘拟合:
% opt指定拟合选项(注意查看命令窗口提示的优化终止条件,如对结果不满意考虑适当修改)
% b0为初值(要慎重选择,不同初值得到的结果可能不同)
opt = optimset('MaxFunEvals',50000,'MaxIter',10000,'TolFun',1E-10)
% b0 = [0 -10 1 1];
% b0 = [0.6 -60 15 0.7];
b0 = [0.7 -137 40 0.7];
b = lsqcurvefit(@myfun,b0,x,y,[],[],opt);
A = b(1)
B = b(2)
C = b(3)
D = b(4)
% 把拟合结果与原始数据对照
plot(y,'-o');
hold on
plot(myfun(b,x),'r:x');
function y = myfun(x,xdata)
A = x(1);
B = x(2);
C = x(3);
D = x(4);
x1 = xdata(1,:);
x2 = xdata(2,:);
x3 = xdata(3,:);
y = A*x1./(1+B*x2+C*x3)+D;
再问: 运行报错了 ??? function zd487022570 | Error: Function definitions are not permitted in this context. 看不懂function zd487022570这是什么意思……
再答: 你是不是文件里还加了别的东西? 把我上面贴的代码直接贴到新建的文件中,然后保存就行,不可能有错。我在6.5和R2007b两个版本上测试过。 最上面的“function zd487022570”是因为调用lsqcurvefit需要把拟合公式写成函数,而这种问题用inline函数或匿名函数都不太方便,所以使用了function。而在一个文件中如果有function就不能是script,所以就把前面的代码取个名字也成为一个function。函数的名字就用的是你提问这个网页的号码加了两个字母的前缀,当然,你可以把它改成其他任何合法的标识符。