I need help building a dynamically evolving matrix based on a polynomial of unknown or user inputted size.

조회 수: 1 (최근 30일)
I need help creating an array based on a polynomial of unknown length. Polyval and Poly fit cannot be used.
I need something that would do....
n=length(x);
X=[ones(n,1) x(:)];
% and after 1 iteration
X=[ones(n,1) x(:) x(:).^2];
%and after the 2nd iteration
X=[ones(n,1) x(:) x(:).^2 x(:).^3];
%and after the 3rd iteration
X=[ones(n,1) x(:) x(:).^2 x(:).^3 x(:).^4];
% and so on for n amount of iterations

답변 (3개)

Walter Roberson
Walter Roberson 2019년 4월 22일
편집: Walter Roberson 2019년 4월 22일
xv = x(:);
yv = y(:);
X = [];
deg = 0;
while true
X = [X, xv.^deg];
coeffs = X \ yv;
residue = sum( (polyval(flipud(coeffs), xv) - yv).^2 );
if residue < tolerance
break;
end
end

Steven Lord
Steven Lord 2019년 4월 22일
Take a look at the vander function.

Terry Poole
Terry Poole 2019년 4월 22일
I finally got it last night by using
X=[ones(n,1) x(:)]; for i=2:n X[:,i-1]=X[:,i]+x.^(i+1); end

카테고리

Help CenterFile Exchange에서 Polynomials에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by