【问题标题】:How to convert the result of a fitted curve from "cfit" to actual array data in Matlab?如何将拟合曲线的结果从“cfit”转换为 Matlab 中的实际数组数据?
【发布时间】:2022-01-13 16:36:15
【问题描述】:

我想拟合某个曲线。我的数据存储在“x”和“y2”中。为了拟合这条曲线,有一些预定义的函数,比如“fit”。

我的例子:

x = [20 30 40 50 60 70 80 90 100 200];
y = [4.229 2.514 1.714 1.143 0.8 0.629 0.514 0.4 0.349 0.057];
p_0 = 2*10^(-5);
y2 = 10.^(y/20)*p_0;

[xData,yData] = prepareCurveData(x,y2);
fit_type = 'pchipinterp';

[fitresult] = fit(xData,yData,ft);

结果“fitresult”现在作为“1x1 cfit”对象存储在 MATLAB 工作区中。 有什么方法可以从 fitresult 中获取一个我可以手动定义的数组长度的实际数组? (例如 MATLAB 工作区:fitresult: 1x3841 double

【问题讨论】:

    标签: arrays matlab curve-fitting curve data-fitting


    【解决方案1】:

    编辑你的脚本:

    fit_type = 'pchipinterp';
    [fitresult] = fit(xData,yData,fit_type);
    

    fitresult 有两个类 CoefficientsShape-preserving (pchip) interpolant,每一个都有一个分量 p拟合结果(x) 拟合结果 =

     Shape-preserving (pchip) interpolant:
       fitresult(x) = piecewise polynomial computed from p
     Coefficients:
       p = coefficient structure
    

    要提取结果,请将这些代码行添加到您的脚本中(名称由您决定):

    1. piecewise_polynomial_computed_from_p = fitresult(x)      
    2. coefficient_structure = fitresult.p
    

    这里的 coefficient_structure 是一个构造函数:

    coefficient_structure = 
          form: 'pp'
        breaks: [20 30 40 50 60 70 80 90 100 200]
         coefs: [9x4 double]
        pieces: 9
         order: 4
           dim: 1
    

    要从中提取结果,例如:

    Breaks = coefficient_structure.breaks
    

    结果是:

    Breaks =
        20    30    40    50    60    70    80    90   100   200
    

    【讨论】:

    • 你完全正确,在我的代码中应该有“fit_type”而不是“ft”。
    • 感谢您的信息!!但是我怎样才能从 fitresult 中获得完整的值?例如,连接 4.229 和 2.514 之间的 y 值的 20 和 30 之间的值?几乎就像我想绘制插值一样 plot(x_array_length_of_the_interpolated_values,interpolated_values)。我可以从 coefficient_structure.breakscoefficient_structure.coefs 中计算出这些值吗?
    • 我不明白你到底需要什么,但我猜: - 图(1)图(x,piecewise_polynomial_computed_from_p')图(2)图(y,piecewise_polynomial_computed_from_p')然后突出显示拟合函数按 cntl+d 或调试脚本进入函数(“fit”) 跟随脚本直到计算 coefficient_structure.breaks 和 coefficient_structure.coefs 元素的步骤从中得到启发。
    • 谢谢,这很有帮助。我实际上可以在曲线拟合工具中获得相当不错的结果,因为我的拟合类型是“exp2”。方程和系数可以单独打印和绘制。因此我得到了我的数据。 :D
    • 我很乐意提供帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    相关资源
    最近更新 更多