0

I've got two arrays;

R = [r0, r1, r2, ..., r999]

Z = [z0, z1, z2, ..., z999]

I want to apply polyfit to the above, where the function is R(z). I need the polynomial to be x^2+x^4+x^6+x^8

In excel, the trend function does not allow for only even powers, so I've tried to write this in Matlab, but I can't figure out how customise polyfit so that it only uses even powers as described above.

Any suggestions? thanks

ramz
  • 1

1 Answers1

1

You can use the Least Squares Method from linear algebra to solve this:

% The Data
R = [r0, r1, r2, ..., r999]'; % Note the apostrophe
Z = [z0, z1, z2, ..., z999]';

% Create Vandermonde-ish matrix
X = [Z.^2 Z.^4 Z.^6 Z.^8];

% Solve equation system
a = X\R;

% Reshape and pad with zeros for the odd and 0th powers
p = [zeros(size(a)) a]';
pval = flip([0; p(:)]);
Skogsv
  • 494
  • 3
  • 14