今天看到有人要用matlab作数据简单拟合;用gnuplot+CTeX试了一下效果

[求助]matlab曲线拟合及修正 已有1人参与
初始悬赏金币 5 个
本人新手,还未对matlab进行深入学习,紧急求助~
已知:x=[8.9 11.7 13.2 13.4 14 14.2 14 10.7 21.671 23.487 20.902 16.507];
    y=[17.698 17.698 27.365 27.365 37.031 37.031 27.365 17.698 66.03 75.696 56.364 37.031]。

请问该如何用matlab拟合出多项式方程y=f(x),然后如何对方程进行修正?


代码如下:

\documentclass[border=10pt]{standalone}
\usepackage{siunitx}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{pgfplotstable}
\usepackage{filecontents}
\begin{filecontents*}{myData.dat}
X     Y
8.900	17.698
11.700	17.698
13.200	27.365
13.400	27.365
14.000	37.031
14.200	37.031
14.000	27.365
10.700	17.698
21.671	66.030
23.487	75.696
20.902	56.364
16.507	37.031
\end{filecontents*}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{
width=10cm,
legend style={font=\scriptsize}
}
\begin{axis}[%
xlabel=$x$,
ylabel=$y$,
ymin=0,
xmin=0,
scaled y ticks=base 10:0,
legend cell align = left,
legend pos = north west
]
\addplot[only marks] table {mydata.dat};
\addlegendentry{scatter plot};
\addplot+[no markers,red] table [y={create col/linear regression={y=Y}}]{myData.dat};
\addlegendentry{%
Linear regression:  $y=\pgfmathprintnumber{\pgfplotstableregressiona} x
\pgfmathprintnumber[print sign]{\pgfplotstableregressionb}$};
% polynomial fit
\addplot [no markers, blue] gnuplot [raw gnuplot] { % allows arbitrary gnuplot commands
f(x) = a*x**2+b*x+c;     % Define the function to fit
a=10;b=10; c=10;         % Set reasonable starting values here
fit f(x) 'myData.dat' u 1:2 via a,b,c; % Select the file, starts at col 1 and two variables
plot [x=0:28] f(x);    % Specify the range to plot
set print "parameters.dat";  % Open a file to save the parameters
print a, b, c;                  % Write the parameters to file
};
\addlegendentry{\pgfplotstableread{parameters.dat}\parameters % Open the file Gnuplot wrote
\pgfplotstablegetelem{0}{0}\of\parameters \pgfmathsetmacro\paramA{\pgfplotsretval} % Get first element, save into \paramA
\pgfplotstablegetelem{0}{1}\of\parameters \pgfmathsetmacro\paramB{\pgfplotsretval}
\pgfplotstablegetelem{0}{2}\of\parameters \pgfmathsetmacro\paramC{\pgfplotsretval}
polynomial:$y=\pgfmathprintnumber{\paramA} x^2 \pgfmathprintnumber[print sign]{\paramB} x \pgfmathprintnumber[print sign]{\paramC}$
}
\end{axis}
\end{tikzpicture}
\end{document}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值