leftimage for poohprod.ru

Curve Fitting with Scilab

Neither Scilab nor Scicoslab have a function for straight curve fitting, such as the polyfit function that we can find in Matlab. However, it’s not that difficult to develop (or find?) a custom made function for that purpose.

In [1] we can find a suggestion for this task. This is an edited version of the transcription of the 15-line code.

function p = polyfit(x, y, n)
if length(x) ~= length(y)
error('x and y vectors must be the same size')

x = x( : );
y = y( : );
V = ones(length(x), n+1);

for j = n : -1 : 1
V(:, j) = x .* V (:, j+1);

[Q, R] = qr(V);
QTy = Q' * y;
p = R(1 : n+1, 1 : n+1) \ QTy(1 : n+1);
p = p.';

There are three inputs: vectors x and y, and a scalar n. x and y are the vectors defining our points to be fitted, and n is the order of the polynomial to be found.

On the other hand, we also need an equivalent of the polyval function given in Matlab. Naturally there are many possibilities here. The following code is much simpler than the previous one and we’re showing our suggestion.

function y = polyval(p, x)
y = 0*x;
p = mtlb_fliplr(p);
for ix = 1 : length(p)
y = y + p(ix) * x.^(ix-1);

The goal of this function is to use the polynomial found by polyfit, receive an x-value and release the corresponding y-value produced by that polynomial. There are two inputs: we need the coefficients of the polynomial, in p, and the x-value, in x. We just do the required math in a simple and straight way. We use the function mtlb_fliplr (provided in the M2SCI toolbox) just to sort the order of the exponent of the variable.

Now, let’s see in action both functions.

/ our environment
clear; clc

/ our two functions

/ (arbitrarily) the number of points to take into account
np = 10;

/ the x-vector and two functions, the second function
/ is a noised version of the first one
x = linspace(0, 1, np);
y1 = x.^3 - 5*x.^2 - 3*x - 7;
y2 = y1 + .1*rand(1, np);

/ the x and y vectors, and the order of the polynomial
/ that we want to obtain
p = polyfit(x, y2, 3)

/ other x-values and find the original function
x = 0 : .4 : 1
y1 = x.^3 - 5*x.^2 - 3*x - 7

/ to find the equivalent values in the
/ function
y = polyval(p, x)

/ the values just for comparison purposes
ratio = y1./y

Now, the (commented) results shown by Scilab are:

/ our 3rd. order polynomial from the noised function
p = 1.1103879 - 5.0400213 - 3.052195 - 6.9291697

/ of points generated
x = 0. 0.4 0.8
y1 = - 7. - 8.936 - 12.088

/ y-values of the noised function
y = - 6.9291697 - 8.8853863 - 12.028021

/ the original vs the noised results
ratio = 1.0102221 1.0056963 1.0049866

We can see that the polyfit and polyval functions coded in Scilab are working pretty fine.


[1] Medrano Sanchez, C., Plaza Garcia, I.; Software libre para Calculo Numerico; Alfaomega; Mexico, DF, 2009.

From 'Curve Fitting Scilab' to Matlab home

From 'Curve Fitting' to Scilab menu


footer for matlab page

Related pages

calculator for integralsascii value of lettersfourier series tutorials examplesconversion of binary to gray codematlab loop syntaxfor loop example matlabexample of a periodic functionhow to figure square feet from inchesrgb matlabspline fit matlabmatlab gui templateascii character tablehow to solve smith chart problemsmatlab tutorial for beginnersgray code to binary conversion examplesvolt ohm calculatorformulas of simple interest and compound interestpascal triangle using ccoding in matlab tutorialascii conversion calculatorgauss jordan inverseplot3 matlabwhile loop example matlabsolving matrices in matlabfinding roots using bisection methodrand matlab1089 magic trickmat lab programsinverting a matrix in matlabtower of hanoi stepsfinding factorialbinary number to octalcharging capacitor circuithow do you calculate your bmi manuallybisection methodmatlab handledefinite integral calculationfuture value annuity calculatorformula to compute compound interestsimple interest solversolve simultaneous equations calculator onlineramp function in matlabpiece wise function examplesfibonacci series programbinary and hexadecimal number systemgauss jordan elimination matlabcalculating square feet from inchescramer rule 3x3bisection matlabproblems on nodal analysissurface plot matlabdefinite calculatormatlab randndeterminant matlabdemorgans lawhow to invert a matrix in matlabplot histogram in matlabmatlab 3d plot colormatlab chi squarehistogram in matlabdemorgans rulematlab textscansummation of trigonometric seriesintegral calculator definitesynthetic division of polynomials examplestaylor series calculator onlinepolynomial coefficients matlabfibonacci series graphhow do you calculate bmi manuallymatlab fft helpmatlab gaussian functionmatlab code for trapezoidal rulerad decay calculatorfinding simple interest rate calculatorcholesky decomposition matlabbinary code converter to numbers