在 Mathematica 中,可以使用 FindFitNonlinearModelFit 函数来实现数据的曲线拟合。

X Y
11 5
60 81
14 28
50 80
51 45
36 93
30 65
96 86

1. 准备数据

首先,将数据点输入为一个列表:

data = {{11, 5}, {60, 81}, {14, 28}, {50, 80}, {51, 45}, {36, 93}, {30, 65}, {96, 86}};

2. 选择拟合模型

假设想拟合一个线性模型(一次多项式):
y=ax+by=ax+by=ax+b

或者更高阶的多项式,例如二次多项式:
y=ax2+bx+cy=ax^2+bx+cy=ax2+bx+c

或者其他非线性模型,如指数模型:
y=axbx+cy=ax^{bx} +cy=axbx+c

这里以 二次多项式拟合 为例。

3. 使用 FindFit 进行拟合

model = a x^2 + b x + c;  (* 定义模型 *)
fit = FindFit[data, model, {a, b, c}, x]  (* 进行拟合 *)

输出结果类似:

{a -> 0.012, b -> 1.23, c -> -10.5}

4. 使用 NonlinearModelFit(推荐,可获取更多统计信息)

nlm = NonlinearModelFit[data, model, {a, b, c}, x];
nlm["BestFitParameters"]  (* 查看拟合参数 *)

输出示例:

{a -> 0.012, b -> 1.23, c -> -10.5}

5. 绘制数据和拟合曲线

Show[
 ListPlot[data, PlotStyle -> Red],  (* 绘制原始数据点 *)
 Plot[nlm[x], {x, 0, 100}, PlotStyle -> Blue],  (* 绘制拟合曲线 *)
 Frame -> True, Axes -> False
]

6. 检查拟合优度(可选)

nlm["RSquared"]  (* R²值,越接近1拟合越好 *)
nlm["AdjustedRSquared"]  (* 调整后的R² *)

完整代码示例

data = {{11, 5}, {60, 81}, {14, 28}, {50, 80}, {51, 45}, {36, 93}, {30, 65}, {96, 86}};
model = a x^2 + b x + c;
nlm = NonlinearModelFit[data, model, {a, b, c}, x];
fitParams = nlm["BestFitParameters"]
Show[
 ListPlot[data, PlotStyle -> Red],
 Plot[nlm[x], {x, 0, 100}, PlotStyle -> Blue],
 Frame -> True, Axes -> False
]

其他模型示例

  • 线性拟合model = a x + b
  • 指数拟合model = a Exp[b x] + c
  • 自定义非线性模型:直接替换 model 表达式即可。

如果需要更复杂的模型或优化,可以调整 FindFit 的选项(如 Method)。

拟合结果已经是 y=ax2+bx+cy=ax^2+bx+cy=ax2+bx+c 的形式了(a=0.0295108, b=-3.63834, c=162.608)。如果希望以 数学表达式格式 显示(如 f(x) = 0.0295x² - 3.638x + 162.61),可以使用以下方法:


方法 1:直接提取参数并格式化输出

f[x_] := a x^2 + b x + c /. fitParams; (* 定义函数 f(x) *)
TraditionalForm[f[x]] (* 以传统数学格式显示 *)

输出示例:f(x)=0.0295108x2−3.63834x+162.608f(x) = 0.0295108 x^2 - 3.63834 x + 162.608f(x)=0.0295108x23.63834x+162.608


方法 2:自定义显示(控制小数位数)

{aVal, bVal, cVal} = {a, b, c} /. fitParams;
f[x_] := aVal x^2 + bVal x + cVal;
Print["f(x) = ", Round[aVal, 0.0001], "x² + ", Round[bVal, 0.0001], "x + ", Round[cVal, 0.0001]]

输出示例:f(x)=0.0295x2−3.6383x+162.608f(x) = 0.0295x² - 3.6383x + 162.608f(x)=0.0295x23.6383x+162.608


方法 3:用 StringForm 动态生成表达式

expression = StringForm["f(x) = `` x² + `` x + ``", 
   Round[aVal, 0.001], Round[bVal, 0.001], Round[cVal, 0.001]];
Print[expression]

输出示例:
f(x)=0.03x2−3.638x+162.608f(x) = 0.03 x² - 3.638 x + 162.608f(x)=0.03x23.638x+162.608


方法 4:直接输出 LaTeX 格式(用于论文)

Print["f(x) = ", aVal, "x^2 + ", bVal, "x + ", cVal]
(* 或手动调整为 LaTeX *)
Print["f(x) = ", aVal, " x^2 + ", bVal, " x + ", cVal]

输出示例:
f(x)=0.0295108x2−3.63834x+162.608f(x) = 0.0295108 x^2 - 3.63834 x + 162.608f(x)=0.0295108x23.63834x+162.608

可以复制到 LaTeX 中:f(x)=0.0295x2−3.6383x+162.608f(x) = 0.0295x^2 - 3.6383x + 162.608f(x)=0.0295x23.6383x+162.608


完整代码示例

X Y
51 45
36 93
30 65
96 86
data = {{51, 45}, {36, 93}, {30, 65}, {96, 86}};
model = a x^2 + b x + c;
nlm = NonlinearModelFit[data, model, {a, b, c}, x];
fitParams = nlm["BestFitParameters"];

(* 提取参数并定义函数 *)
{aVal, bVal, cVal} = {a, b, c} /. fitParams;
f[x_] := aVal x^2 + bVal x + cVal;

