matlab 作拟合的直线例如x,y如下x=[12.10 11.96 11.80 11.65 11.50];y=[23.00 34.00 45.00 56.00 67.00];并求直线的斜率能不能说得再清楚些
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/16 12:44:06
matlab 作拟合的直线例如x,y如下x=[12.10 11.96 11.80 11.65 11.50];y=[23.00 34.00 45.00 56.00 67.00];并求直线的斜率能不能说得再清楚些
matlab 作拟合的直线
例如x,y如下
x=[12.10 11.96 11.80 11.65 11.50];
y=[23.00 34.00 45.00 56.00 67.00];
并求直线的斜率
能不能说得再清楚些
matlab 作拟合的直线例如x,y如下x=[12.10 11.96 11.80 11.65 11.50];y=[23.00 34.00 45.00 56.00 67.00];并求直线的斜率能不能说得再清楚些
>> help polyfit
POLYFIT Fit polynomial to data.
POLYFIT(X,Y,N) finds the coefficients of a polynomial P(X) of
degree N that fits the data,P(X(I))~=Y(I),in a least-squares sense.
[P,S] = POLYFIT(X,Y,N) returns the polynomial coefficients P and a
structure S for use with POLYVAL to obtain error estimates on
predictions.If the errors in the data,Y,are independent normal
with constant variance,POLYVAL will produce error bounds which
contain at least 50% of the predictions.
The structure S contains the Cholesky factor of the Vandermonde
matrix (R),the degrees of freedom (df),and the norm of the
residuals (normr) as fields.
[P,S,MU] = POLYFIT(X,Y,N) finds the coefficients of a polynomial
in XHAT = (X-MU(1))/MU(2) where MU(1) = mean(X) and MU(2) = std(X).
This centering and scaling transformation improves the numerical
properties of both the polynomial and the fitting algorithm.
Warning messages result if N is >= length(X),if X has repeated,or
nearly repeated,points,or if X might need centering and scaling.
See also POLY,POLYVAL,ROOTS.
方程式y=ax+b,记得把x和y写成列向量
clc
A = [x ones(size(x)]
ab = inv(A'*A)*A'*y
a=ab(1)
b=ab(2)