MatLab中的自定义曲线拟合(Custom Curve Fitting in MatLab)

编程入门 行业动态 更新时间:2024-10-12 20:21:16
MatLab中的自定义曲线拟合(Custom Curve Fitting in MatLab)

我正在尝试在MatLab中定义自定义曲线拟合。 我想要适合的曲线如下:

A*exp(B*x)+C

需要这种拟合的原因是因为我的数据呈指数衰减,但是这个数据不接近零(在这个数据集中,曲线应该衰减并接近零以上的某个数字)。 使用fittype('exp2')也不是一个选项,因为它会过度拟合数据(当曲线在0以上保持平坦时向上弯曲)。 我使用以下代码定义拟合类型:

ft = fittype('(A*exp(B*x)) + C','coefficients', {'A', 'B', 'C'});

但是,当我尝试将数据拟合到此曲线时,我收到以下错误消息:

“使用cfit / subsref> iDotReference出错(第66行)名称既不是系数也不是问题参数。

cfit / subsref中的错误(第19行)out = iDotReference(obj,currsubs);“

在这种情况下,我无法弄清楚MatLab究竟在抱怨什么。 有时候,由于我不知道的原因,代码会运行,但适合的情况非常糟糕。 我在这里做错了吗? 这是指数衰减到某个0以上的指数的最佳方法吗?

以下代码是我如何尝试运行我的拟合:

[cf, gof] = fit(time', testArray', fittype);

I am trying to define a custom Curve Fit in MatLab. The curve which I am trying to fit to is as follows:

A*exp(B*x)+C

The reason this fit is needed is because my data decays exponentially, however this data does not approach zero (in this data set, the curve should decay and approach some number above zero). Using fittype('exp2') is also not an option because it overfits the data (curves upwards towards the end when it should remain flat somewhere above 0). I define the fit type using the following code:

ft = fittype('(A*exp(B*x)) + C','coefficients', {'A', 'B', 'C'});

However, when I try to fit data to this curve I get the following error message:

"Error using cfit/subsref>iDotReference (line 66) Name is neither a coefficient or a problem parameter.

Error in cfit/subsref (line 19) out = iDotReference( obj, currsubs );"

I can't figure out exactly what MatLab is complaining about in this case. Sometimes, for reasons I do not know, the code will run but the fit is just terrible. Am I doing something wrong here? Is this the best way to an exponential that decays to some above 0 value?

The following code is how I try to run my fit:

[cf, gof] = fit(time', testArray', fittype);

最满意答案

提供初步猜测有助于适应性很强。 我发现初始值的正确符号尤为重要。

无论如何,如果我运行此代码,我不会遇到你遇到的问题:

ft = fittype('(A*exp(B*x)) + C', 'coefficients', {'A', 'B', 'C'}); time = 0:0.1:20; testArray = (4*exp(-.2*time) + 10) + normrnd(0,1,size(time)); [cf, gof] = fit(time', testArray', ft, 'StartPoint', [17 -.6 100]); % check: figure(1) clf hold on plot(time,testArray,'k.') plot(time, cf.A*exp(cf.B*time) + cf.C, 'r');

但我不知道这是否解决了你的问题,因为我无法重现你的错误。

Providing an initial guess helps the fit a great deal. What I found is that the correct sign of the initial values is especially important.

Anyway, I dont run into the problem you have if I run this code:

ft = fittype('(A*exp(B*x)) + C', 'coefficients', {'A', 'B', 'C'}); time = 0:0.1:20; testArray = (4*exp(-.2*time) + 10) + normrnd(0,1,size(time)); [cf, gof] = fit(time', testArray', ft, 'StartPoint', [17 -.6 100]); % check: figure(1) clf hold on plot(time,testArray,'k.') plot(time, cf.A*exp(cf.B*time) + cf.C, 'r');

But I dont know if this solved your problem, since I cant reproduce your error.

更多推荐

本文发布于:2023-08-02 08:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1372382.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   曲线   MatLab   Fitting   Curve

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!