-
Notifications
You must be signed in to change notification settings - Fork 1
/
polyInterp.m
43 lines (36 loc) · 1.15 KB
/
polyInterp.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
function [coeffs] = polyInterp(x,y,tipo)
% POLYINTERP is a function which calculates the polynomium that best fits
% the sample points givem by the inputs. The two first inputs represents
% the function y(x), and the third represents the kind of output you want.
%
% If "tipo" is specified as "polyvet", then you'll have the output "coeffs"
% as a vector with the polynomial coefficients.
%
% If "tipo" is specified as "fhandle", then you'll have the output "coeffs"
% as a function handle with the polynomial coefficients.
if isscalar(x) || ischar(x) || iscell(x) || isscalar(y) || ischar(y) || iscell(y)
error('Both inputs MUST be arrays!');
end
if length(x) ~= length(y)
error('Both inputs MUST have the same length!');
end
if nargin == 2
tipo = 'polyvet';
end
%% Let's do the work
% set both inputs as column vectors
x = x(:);
y = y(:);
% specifing the order of the polynomial
N = length(x);
matX = repmat(x, [1, N]);
ordem = 0:N-1;
mat = matX.^ordem;
coeffs = fliplr((mat\y)');
if strcmp(tipo, 'fhandle')
syms x
y = x.^fliplr(ordem);
y = sum(y.*coeffs);
coeffs = matlabFunction(y);
end
return