Senin, 29 April 2013

Introduction to Scilab and Polynomial Curve Fitting (See the Derivation of the Program Using Scilab)



Scilab is an open source software for numerical mathematics and scientific visualization. It is capable of interactive calculations as well as automation of computations through programming. It provides all basic operations on matrices through built-in functions so that the trouble of developing and testing code for basic operations are completely avoided. Its ability to plot 2D and 3D graphs helps in visualizing the data we work with. All these make Scilab an excellent tool for teaching, especially those subjects that involve matrix operations. Further, the numerous toolboxes that are available for various specialized applications make it an important tool for research. Being compatible with Matlab®, all available Matlab M-files can be directly used in Scilab with the help of the Matlab to Scilab translator. Scicos, a hybrid dynamic systems modeler and simulator for Scilab, simplifies simulations. The greatest features of Scilab are that it is multi-platform and is free. It is available for many operating systems including Windows, Linux and MacOS X [1].

Curve Fitting
The task of finding suitable equations to represent the performance of components or thermodynamic properties (and etc) is a common preliminary step to simulating and optimizing complex systems. Data may be available in tabular or graphic form, and we seek to represent the data with an equation that is both simple and faithful. A requirement for keeping the equation simple is to choose the proper terms (exponential, polynomial, etc) to include in the equation [2]. So that with using software that is available and free, we expect to find an equation which fits our data or graphic.

This time I will try how to use free open software Scilab for polynomial curve fitting.

A typical experiment collects data related to one parameter (say x) and the corresponding value of a dependent variable (say y). These observations can be stored in two vectors, namely, x and y. Using least square regression, it is possible to compute the coefficients of a polynomial function, of some selected degree, for y in terms of x. The equation for a polynomial of degree  n can be expressed as:



where  ai, i =1 to n+1 are unknown coefficients which can be computed by solving the following set of linear simultaneous equations:

 


We can express this equation by:
[X][a]=[b]

We can solve for the unkown coefficients as follows:
a = [X]-1

Once the coefficients are determined, the predicted values of y for the observed values of x as follows:

Let us consider the sample data given below:

Let us fit a fourth order polynomial equation to the above data, of the form:

The required simultaneous equations that must be solved are as follows:

The elements of the above matrix equation are listed in the following table:

Similarly, the right hand vector is

The simultaneous equations are therefore as follows:

Solving these equations gives
a1 =2.6855, a2 =2.3015, a3 =−1.2326, a4=0.2169 and a5=−0.0118

The fourth order polynomial equation is thus

y =2.6855 + 2.3015 x − 1.2326 x2   + 0.2169 x3− 0.0118 x4

In Scilab, it is not necessary to use loops to compute the elements of the coefficient matrix and right hand vector. Instead, they can be computed through the following matrix multiplication:

The predicted values of y based on this polynomial equation can be computed as follows:
-- > y = xx*a;
We can plot the graph x against observed and predicted values with the following:
-->plot(x, y)

Now that we know how this calculation is to be done, let us write a function to automate this. Let the function interface be as follows:
Interface:[a, yf] = polyfit(x, y, n)
Input Parameters:
x = column vector of observed values of the independent variable,
y = column vector of observed values of dependent variable,
n = degree of the polynomial fit
Output parameters:
a = column vector of coefficients of the polynomial of order n that minimizes the least squares error of the fit,  ai, i =1, 2,... , n+1 where  ai is the coefficient of the term xi−1. a is a column vector.
yf= a column vector of calculated values of yfor a polynomial of order n.

The function polyfit() can be written in SciPad and then loaded into Scilab workspace:

Once the function is successfully loaded into the Scilab workspace, to fit a fifth order polynomial, we can call it as follows:
-->[a5, y(:,3)] = polyfit(x, y, 5);                                                  
-->plot(x, y, x, yf); xtitle('POLYNOMIAL CURVE FITTING', 'x', 'y');
 
Ref.:


[1] Satish Annigeri. An Introduction to Scilab. B.V. Bhoomaraddi College of Engineering & Technology, Hubli
[2] W. F. Stoecker. 1989. Design of Thermal Systems, Third Edition. McGraw-Hill, Inc.


1 komentar:

  1. I want to find the coeffizients of 5th order polynomial and to plot them in Scilab (x (t) as a trajectory, first derivative of x (t) as a speed and second derivative of x (t) as an acceleration):

    x(t)=c_0+c_1 t+c_2 t^2+c_3 t^3+c_4 t^4+c_5 t^5
    x ̇(t)=c_1+2c_2 t+3c_3 t^2+4c_4 t^3+5c_5 t^4
    x ̈(t)=2c_2+6c_3 t+12c_4 t^2+20c_5 t^3
    x ⃛(t)=6c_3+24c_4 t+60c_5 t^2

    I have: c_0 = c_1 = c_2 =0
    and
    x(0)=0; x(t_e )=x_e
    x ̇(0)=0; x ̇(t_e )=0
    x ̈(0)=0; x ̈(t_e )=0

    My question is how can I calculate the coefficient C_3, c_4 and C_5 ???

    I'll be very grateful if anyone has an idea:-)

    Thank you
    Sirrbouk

    BalasHapus