(* 输出格式化后的表达式 *)
Print["拟合函数为: ", TraditionalForm[f[x]]]
Print["或更简洁形式: f(x) = ", Round[aVal, 0.0001], "x² + ", Round[bVal, 0.0001], "x + ", Round[cVal, 0.0001]]

(* 绘制图形 *)
Show[
  ListPlot[data, PlotStyle -> Red, PlotLabel -> "二次多项式拟合"], 
  Plot[f[x], {x, 0, 100}, PlotStyle -> Blue], 
  Frame -> True, Axes -> False
]

输出结果示例

  • 拟合参数:
    {a -> 0.0295108, b -> -3.63834, c -> 162.608}
    
  • 格式化后的函数:
    拟合函数为: 0.0295108- 3.63834 x + 162.608
    或更简洁形式: f(x) = 0.0295- 3.6383x + 162.608
    

在 Mathematica 中,可以轻松地拟合 更高阶多项式(如三次)指数函数,只需修改 model 的定义即可。以下是具体方法:


1. 三次多项式拟合(Cubic Fit)

模型形式

f(x)=ax3+bx2+cx+df(x) = a x^3 + b x^2 + c x + df(x)=ax3+bx2+cx+d

代码实现

data = {{11, 5}, {60, 81}, {14, 28}, {50, 80}, {51, 45}, {36, 93}, {30, 65}, {96, 86}};

(* 定义三次多项式模型 *)
model = a x^3 + b x^2 + c x + d;
nlm = NonlinearModelFit[data, model, {a, b, c, d}, x];
fitParams = nlm["BestFitParameters"]

(* 定义拟合函数 *)
f[x_] := a x^3 + b x^2 + c x + d /. fitParams;

(* 输出拟合方程 *)
Print["f(x) = ", Coefficient[f[x], x, 3], "x³ + ", Coefficient[f[x], x, 2], "x² + ", Coefficient[f[x], x, 1], "x + ", Coefficient[f[x], x, 0]];

(* 绘制拟合曲线 *)
Show[
  ListPlot[data, PlotStyle -> Red, PlotLabel -> "三次多项式拟合"], 
  Plot[f[x], {x, 0, 100}, PlotStyle -> Blue], 
  Frame -> True, Axes -> False
]

输出示例

{a -> -0.000123, b -> 0.0234, c -> -2.345, d -> 123.456}
f(x) = -0.000123+ 0.0234- 2.345x + 123.456

2. 指数函数拟合(Exponential Fit)

模型形式

f(x)=aebx+cf(x) = a e^{b x} + cf(x)=aebx+cf(x)=aebxf(x) = a e^{b x}f(x)=aebx

代码实现

(* 定义指数模型 *)
model = a Exp[b x] + c; (* 或 model = a Exp[b x] *)
nlm = NonlinearModelFit[data, model, {a, b, c}, x];
fitParams = nlm["BestFitParameters"]

(* 定义拟合函数 *)
f[x_] := a Exp[b x] + c /. fitParams;

(* 输出拟合方程 *)
Print["f(x) = ", Round[a /. fitParams, 0.001], " e^(", Round[b /. fitParams, 0.001], "x) + ", Round[c /. fitParams, 0.001]];

(* 绘制拟合曲线 *)
Show[
  ListPlot[data, PlotStyle -> Red, PlotLabel -> "指数函数拟合"], 
  Plot[f[x], {x, 0, 100}, PlotStyle -> Blue], 
  Frame -> True, Axes -> False
]

输出示例

{a -> 10.5, b -> 0.02, c -> 5.0}
f(x) = 10.5 e^(0.02x) + 5.0

3. 其他常见拟合模型

模型类型 数学形式 Mathematica model 定义
线性 ( y = a x + b ) a x + b
二次多项式 ( y = a x^2 + b x + c ) a x^2 + b x + c
三次多项式 ( y = a x^3 + b x^2 + c x + d ) a x^3 + b x^2 + c x + d
指数 ( y = a e^{b x} + c ) a Exp[b x] + c
对数 ( y = a \ln(x) + b ) a Log[x] + b
幂函数 ( y = a x^b ) a x^b

4. 如何选择合适的拟合模型?

  1. 观察数据趋势
    • 如果数据呈 抛物线二次多项式
    • 如果数据呈 S型三次多项式
    • 如果数据 快速增长/衰减指数函数
    • 如果数据呈 对数增长对数函数
  2. 检查拟合优度
    • 使用 nlm["RSquared"] 查看 ( R^2 )(越接近1越好)。
    • 使用 Show[ListPlot[data], Plot[f[x], ...]] 观察拟合曲线是否贴合数据。

5. 完整代码(以三次多项式为例)

data = {{11, 5}, {60, 81}, {14, 28}, {50, 80}, {51, 45}, {36, 93}, {30, 65}, {96, 86}};

(* 三次多项式拟合 *)
model = a x^3 + b x^2 + c x + d;
nlm = NonlinearModelFit[data, model, {a, b, c, d}, x];
fitParams = nlm["BestFitParameters"];

(* 定义函数并输出 *)
f[x_] := a x^3 + b x^2 + c x + d /. fitParams;
Print["f(x) = ", f[x] // TraditionalForm];

(* 绘制图形 *)
Show[
  ListPlot[data, PlotStyle -> Red, PlotLabel -> "三次多项式拟合"], 
  Plot[f[x], {x, 0, 100}, PlotStyle -> Blue], 
  Frame -> True, Axes -> False
]

最后

  • 多项式拟合:修改 model = a x^n + ... 的阶数。
  • 指数/对数拟合:使用 Exp[b x]Log[x]
  • 查看拟合质量:用 nlm["RSquared"] 或绘图对比。
Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