How to make a polynomial approximation in Scilab? -


i've set of measures, want approximate. know can 4th degree polynomial, don't know how find it's 5 coefficients using scilab.

for now, must use user-friendly functions of open office calc... so, keep using scilab, i'd know if built-in function exists, or if can use simple script.

there no built-in polyfit function in matlab, can make own:

function cf = polyfit(x,y,n) = ones(length(x),n+1) i=1:n     a(:,i+1) = x(:).^i end cf = lsq(a,y(:)) endfunction 

this function accepts 2 vectors of equal size (they can either row or column vectors; colon operator makes sure column-oriented in computation) , degree of polynomial.

it returns column of coefficients, ordered 0th nth degree.

the computational method straightforward: set (generally, overdetermined) linear system requires polynomial pass through every point. solve in sense of least squares lsq (in practice, seems cf = a\y(:) performs identically, although algorithm bit different there).

example of usage:

x = [-3 -1 0 1 3 5 7] y = [50 74 62 40 19 35 52] cf = polyfit(x,y,4)  t = linspace(min(x),max(x))'   // use these coefficients plot polynomial = ones(length(t),n+1) i=1:n     a(:,i+1) = t.^i end plot(x,y,'r*') plot(t,a*cf) 

output:

polynomial fit


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -