用Matlab进行曲线拟合失败了

发布于 2024-12-25 13:11:41 字数 929 浏览 1 评论 0原文

我正在尝试在命令行中使用 fit 在 Matlab 中拟合曲线。输入数据是:

X =

     1
     2
     4
     5
     8
     9
    10
    13

Y =

1.0e-04 *

    0.1994
    0.0733
    0.0255
    0.0169
    0.0077
    0.0051
    0.0042
    0.0027

目标函数是

Y = 1/(kappa*X.^a)

我正在使用 fittypefitoptionsfit 如下:

model1 = fittype('1/(kappa*x.^pow)');
opt1 = fitoptions(model1);
opt1.StartPoint = [1e-5 -2];
[fit1,gof1] = fit(X,Y.^-1,model1,opt1)

我使用 得到结果rsquare 约为 -450,与测量方向大致相同。

编辑

我删除了 fit 命令中的 .^-1 。这改善了行为,但并不完全正确。如果我将 model1 设置为:

model1 = fittype('1/(kappa*x.^pow)');

拟合不好。如果我将其设置为:

model1 = fittype('kappa*x.^pow');

拟合良好(kappa 是一个非常小的数字,pow 为负数)。

我还标准化了 Y 并且得到了合理的结果

I'm trying to fit a curve in Matlab using fit in command line. The input data is:

X =

     1
     2
     4
     5
     8
     9
    10
    13

Y =

1.0e-04 *

    0.1994
    0.0733
    0.0255
    0.0169
    0.0077
    0.0051
    0.0042
    0.0027

And the target function is

Y = 1/(kappa*X.^a)

I am using fittype, fitoptions, and fit as follow:

model1 = fittype('1/(kappa*x.^pow)');
opt1 = fitoptions(model1);
opt1.StartPoint = [1e-5 -2];
[fit1,gof1] = fit(X,Y.^-1,model1,opt1)

I get results with rsquare of roughly -450 which are vaguely in the same direction as the measurement.I have attached a figure to demonstrate this. How can I improve Matlab fitting skills?

Edit:

I removed the .^-1 in the fit command. This improved the behavior but it is not entirely correct. If I set model1 to be:

model1 = fittype('1/(kappa*x.^pow)');

The fit is bad. If I set it to be:

model1 = fittype('kappa*x.^pow');

The fit is good (with kappa being a very small number and pow being negative).

I have also normalized Y and I get a reasonable results

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑红尘 2025-01-01 13:11:41

您应该替换

[fit1,gof1] = fit(X,Y.^-1,model1,opt1)

[fit1,gof1] = fit(X,Y,model1,opt1)

另外,kappa 的初始条件是 1e-5,如果 kappa 位于分子中,则这才有意义。

使用模型 kappa*x.^pow,初始条件为 [1e-5 -2],您将得到正确的拟合:

X =[1     2     4     5     8     9    10    13]';
Y = 1.0e-04 * [0.1994 0.0733 0.0255 0.0169 0.0077 0.0051 0.0042  0.0027]';

model1 = fittype('kappa*x.^pow');
opt1 = fitoptions(model1);
opt1.StartPoint = [1e-5 -2];
[fit1,gof1] = fit(X,Y,model1,opt1)
plot(fit1, X, Y)

拟合结果为

>> fit1
fit1 = 
   General model:
   fit1(x) = kappa*x.^pow
   Coefficients (with 95% confidence bounds):
     kappa =   2.044e-05  (1.931e-05, 2.158e-05)
     pow =      -1.657  (-1.851, -1.464)

拟合曲线

You should replace

[fit1,gof1] = fit(X,Y.^-1,model1,opt1)

by

[fit1,gof1] = fit(X,Y,model1,opt1)

Also your initial condition for kappa is 1e-5, which would make sense if kappa was in the numerator.

Using the model kappa*x.^pow, with the initial condition [1e-5 -2], you would get the right fit:

X =[1     2     4     5     8     9    10    13]';
Y = 1.0e-04 * [0.1994 0.0733 0.0255 0.0169 0.0077 0.0051 0.0042  0.0027]';

model1 = fittype('kappa*x.^pow');
opt1 = fitoptions(model1);
opt1.StartPoint = [1e-5 -2];
[fit1,gof1] = fit(X,Y,model1,opt1)
plot(fit1, X, Y)

The fitted result is

>> fit1
fit1 = 
   General model:
   fit1(x) = kappa*x.^pow
   Coefficients (with 95% confidence bounds):
     kappa =   2.044e-05  (1.931e-05, 2.158e-05)
     pow =      -1.657  (-1.851, -1.464)

Fitted Curve

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文