简体   繁体   English

levenberg marquardt曲线拟合MATLAB

[英]levenberg marquardt curve fitting MATLAB

enter image description hereI don't know how choose the lb and ub for lsqcurvefit in MATLAB , as well as x0 , to fit my function to data, I mean I have some output but they are not correct, 在这里输入图像描述我不知道如何在MATLAB中为lsqcurvefit选择lbub ,以及x0 ,以使我的函数适合数据,我的意思是我有一些输出,但它们不正确,

Here is my data: 这是我的数据:

xdata= [22.8700000000000;7.92000000000000;3.45000000000000;1.78000000000000;
        1.57000000000000;6.41000000000000;12.9000000000000;1.82000000000000;
        1.86000000000000;3.71000000000000;12.0900000000000;15.9900000000000;
        18.9600000000000;23.1500000000000;23.4500000000000;24.8200000000000;
        25.0700000000000;13.2800000000000];
ydata= [8.44300000000000;7.92100000000000;7.64600000000000;7.51600000000000;
        7.47100000000000;7.82100000000000;8.03200000000000;7.76200000000000;
        7.77400000000000;7.87800000000000;8.07000000000000;8.26000000000000;
        8.40000000000000;8.52000000000000;8.52000000000000;8.57000000000000;
        8.58000000000000;8.03200000000000];

and then I will have myfunc in a separate m file: 然后我将myfunc放在一个单独的m文件中:

 function F = myfun(x,xdata)
  F=x(1)*(1-x(2)^2)./((1+x(2)^2+2*x(2)*cosd(xdata)).^1.5);

I have x(1) and x(2) , unknown which I like to estimate after fitting to my data, and I know that k x(2) will not be a negative value. 我有x(1)x(2) ,我想在拟合数据后估计它是未知的,我知道k x(2)不会是负值。

So I set lsqcurvefit like this: 所以我像这样设置lsqcurvefit

[x, resnorm]=lsqcurvefit(@myfun,[-0.5:0.5], xdata, ydata, 0, 1.5, options)

And this is the result: 这就是结果:

x = 1.5000 -0.4945 x = 1.5000 -0.4945
resnorm = 52.1739 resnorm = 52.1739

which shows a negative value for x(2) ! 它显示x(2)的负值!

could you please help me? 请你帮助我好吗?

Thanks a lot for answer my question, and now after the command calculated the x and resnorm, I use the results in my function it means that I used x(1)=92.8054 x(2)=0.7427 非常感谢你回答我的问题,现在在命令计算出x和resnorm之后,我在我的函数中使用了结果,这意味着我使用了x(1)= 92.8054 x(2)= 0.7427

so; 所以;

F=92.8054*(1-(0.7427)^2)./((1-0.7427)^2+2*(0.7427)*cosd(xdata)).^1.5; F = 92.8054 *(1-(0.7427)^ 2)./((1-0.7427)^ 2 + 2 *(0.7427)* COSD(XDATA))^ 1.5。

now I have vector F , when I plot my data and results , plot(xdata, ydata, 'o', xdata, F, '*') 现在我有矢量F,当我绘制我的数据和结果时,绘图(xdata,ydata,'o',xdata,F,'*')

I don't why the range of axis y is so different! 我不知道为什么轴y的范围是如此不同! maybe I need to add x(3) to my function. 也许我需要在我的函数中添加x(3)。

I attached the figure. 我附上了这个数字。 在此输入图像描述

在此输入图像描述

在此输入图像描述

Your upper and lower boundaries must be vectors with the same number of elements as what you are trying to estimate, in your case x. 您的上边界和下边界必须是与您尝试估算的元素数量相同的向量,在您的情况下为x。

So for example if you want x(1) to be unbounded and x(2) to be between 0 and 1.5 then try 因此,例如,如果您希望x(1)无界,x(2)介于0和1.5之间,那么请尝试

[x, resnorm]=lsqcurvefit(@myfun,[-0.5:0.5], xdata, ydata, [-inf, 0], [inf, 1.5], options)

To calculate F use your objective function you have already created: 要计算F,请使用您已创建的目标函数:

F = myfun(x, xdata) and then plot it the way you already have. F = myfun(x, xdata)然后以你已有的方式绘制它。 In your comment below you have switch a + to a - , this is why your graphs aren't aligning. 在下面的评论中,您将a +切换为- ,这就是您的图表未对齐的原因。

The parameters lb and ub are the lower and upper bounds of your output, ie your optimized value xopt will satisfy lb <= xopt <= ub . 参数lbub是输出的下限和上限,即优化值xopt将满足lb <= xopt <= ub

As you already know, that x(2) cannot be negative, you already have one lower bound, which is zero, ie lb(2) = 0 . 如您所知, x(2)不能为负数,您已经有一个下限,即0,即lb(2) = 0 Now you only need to define a lower bound for x(1) and upper bounds for both x(1) and x(2) . 现在,您只需为x(1)x(2)定义x(1)和上限的下限。

The following code will restrict x(1) to [-inf, 1e3] and x(2) to [0, 1e3] : 以下代码将x(1) to [-inf, 1e3]限制x(1) to [-inf, 1e3] ,将x(2) to [0, 1e3]

lb = [-inf, 0];
ub = [1e3, 1e3];
[x, resnorm] = lsqcurvefit(@myfun,[-0.5:0.5], xdata, ydata, 0, 1.5, ...
                           lb, ub, options)

I'm also a bit puzzeled that your approach worked. 我也有点觉得你的方法很有效。 According to the documentation , you should pass empty vectors, if you do not have upper or lower bounds but want to provide options , ie your example should read 根据文档 ,您应该传递空向量,如果您没有上限或下限但想要提供options ,即您的示例应该读取

[x, resnorm] = lsqcurvefit(@myfun,[-0.5:0.5], xdata, ydata, 0, 1.5, ...
                           [], [], options)

Probably, we have different versions of Matlab. 可能我们有不同版本的Matlab。

Why not to use simple least squares solution: 为什么不使用简单的最小二乘解决方案:

in = [ones(size(xdata, 1), 1), xdata];
w = in \ ydata;
ydata_fit = in * w;

Result: 结果:

在此输入图像描述

>> disp(w)
    7.5744
    0.0401

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM