MATLAB如出错 polyfit (第 64 行) V(:,n+1) = ones(length(x),1,class(x));何绘制点图
时间: 2024-10-22 19:14:40 浏览: 47
当你在MATLAB中使用`polyfit`函数遇到错误,并且错误出现在第64行,这通常是由于尝试将一维数组`ones(length(x),1,class(x))`赋值给向量`V(:,n+1)`导致的问题。`polyfit`用于拟合多项式,而`V`可能是存储多项式系数的矩阵。
在创建`V`矩阵时,该行可能预期的是一个二维列向量,但`ones`返回的是一个一维行向量。解决这个问题的一种常见做法是在`ones`之前将其转置,即:
```matlab
V(:,n+1) = ones(1, length(x), class(x)).'; % 添加'.'
```
这里加上`. `'表示对一维数组进行转置,使其变成长度为`length(x)`的一列向量,然后可以正确地添加到`V`的对应位置。
至于如何绘制包含拟合数据的点图,你可以这样做:
1. 使用`polyval`函数计算多项式的值,对于每个输入`x`点。
2. 将原数据点和拟合点画在同一张图上。
示例代码如下:
```matlab
% 假设你已经有了拟合系数V和x数据
p = V(1:n, :); % 提取多项式系数
y_fit = polyval(p, x); % 计算拟合曲线
% 绘制原始数据点和拟合线
plot(x, y, 'o', x, y_fit, '-') % 'o'表示标记原始点,'-'表示连续线
xlabel('X轴')
ylabel('Y轴')
title('Original Data Points and Polynomial Fit')
```
相关问题
matlab polyfit源码,MATLAB的拟合函数polyfit 的程序代码是什么啊
polyfit函数的MATLAB程序代码如下:
```matlab
function [p, S, mu] = polyfit(x, y, n)
%POLYFIT Fit polynomial to data.
% POLYFIT(X,Y,N) finds the coefficients of a polynomial P(X) of
% degree N that fits the data Y best in a least-squares sense. P(X)
% is a row vector of length N+1 containing the polynomial coefficients
% in descending powers.
%
% [P,S] = POLYFIT(X,Y,N) returns the polynomial coefficients P and a
% structure S for use with POLYVAL to obtain error estimates for
% predictions. S contains fields:
% S.normr -- 2-norm of the residual: norm(Y - PVAL)./sqrt(length(Y))
% S.resid -- vector of residuals: Y - PVAL
% S.df -- degrees of freedom
% S.normr^2 -- S.normr squared
% S.R -- the triangular factor from a QR decomposition of the
% Vandermonde matrix of X
% S.coeff -- vector of coefficients in a model that is orthogonal
% to the constant term: X - repmat(mean(X),size(X,1),1)
% = Q*R, Y - repmat(mean(Y),size(Y,1),1) = Q*[Rc;rest]
% where Rc is a vector of regression coefficients for
% the orthogonal polynomial terms, and rest is a vector
% of coefficients for the null space of X - ones(n,1)*[1
% mean(X)].
%
% [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.
%
% Class support for inputs X,Y:
% float: double, single
%
% See also POLY, POLYVAL, ROOTS, LSCOV, VANDER.
% Copyright 1984-2004 The MathWorks, Inc.
% $Revision: 5.26.4.8 $ $Date: 2004/12/06 16:38:03 $
if ~isequal(size(x),size(y))
error('X and Y vectors must be the same size.')
end
x = x(:);
y = y(:);
if nargin < 3, n = 1; end
if n > length(x)-1
error('N must be less than the length of X.')
end
if length(x) ~= length(y)
error('X and Y vectors must be the same length.')
end
% Construct Vandermonde matrix.
V(:,n+1) = ones(length(x),1,class(x));
for j = n:-1:1
V(:,j) = x.*V(:,j+1);
end
% Compute coefficients.
[Q,R] = qr(V,0);
ws = warning('off','MATLAB:rankDeficientMatrix');
p = R\(Q'*y); % Same as p = V\y;
warning(ws);
% Remove trailing zeros.
p = p.'; % Polynomial coefficients are row vectors by convention.
if length(p) > n
p = p(1:n+1);
end
% Compute error structure vector.
if nargout > 1
r = y - V*p.';
normr = norm(r);
df = length(y) - (n+1);
S.normr = normr;
S.resid = r;
S.df = df;
S.normr2 = normr^2;
S.R = R;
S.coeff = p;
end
% Undo scaling and centering.
if nargout > 2
mu = [mean(x); std(x)];
else
mu = [];
end
if length(p) > 1
p = polyfix(p,mu);
end
% ------------------------------------------------------------------
function c = polyfix(c,mu)
%POLYFIX Fix polynomial coefficients for new data.
% C = POLYFIX(C,MU) fixes the polynomial coefficients C for a
% change of variable XHAT = (X-MU(1))/MU(2). MU(1) is the mean of
% the original data and MU(2) is the standard deviation. C is a
% row vector of polynomial coefficients in descending powers.
if isempty(c), return, end
m = length(c) - 1;
if m == 0, return, end
c = c(:);
% Scale coefficients.
scal = cumprod([1 repmat(mu(2),1,m)]./[1:m+1]);
c = c.*scal;
% Shift coefficients.
c(1:m) = c(1:m) - c(m+1:-1:2).'*mu(1)./mu(2).^(1:m);
% Remove scale factors.
c = c./mu(2).^max(0,(0:m));
c = c.'; % Polynomial coefficients are row vectors by convention.
阅读全文
相关推荐

















