【问题标题】:Mathematica: Processing Fit ParametersMathematica:处理拟合参数
【发布时间】:2013-03-18 16:26:19
【问题描述】:

我想访问 NonlinearModelFit 的拟合参数。这是代码

model = a*Cos[b*t + c];
fit = NonlinearModelFit[data, model, {a, b, c}, t, Method -> NMinimize]

当我使用命令时:

fit["BestFitParameters"]

值以下列格式返回:

{a -> 1, b -> 2, c -> -3}

现在我想将 a 的值存储在变量 x 中

x=fit["BestFitParameters"][[1]]

但这给了

x= a -> 1

不,我想知道如何解析“-> - 运算符”来获得

x=1

提前致谢!

【问题讨论】:

    标签: design-patterns wolfram-mathematica rule data-fitting


    【解决方案1】:

    嗯,你可以写

    x = a/.{a -> 1, b -> 2, c -> -3}
    

    这会将a 的值分配给x

    我不完全确定你应该这样做,你可以简单地将规则列表存储在一个变量中,并在需要时从中提取位。

    【讨论】:

      【解决方案2】:

      我可以建议另一种方法吗:

      C1fitparameters = 
      Table[{C1fit["BestFitParameters"][[i, 1]], C1fit["BestFitParameters"][[i, 2]]}, 
      {i, 1, Length[C1fit["BestFitParameters"]]}]
      

      返回一个列表:

      {{A, 5.11419}, {Beta, 14.2637}, {Omega_0, 174.118}, {Phi, -0.117246}}
      

      所以要访问变量名:

      C1fitparameters[[1, 1]]
      

      返回 A。并且:

      C1fitparameters[[1, 2]]
      

      返回 A 的值,即 5.11419。瞧。

      【讨论】:

        【解决方案3】:

        你想要

        x = fit["BestFitParameters"][[1]][[2]]
        

        这会检索 a->1 的右侧。使用x = fit["BestFitParameters"][[1]][[1]] 将检索左侧(在本例中为“a”)

        【讨论】:

        • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您正在为将来的读者回答问题,而这些人可能不知道您的代码建议的原因。也请尽量不要用解释性的 cmets 挤满你的代码,这会降低代码和解释的可读性!
        猜你喜欢
        • 1970-01-01
        • 2012-01-21
        • 2012-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多