SlideShare a Scribd company logo
1 of 16
Download to read offline
www.openeering.com
powered by
DATA FITTING IN SCILAB
In this tutorial the reader can learn about data fitting, interpolation and
approximation in Scilab. Interpolation is very important in industrial applications
for data visualization and metamodeling.
Level
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
Data Fitting www.openeering.com page 2/16
Step 1: Purpose of this tutorial
Many industrial applications require the computation of a fitting function in
order to construct a model of the data.
Two main data fitting categories are available:
Interpolation which is devoted to the development of numerical
methods with the constraint that the fitting function fits exactly all
the interpolation points (measured data);
Approximation which is devoted to the development of numerical
methods where the type of function is selected and then all
parameters are obtained minimizing a certain error indicator to
obtain the best possible approximation.
The first category is useful when data does not present noise, while the
second one is used when data are affected by error and we want to
remove error and smooth our model.
Step 2: Roadmap
In the first part we present some examples of polynomial interpolation and
approximation. After we propose exercises and remarks.
Descriptions Steps
Interpolation 3-16
Approximation or curve fitting 17-20
Notes 21-22
Exercise 23
Conclusion and remarks 24-25
Data Fitting www.openeering.com page 3/16
Step 3: Interpolation
The idea of approximation is to replace a function with a function
selected from a given class of approximation function .
Two main cases exist:
Continuous function: In this case the function is known
analytically and we want to replace it with an easier function, for
example we may want to replace a complex function with a
polynomial for which integration or differentiation are easy;
Discrete function: In this case only some values of the function
are known, i.e. and we want to make a mathematical
model which is close to the unknown function such that
it is possible to establish the value of outside the known
points.
Example of approximation of a continuous function using a piecewise
approximation
Step 4: Main class of interpolation function
Several families of interpolation functions exist. The most common are:
Polynomial interpolation of degree : In this case, we
approximate data with a polynomial of degree of the form
Piecewise polynomial: In this case the interval is subdivided into
subintervals in which we define a polynomial approximation of low
degree with or without continuity on the derivatives between each
subinterval.
Example piecewise interpolation with first derivative continuous and
discontinuous connections
Data Fitting www.openeering.com page 4/16
Step 5: Test case: Runge function
The Runge function is defined as
Typically is considered in the interval [-5,5].
In our examples we consider 7 interpolation points, denoted with ,
equally distributed in the interval [-5,5].
The following code implements the Runge function and perform
visualization.
// Define Runge function
deff('[y]=f(x)','y = 1 ./(1+x.^2)');
// Interpolation points
xi = linspace(-5,5,7)'; yi = f(xi);
// Data
xrunge = linspace(-5,5,101)'; yrunge = f(xrunge);
// Plot Runge function
scf(1); clf(1);
plot(xrunge,yrunge,'b-');
plot(xi,yi,'or');
xlabel("x");
ylabel("y");
title("Runge function");
Interpolation function
Data Fitting www.openeering.com page 5/16
Step 6: Piecewise constant interpolation
Piecewise constant interpolation is the simplest way to interpolate data. It
consists on locating the nearest data value and assigning the same value
to the unknown point.
Piecewise constant interpolation
Step 7: Piecewise constant interpolation in Scilab
The Scilab command used to perform piecewise interpolation is
"interp1" where the third argument is "nearest". The fourth argument
specifies if an extrapolation method should be used when the evaluation
points are outside the interval of the interpolation points.
// Interpolation
// Evaluation points
xval = linspace(-6,6,101)';
xx_c = xval;
yy_c = interp1(xi,yi,xx_c,'nearest','extrap');
// Plot
scf(3);
clf(3);
plot(xrunge,yrunge,'k-');
plot(xx_c,yy_c,'b-');
plot(xi,yi,'or');
xlabel("x");
ylabel("y");
title("Piecewise interpolation");
legend(["Runge func";"Interp.";"Interp. val"]);
Piecewise constant interpolation
Data Fitting www.openeering.com page 6/16
Step 8: Piecewise linear interpolation
Linear interpolation is a polynomial of degree 1 that connects two points,
, and the interpolant is given by
Piecewise linear interpolation (green) and extrapolation (red)
Step 9: Linear interpolation in Scilab
The Scilab command used to perform linear interpolation is again
"interp1" but now the third argument is "linear". We can note that the
code is similar to the previous one for piecewise interpolation.
// Interpolation
xx_l = xval;
yy_l = interp1(xi,yi,xx_c,'linear','extrap');
// Plot
scf(4);
clf(4);
plot(xrunge,yrunge,'k-');
plot(xx_l,yy_l,'b-');
plot(xi,yi,'or');
xlabel("x");
ylabel("y");
title("Linear interpolation");
legend(["Runge func";"Interp.";"Interp. val"]); Piecewise linear interpolation
Data Fitting www.openeering.com page 7/16
Step 10: Polynomial interpolation
Given a set of data points , where all are different, we find
the polynomial of degree which exactly passes through these points.
Polynomial interpolations may exhibits oscillatory effects at the end of the
points. This is known as Runge phenomenon. For example, the Runge
function has this phenomenon in the interval [-5,+5] while in the interval [-
1,1] this effect is not present.
Polynomial interpolation
Step 11: Polynomial interpolation in Scilab
The Scilab command used to perform polynomial interpolation is
"polyfit" which is included in the zip file (see references). The syntax
requires the interpolation points and the degree of the polynomial
interpolation which is equal to the number of point minus one.
Note that the command "horner" evaluates a polynomial in a given set of
data.
// Import function
exec("polyfit.sci",-1);
// Interpolation
xx_p = xval;
[Pn] = polyfit(xi, yi, length(xi)-1);
yy_p = horner(Pn,xx_p);
// Plot
scf(5);
clf(5);
plot(xrunge,yrunge,'k-');
plot(xx_p,yy_p,'b-');
plot(xi,yi,'or');
xlabel("x");
ylabel("y");
title("Polynomial interpolation");
legend(["Runge func";"Interp.";"Interp. val"]);
Polynomial interpolation
Data Fitting www.openeering.com page 8/16
Step 12: Cubic spline interpolation
Cubic spline interpolation uses cubic polynomials on each interval. Then
each polynomial is connected to the next imposing further continuity
equations for the first and second derivatives.
Several kinds of splines are available and depend on how degrees of
freedom are treated. The most well-known splines are: natural, periodic,
not-a-knot and clamped.
Spline interpolation
Step 13: Cubic spline in Scilab
The Scilab command used to perform cubic spline interpolation is
"splin". Several types of splines exist and can be specified by setting the
third argument of the function. The evaluation of the spline is done using
the command "interp" where it is possible to specify the extrapolation
strategy.
// Splines examples
d = splin(xi, yi,"not_a_knot");
// d = splin(xi, yi,"natural");
// d = splin(xi, yi,"periodic");
xx_s = xval;
yy_s = interp(xx_s, xi, yi, d, "linear");
// Plot
scf(6);
clf(6);
plot(xrunge,yrunge,'k-');
plot(xx_s,yy_s,'b-');
plot(xi,yi,'or');
xlabel("x");
ylabel("y");
title("Spline interpolation");
legend(["Runge func";"Interp.";"Interp. val"]);
Cubic spline interpolation with linear extrapolation
Data Fitting www.openeering.com page 9/16
Step 14: Radial Basis Interpolation (RBF)
Radial basis function modeling consists of writing the interpolation function
as a linear combination of basis functions that depends only on the
distance of the interpolation points . This is equal to:
Using the interpolation conditions we have to solve the
following linear system
where the element .
Many radial basis functions exist, the most famous are:
Gaussian:
Multiquadratic:
Inverse multiquadratic:
Thin plate spline:
Step 15: Gaussian RBF in Scilab
In our example we use the Gaussian RBF.
// Gaussian RBF
deff('[y]=rbf_gauss(r,sigma)','y = exp(-r.^2 ./(2*sigma))');
// Plot
scf(7);
clf(7);
r = linspace(0,3);
y1 = rbf_gauss(r,0.1);
y2 = rbf_gauss(r,1.0);
y3 = rbf_gauss(r,2.0);
plot(r,y1,'k-');
plot(r,y2,'b-');
plot(r,y3,'r-');
xlabel("$r$");
ylabel("$phi(r)$");
title("Gaussian rbf");
legend(["$sigma = 0.1$";"$sigma = 1.0$";"$sigma = 2.0$"]);
Gaussian RBF for different value of sigma
Data Fitting www.openeering.com page 10/16
Step 16: RBF in Scilab
On the right we report the optimal Gaussian RBF obtained for the
interpolation of the Runge function where we have optimized the
parameters. The full code is reported in the Openeering web site.
Generally, each radial basis function depends on a parameter and this
parameter is known as modeling parameter. This parameter has effect on
the oscillation behavior of the function and the optimal choice is not an
easy task. Many techniques are available for finding the best modeling
parameter, the most famous is probably the “leave one out”.
The leave-one-out cross-validation (LOOCV) consists in using a single
point from the original set of data as a validation data. The validation of
the model is given by that point. This process can be repeated for all the
points in the data set such that, in the end, all the points are used once as
validation point. This method can produce a mean value of all these leave-
one-out errors and gives a global estimate of the model.
Since a value that estimates the model is available, we can use an
optimization solver for finding the best parameter in order to minimize the
error of the model.
Behaviour of the error versus sigma
Optimal RBF with (left), non optimal solution (right)
Data Fitting www.openeering.com page 11/16
Step 17: Approximation or curve fitting
When data is affected by errors, polynomial interpolation cannot be
appropriate since the approximation function is constrained to be through
the interpolation points. So it makes sense to fit the data starting from a
given class of functions and minimizing the difference between the data
and the class of functions, i.e.
The "polyfit" function computes the best least square polynomial
approximation of data. If we choose in the "polyfit" function, we
approximate data with linear function of the form , i.e. we
compute the linear least squares fitting.
Schematic example of min interpretation
Step 18: 1D approximation
In this example we add noise to the function and then we
make polynomial approximation of order 1 and 2.
The critical code is reported here (only case 1):
np = 100; noise = 0.7*(rand(np,1)-0.5);
x = linspace(0,2,np)';
yexact = x.^2 + x;
ynoise = yexact + noise;
// degree 1 approximation
p1 = polyfit(x, ynoise, 1);
p1val = horner(p1,x);
scf(10); clf(10);
plot(x,yexact,'k-'); plot(x,ynoise,'b-'); plot(x,p1val,'r-');
For details download the zip file with the source codes. Comparison of best fitting for degree 1 and 2
Data Fitting www.openeering.com page 12/16
Step 19: 2D approximation
In this example we want to approximate scattered data with a linear least
square fitting in two dimensions.
Given the polynomial approximation and the
scattered interpolation points , the optimal computation of the
unknown parameters requires to solve the following
overdetermined linear system
The matrix is known as the Vandermonde matrix and arises in polynomial
interpolation. The i-th row of the matrix corresponds to the polynomial
evaluated at point i-th. In our case, the i-th row corresponds to:
This is done using the Singular Value Decomposition (SVD) or
equivalently using the backslash command. Here, we report only the
solution stage. The full code can be downloaded from our web page.
// Generating random points along a plane
np = 30;
noise = 0.5*(rand(np,1)-0.5);
// Extract data
x = rand(np,1);
y = rand(np,1);
znoise = -x+2*y+noise;
// Vandermonde matrix for P(x,y) = a+b*x+c*y
V = [ones(np,1),x,y];
// Find coefficient i.e. minimize error norm
coeff = Vznoise;
Example of least square approximation in 2D
Data Fitting www.openeering.com page 13/16
Step 20: nD linear approximation
The previous example can be easily extended to an n-dimensional
problem, i.e. we search for an approximation of the form
.
Here, we report the code for the problem where some coefficients may be
equal to zero and could be deleted from the estimation problem.
The idea here implemented consists of the following strategy:
1. Perform least square approximation of data;
2. Find the zero coefficients with some pre-fixed tolerance;
3. Re-perform least square approximation of the reduce problem
where we have deleted the columns of the Vandermonde matrix
corresponding to the zero coefficients.
The code reported on the right estimates these coefficients. The full code
can be downloaded from our website.
n = 10; // Problem dimensions
neval = 100; // Number of evaluation points
pcoeff = 1:n+1; // Problem coefficient ([a0, a1, ..., an])
pcoeff([2,5,8]) = 0; // Some zero coefficients
// Evaluation points
[xdata,ydata] = generatedata(pcoeff, n, neval);
// Estimate coefficients
pstar = estimatecoeff(xdata, ydata);
// Find "zero" coefficient (define tolerance)
tol = 0.1;
zeroindex = find(abs(pstar)<=tol);
// re-estimate coefficients
pzero = estimatecoeffzero(xdata, ydata, zeroindex);
The code use the following functions:
y=evaldata(pcoeff,x)
that evaluates the polynomial defined by coefficients
on point adding a uniform
noise on the definition of the coefficients;
[xdata,ydata]=generatedata(pcoeff,n,neval)
that generates the evaluation points and their values;
pstar=estimatecoeff(xdata,ydata)
that estimates the polynomial coefficients using the least square
method;
pzero=estimatecoeffzero(xdata,ydata,zeroindex)
that estimates the polynomial coefficients where some coefficients,
specified by the vector zeroindex , are equal to zero.
Data Fitting www.openeering.com page 14/16
Step 21: Another polyfit function
The numerical solution of the polynomial interpolation problem requires to
solve the linear system with the Vandermonde matrix. Numerically, this
problem is ill-conditioned and requires an efficient strategy for a “correct”
solution. This is performed by using, for example, QR factorization.
The function “polyfit_fulldemo.sce”, provided by Konrad Kmieciak,
and contained in the zip file, is an alternative to the polyfit function.
function [u]=polyfit(x, y, n)
// Vandermonde
for k=n:-1:0
if k==n then w=0;
end
w=w+1;
Xu(:,w)=[x.^k];
end
// QR
[q r k]=qr(Xu,'0');
s = inv(r) * (q' * y); // s = r  (q' * y)
for o=1:length(s)
u(find(k(:,o)>0))=s(o);
end
endfunction
Step 22: Industrial applications of data fitting
Interpolation and approximation are two major techniques for constructing
mathematical models. Mathematical models can substitute complex model
in real-case applications.
When the original model is complex, or when it requires long and costly
evaluations (for example with finite element analysis – FEA), a simplified
model of the original model is required.
The model of the model is often called metamodel (a.k.a. response
surfaces) and the metamodeling technique is widely used in industrial
applications.
In real-case applications, when optimizing product or services, we need to
evaluate several times the model. This means that having a metamodel
that can be evaluated faster is, most of the time, the only way for finding
optimal solutions.
Metamodeling
+
Response surface
+
Self-Organizing Maps
+
Neural Networks
=
Industrial applications
Data Fitting www.openeering.com page 15/16
Step 23: Exercise on exponential decay
As an exercise, try to estimate the coefficient of a decay function of the
form:
using a linear least square fitting of data.
Hits: Use a logarithmic change of variable to reduce the problem in the
following form
Exponential decay fitting of data
Step 24: Concluding remarks and References
In this Scilab tutorial we have shown how to apply data fitting in Scilab
starting from piece-wise interpolation, presenting polynomial fitting and
cubic spline, and ending up with radial basis functions (RBF).
In the case of polynomial interpolation we used the function "polyfit"
which can be downloaded from the Scilab webpage and is included in the
provided source codes.
1. Scilab Web Page: Available: www.scilab.org.
2. Openeering: www.openeering.com.
3. Javier I. Carrero is the author of the polyfit function. The original
code is available on the Scilab.org web pages and is included in the
zip file.
Data Fitting www.openeering.com page 16/16
Step 25: Software content
To report bugs or suggest improvements please contact the Openeering
team. We thank Konrad Kmieciak for reporting us a new version of the
polyfit function.
www.openeering.com
Thank you for your attention,
Manolo Venturin
Silvia Poles
--------------
Main directory
--------------
ex0.sce : Plotting of the first figure
ex1.sce : Solution of exercise 1
interpolation.sce : Interpolation examples codes
polyfit.sci : Polyfit function
polyfit_manual.pdf : Polyfit manual
polyfit_fulldemo.sce : Another version of polyfit
estimate_lincoeff.sce : Estimantion of nD linear model
license.txt : The license file

More Related Content

What's hot

Pid controller tuning using fuzzy logic
Pid controller tuning using fuzzy logicPid controller tuning using fuzzy logic
Pid controller tuning using fuzzy logic
Roni Roshni
 
Controladores pid ajuste empírico
Controladores pid ajuste empíricoControladores pid ajuste empírico
Controladores pid ajuste empírico
SeVictor Rudas Caja
 
Unidad 1 introducción a la modelación de sistemas (1)
Unidad 1 introducción a la modelación de sistemas (1)Unidad 1 introducción a la modelación de sistemas (1)
Unidad 1 introducción a la modelación de sistemas (1)
Edwin Hernandez
 
Proportional integral and derivative PID controller
Proportional integral and derivative PID controller Proportional integral and derivative PID controller
Proportional integral and derivative PID controller
Mostafa Ragab
 

What's hot (20)

Probabilistic Power Analysis
Probabilistic Power AnalysisProbabilistic Power Analysis
Probabilistic Power Analysis
 
Diseño de compensadores
Diseño de compensadoresDiseño de compensadores
Diseño de compensadores
 
Pid controller tuning using fuzzy logic
Pid controller tuning using fuzzy logicPid controller tuning using fuzzy logic
Pid controller tuning using fuzzy logic
 
Verification Challenges and Methodologies
Verification Challenges and MethodologiesVerification Challenges and Methodologies
Verification Challenges and Methodologies
 
Dcs lec01 - introduction to discrete-time control systems
Dcs   lec01 - introduction to discrete-time control systemsDcs   lec01 - introduction to discrete-time control systems
Dcs lec01 - introduction to discrete-time control systems
 
Fpga
FpgaFpga
Fpga
 
Controladores pid ajuste empírico
Controladores pid ajuste empíricoControladores pid ajuste empírico
Controladores pid ajuste empírico
 
Sistemas de control en tiempo discreto
Sistemas de control en tiempo discretoSistemas de control en tiempo discreto
Sistemas de control en tiempo discreto
 
PID Controllers
PID Controllers PID Controllers
PID Controllers
 
Advanced sliding mode control for mechanical systems
Advanced sliding mode control for mechanical systemsAdvanced sliding mode control for mechanical systems
Advanced sliding mode control for mechanical systems
 
Mixed signal verification challenges - slides
Mixed signal verification challenges - slidesMixed signal verification challenges - slides
Mixed signal verification challenges - slides
 
Control actions
Control actionsControl actions
Control actions
 
Unidad 1 introducción a la modelación de sistemas (1)
Unidad 1 introducción a la modelación de sistemas (1)Unidad 1 introducción a la modelación de sistemas (1)
Unidad 1 introducción a la modelación de sistemas (1)
 
Proportional integral and derivative PID controller
Proportional integral and derivative PID controller Proportional integral and derivative PID controller
Proportional integral and derivative PID controller
 
diagramas de bloques
 diagramas de bloques diagramas de bloques
diagramas de bloques
 
PLC Brief
PLC BriefPLC Brief
PLC Brief
 
Ppt on control system
Ppt on control systemPpt on control system
Ppt on control system
 
Field Programmable Gate Array: Building Blocks and Interconnections
Field Programmable Gate Array: Building Blocks and InterconnectionsField Programmable Gate Array: Building Blocks and Interconnections
Field Programmable Gate Array: Building Blocks and Interconnections
 
Concepts of Behavioral modelling in Verilog HDL
Concepts of Behavioral modelling in Verilog HDLConcepts of Behavioral modelling in Verilog HDL
Concepts of Behavioral modelling in Verilog HDL
 
Design of all digital phase locked loop (d pll) with fast acquisition time
Design of all digital phase locked loop (d pll) with fast acquisition timeDesign of all digital phase locked loop (d pll) with fast acquisition time
Design of all digital phase locked loop (d pll) with fast acquisition time
 

Viewers also liked

Viewers also liked (7)

How to develop a Graphical User Interface (GUI) in Scilab
How to develop a Graphical User Interface (GUI) in ScilabHow to develop a Graphical User Interface (GUI) in Scilab
How to develop a Graphical User Interface (GUI) in Scilab
 
Modeling an ODE: 3 different approaches - Part 3
Modeling an ODE: 3 different approaches - Part 3Modeling an ODE: 3 different approaches - Part 3
Modeling an ODE: 3 different approaches - Part 3
 
Data mining
Data miningData mining
Data mining
 
Introduction to Control systems in scilab
Introduction to Control systems in scilabIntroduction to Control systems in scilab
Introduction to Control systems in scilab
 
Modeling an ODE: 3 different approaches - Part 1
Modeling an ODE: 3 different approaches - Part 1Modeling an ODE: 3 different approaches - Part 1
Modeling an ODE: 3 different approaches - Part 1
 
Modeling an ODE: 3 different approaches - Part 2
Modeling an ODE: 3 different approaches - Part 2Modeling an ODE: 3 different approaches - Part 2
Modeling an ODE: 3 different approaches - Part 2
 
Customizing Xcos with new Blocks and Palette
Customizing Xcos with new Blocks and PaletteCustomizing Xcos with new Blocks and Palette
Customizing Xcos with new Blocks and Palette
 

Similar to Data fitting in Scilab - Tutorial

Ai_Project_report
Ai_Project_reportAi_Project_report
Ai_Project_report
Ravi Gupta
 
EE660_Report_YaxinLiu_8448347171
EE660_Report_YaxinLiu_8448347171EE660_Report_YaxinLiu_8448347171
EE660_Report_YaxinLiu_8448347171
Yaxin Liu
 
interpolation-and-its-application-180107160107.pptx
interpolation-and-its-application-180107160107.pptxinterpolation-and-its-application-180107160107.pptx
interpolation-and-its-application-180107160107.pptx
SomitSamanto1
 
Ning_Mei.ASSIGN02
Ning_Mei.ASSIGN02Ning_Mei.ASSIGN02
Ning_Mei.ASSIGN02
宁 梅
 

Similar to Data fitting in Scilab - Tutorial (20)

working with python
working with pythonworking with python
working with python
 
maXbox starter69 Machine Learning VII
maXbox starter69 Machine Learning VIImaXbox starter69 Machine Learning VII
maXbox starter69 Machine Learning VII
 
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
 
05 dataflow
05 dataflow05 dataflow
05 dataflow
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with Scala
 
Pyro Primer
Pyro PrimerPyro Primer
Pyro Primer
 
Ai_Project_report
Ai_Project_reportAi_Project_report
Ai_Project_report
 
EE660_Report_YaxinLiu_8448347171
EE660_Report_YaxinLiu_8448347171EE660_Report_YaxinLiu_8448347171
EE660_Report_YaxinLiu_8448347171
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
 
icpr_2012
icpr_2012icpr_2012
icpr_2012
 
BPstudy sklearn 20180925
BPstudy sklearn 20180925BPstudy sklearn 20180925
BPstudy sklearn 20180925
 
Introduction to Julia
Introduction to JuliaIntroduction to Julia
Introduction to Julia
 
Python for Data Science
Python for Data SciencePython for Data Science
Python for Data Science
 
Interpolation and-its-application
Interpolation and-its-applicationInterpolation and-its-application
Interpolation and-its-application
 
Matlab integration
Matlab integrationMatlab integration
Matlab integration
 
A Systematic Approach To Probabilistic Pointer Analysis
A Systematic Approach To Probabilistic Pointer AnalysisA Systematic Approach To Probabilistic Pointer Analysis
A Systematic Approach To Probabilistic Pointer Analysis
 
interpolation-and-its-application-180107160107.pptx
interpolation-and-its-application-180107160107.pptxinterpolation-and-its-application-180107160107.pptx
interpolation-and-its-application-180107160107.pptx
 
Ning_Mei.ASSIGN02
Ning_Mei.ASSIGN02Ning_Mei.ASSIGN02
Ning_Mei.ASSIGN02
 
Machine Learning Guide maXbox Starter62
Machine Learning Guide maXbox Starter62Machine Learning Guide maXbox Starter62
Machine Learning Guide maXbox Starter62
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo
 

More from Scilab

More from Scilab (20)

Statistical Analysis for Robust Design
Statistical Analysis for Robust DesignStatistical Analysis for Robust Design
Statistical Analysis for Robust Design
 
Electric motor optimization
Electric motor optimizationElectric motor optimization
Electric motor optimization
 
Asteroidlanding - Scilab conference 2019 Keynote
Asteroidlanding - Scilab conference 2019 KeynoteAsteroidlanding - Scilab conference 2019 Keynote
Asteroidlanding - Scilab conference 2019 Keynote
 
Faster Time to Market using Scilab/XCOS/X2C for motor control algorithm devel...
Faster Time to Market using Scilab/XCOS/X2C for motor control algorithm devel...Faster Time to Market using Scilab/XCOS/X2C for motor control algorithm devel...
Faster Time to Market using Scilab/XCOS/X2C for motor control algorithm devel...
 
Scilab and Xcos for Very Low Earth Orbits satellites modelling
Scilab and Xcos for Very Low Earth Orbits satellites modellingScilab and Xcos for Very Low Earth Orbits satellites modelling
Scilab and Xcos for Very Low Earth Orbits satellites modelling
 
X2C -a tool for model-based control development and automated code generation...
X2C -a tool for model-based control development and automated code generation...X2C -a tool for model-based control development and automated code generation...
X2C -a tool for model-based control development and automated code generation...
 
A Real-Time Interface for Xcos – an illustrative demonstration using a batter...
A Real-Time Interface for Xcos – an illustrative demonstration using a batter...A Real-Time Interface for Xcos – an illustrative demonstration using a batter...
A Real-Time Interface for Xcos – an illustrative demonstration using a batter...
 
Aircraft Simulation Model and Flight Control Laws Design Using Scilab and XCos
Aircraft Simulation Model and Flight Control Laws Design Using Scilab and XCosAircraft Simulation Model and Flight Control Laws Design Using Scilab and XCos
Aircraft Simulation Model and Flight Control Laws Design Using Scilab and XCos
 
Scilab for real dummies j.heikell - part3
Scilab for real dummies j.heikell - part3Scilab for real dummies j.heikell - part3
Scilab for real dummies j.heikell - part3
 
Scilab for real dummies j.heikell - part 2
Scilab for real dummies j.heikell - part 2Scilab for real dummies j.heikell - part 2
Scilab for real dummies j.heikell - part 2
 
Scilab for real dummies j.heikell - part 1
Scilab for real dummies j.heikell - part 1Scilab for real dummies j.heikell - part 1
Scilab for real dummies j.heikell - part 1
 
Multiobjective optimization and Genetic algorithms in Scilab
Multiobjective optimization and Genetic algorithms in ScilabMultiobjective optimization and Genetic algorithms in Scilab
Multiobjective optimization and Genetic algorithms in Scilab
 
Scilab optimization workshop
Scilab optimization workshop Scilab optimization workshop
Scilab optimization workshop
 
INRA @ Scilab Conference 2018
INRA @ Scilab Conference 2018INRA @ Scilab Conference 2018
INRA @ Scilab Conference 2018
 
Qualcomm @ Scilab Conference 2018
Qualcomm @ Scilab Conference 2018Qualcomm @ Scilab Conference 2018
Qualcomm @ Scilab Conference 2018
 
Sanofi @ Scilab Conference 2018
Sanofi @ Scilab Conference 2018Sanofi @ Scilab Conference 2018
Sanofi @ Scilab Conference 2018
 
University of Applied Science Esslingen @ Scilab Conference 2018
University of Applied Science Esslingen @ Scilab Conference 2018University of Applied Science Esslingen @ Scilab Conference 2018
University of Applied Science Esslingen @ Scilab Conference 2018
 
DLR @ Scilab Conference 2018
DLR @ Scilab Conference 2018DLR @ Scilab Conference 2018
DLR @ Scilab Conference 2018
 
Fraunhofer IIS @ Scilab Conference 2018
Fraunhofer IIS @ Scilab Conference 2018Fraunhofer IIS @ Scilab Conference 2018
Fraunhofer IIS @ Scilab Conference 2018
 
Arcelormittal @ Scilab Conference 2018
Arcelormittal @ Scilab Conference 2018Arcelormittal @ Scilab Conference 2018
Arcelormittal @ Scilab Conference 2018
 

Recently uploaded

VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Dr.Costas Sachpazis
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
Tonystark477637
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 

Recently uploaded (20)

University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 

Data fitting in Scilab - Tutorial

  • 1. www.openeering.com powered by DATA FITTING IN SCILAB In this tutorial the reader can learn about data fitting, interpolation and approximation in Scilab. Interpolation is very important in industrial applications for data visualization and metamodeling. Level This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
  • 2. Data Fitting www.openeering.com page 2/16 Step 1: Purpose of this tutorial Many industrial applications require the computation of a fitting function in order to construct a model of the data. Two main data fitting categories are available: Interpolation which is devoted to the development of numerical methods with the constraint that the fitting function fits exactly all the interpolation points (measured data); Approximation which is devoted to the development of numerical methods where the type of function is selected and then all parameters are obtained minimizing a certain error indicator to obtain the best possible approximation. The first category is useful when data does not present noise, while the second one is used when data are affected by error and we want to remove error and smooth our model. Step 2: Roadmap In the first part we present some examples of polynomial interpolation and approximation. After we propose exercises and remarks. Descriptions Steps Interpolation 3-16 Approximation or curve fitting 17-20 Notes 21-22 Exercise 23 Conclusion and remarks 24-25
  • 3. Data Fitting www.openeering.com page 3/16 Step 3: Interpolation The idea of approximation is to replace a function with a function selected from a given class of approximation function . Two main cases exist: Continuous function: In this case the function is known analytically and we want to replace it with an easier function, for example we may want to replace a complex function with a polynomial for which integration or differentiation are easy; Discrete function: In this case only some values of the function are known, i.e. and we want to make a mathematical model which is close to the unknown function such that it is possible to establish the value of outside the known points. Example of approximation of a continuous function using a piecewise approximation Step 4: Main class of interpolation function Several families of interpolation functions exist. The most common are: Polynomial interpolation of degree : In this case, we approximate data with a polynomial of degree of the form Piecewise polynomial: In this case the interval is subdivided into subintervals in which we define a polynomial approximation of low degree with or without continuity on the derivatives between each subinterval. Example piecewise interpolation with first derivative continuous and discontinuous connections
  • 4. Data Fitting www.openeering.com page 4/16 Step 5: Test case: Runge function The Runge function is defined as Typically is considered in the interval [-5,5]. In our examples we consider 7 interpolation points, denoted with , equally distributed in the interval [-5,5]. The following code implements the Runge function and perform visualization. // Define Runge function deff('[y]=f(x)','y = 1 ./(1+x.^2)'); // Interpolation points xi = linspace(-5,5,7)'; yi = f(xi); // Data xrunge = linspace(-5,5,101)'; yrunge = f(xrunge); // Plot Runge function scf(1); clf(1); plot(xrunge,yrunge,'b-'); plot(xi,yi,'or'); xlabel("x"); ylabel("y"); title("Runge function"); Interpolation function
  • 5. Data Fitting www.openeering.com page 5/16 Step 6: Piecewise constant interpolation Piecewise constant interpolation is the simplest way to interpolate data. It consists on locating the nearest data value and assigning the same value to the unknown point. Piecewise constant interpolation Step 7: Piecewise constant interpolation in Scilab The Scilab command used to perform piecewise interpolation is "interp1" where the third argument is "nearest". The fourth argument specifies if an extrapolation method should be used when the evaluation points are outside the interval of the interpolation points. // Interpolation // Evaluation points xval = linspace(-6,6,101)'; xx_c = xval; yy_c = interp1(xi,yi,xx_c,'nearest','extrap'); // Plot scf(3); clf(3); plot(xrunge,yrunge,'k-'); plot(xx_c,yy_c,'b-'); plot(xi,yi,'or'); xlabel("x"); ylabel("y"); title("Piecewise interpolation"); legend(["Runge func";"Interp.";"Interp. val"]); Piecewise constant interpolation
  • 6. Data Fitting www.openeering.com page 6/16 Step 8: Piecewise linear interpolation Linear interpolation is a polynomial of degree 1 that connects two points, , and the interpolant is given by Piecewise linear interpolation (green) and extrapolation (red) Step 9: Linear interpolation in Scilab The Scilab command used to perform linear interpolation is again "interp1" but now the third argument is "linear". We can note that the code is similar to the previous one for piecewise interpolation. // Interpolation xx_l = xval; yy_l = interp1(xi,yi,xx_c,'linear','extrap'); // Plot scf(4); clf(4); plot(xrunge,yrunge,'k-'); plot(xx_l,yy_l,'b-'); plot(xi,yi,'or'); xlabel("x"); ylabel("y"); title("Linear interpolation"); legend(["Runge func";"Interp.";"Interp. val"]); Piecewise linear interpolation
  • 7. Data Fitting www.openeering.com page 7/16 Step 10: Polynomial interpolation Given a set of data points , where all are different, we find the polynomial of degree which exactly passes through these points. Polynomial interpolations may exhibits oscillatory effects at the end of the points. This is known as Runge phenomenon. For example, the Runge function has this phenomenon in the interval [-5,+5] while in the interval [- 1,1] this effect is not present. Polynomial interpolation Step 11: Polynomial interpolation in Scilab The Scilab command used to perform polynomial interpolation is "polyfit" which is included in the zip file (see references). The syntax requires the interpolation points and the degree of the polynomial interpolation which is equal to the number of point minus one. Note that the command "horner" evaluates a polynomial in a given set of data. // Import function exec("polyfit.sci",-1); // Interpolation xx_p = xval; [Pn] = polyfit(xi, yi, length(xi)-1); yy_p = horner(Pn,xx_p); // Plot scf(5); clf(5); plot(xrunge,yrunge,'k-'); plot(xx_p,yy_p,'b-'); plot(xi,yi,'or'); xlabel("x"); ylabel("y"); title("Polynomial interpolation"); legend(["Runge func";"Interp.";"Interp. val"]); Polynomial interpolation
  • 8. Data Fitting www.openeering.com page 8/16 Step 12: Cubic spline interpolation Cubic spline interpolation uses cubic polynomials on each interval. Then each polynomial is connected to the next imposing further continuity equations for the first and second derivatives. Several kinds of splines are available and depend on how degrees of freedom are treated. The most well-known splines are: natural, periodic, not-a-knot and clamped. Spline interpolation Step 13: Cubic spline in Scilab The Scilab command used to perform cubic spline interpolation is "splin". Several types of splines exist and can be specified by setting the third argument of the function. The evaluation of the spline is done using the command "interp" where it is possible to specify the extrapolation strategy. // Splines examples d = splin(xi, yi,"not_a_knot"); // d = splin(xi, yi,"natural"); // d = splin(xi, yi,"periodic"); xx_s = xval; yy_s = interp(xx_s, xi, yi, d, "linear"); // Plot scf(6); clf(6); plot(xrunge,yrunge,'k-'); plot(xx_s,yy_s,'b-'); plot(xi,yi,'or'); xlabel("x"); ylabel("y"); title("Spline interpolation"); legend(["Runge func";"Interp.";"Interp. val"]); Cubic spline interpolation with linear extrapolation
  • 9. Data Fitting www.openeering.com page 9/16 Step 14: Radial Basis Interpolation (RBF) Radial basis function modeling consists of writing the interpolation function as a linear combination of basis functions that depends only on the distance of the interpolation points . This is equal to: Using the interpolation conditions we have to solve the following linear system where the element . Many radial basis functions exist, the most famous are: Gaussian: Multiquadratic: Inverse multiquadratic: Thin plate spline: Step 15: Gaussian RBF in Scilab In our example we use the Gaussian RBF. // Gaussian RBF deff('[y]=rbf_gauss(r,sigma)','y = exp(-r.^2 ./(2*sigma))'); // Plot scf(7); clf(7); r = linspace(0,3); y1 = rbf_gauss(r,0.1); y2 = rbf_gauss(r,1.0); y3 = rbf_gauss(r,2.0); plot(r,y1,'k-'); plot(r,y2,'b-'); plot(r,y3,'r-'); xlabel("$r$"); ylabel("$phi(r)$"); title("Gaussian rbf"); legend(["$sigma = 0.1$";"$sigma = 1.0$";"$sigma = 2.0$"]); Gaussian RBF for different value of sigma
  • 10. Data Fitting www.openeering.com page 10/16 Step 16: RBF in Scilab On the right we report the optimal Gaussian RBF obtained for the interpolation of the Runge function where we have optimized the parameters. The full code is reported in the Openeering web site. Generally, each radial basis function depends on a parameter and this parameter is known as modeling parameter. This parameter has effect on the oscillation behavior of the function and the optimal choice is not an easy task. Many techniques are available for finding the best modeling parameter, the most famous is probably the “leave one out”. The leave-one-out cross-validation (LOOCV) consists in using a single point from the original set of data as a validation data. The validation of the model is given by that point. This process can be repeated for all the points in the data set such that, in the end, all the points are used once as validation point. This method can produce a mean value of all these leave- one-out errors and gives a global estimate of the model. Since a value that estimates the model is available, we can use an optimization solver for finding the best parameter in order to minimize the error of the model. Behaviour of the error versus sigma Optimal RBF with (left), non optimal solution (right)
  • 11. Data Fitting www.openeering.com page 11/16 Step 17: Approximation or curve fitting When data is affected by errors, polynomial interpolation cannot be appropriate since the approximation function is constrained to be through the interpolation points. So it makes sense to fit the data starting from a given class of functions and minimizing the difference between the data and the class of functions, i.e. The "polyfit" function computes the best least square polynomial approximation of data. If we choose in the "polyfit" function, we approximate data with linear function of the form , i.e. we compute the linear least squares fitting. Schematic example of min interpretation Step 18: 1D approximation In this example we add noise to the function and then we make polynomial approximation of order 1 and 2. The critical code is reported here (only case 1): np = 100; noise = 0.7*(rand(np,1)-0.5); x = linspace(0,2,np)'; yexact = x.^2 + x; ynoise = yexact + noise; // degree 1 approximation p1 = polyfit(x, ynoise, 1); p1val = horner(p1,x); scf(10); clf(10); plot(x,yexact,'k-'); plot(x,ynoise,'b-'); plot(x,p1val,'r-'); For details download the zip file with the source codes. Comparison of best fitting for degree 1 and 2
  • 12. Data Fitting www.openeering.com page 12/16 Step 19: 2D approximation In this example we want to approximate scattered data with a linear least square fitting in two dimensions. Given the polynomial approximation and the scattered interpolation points , the optimal computation of the unknown parameters requires to solve the following overdetermined linear system The matrix is known as the Vandermonde matrix and arises in polynomial interpolation. The i-th row of the matrix corresponds to the polynomial evaluated at point i-th. In our case, the i-th row corresponds to: This is done using the Singular Value Decomposition (SVD) or equivalently using the backslash command. Here, we report only the solution stage. The full code can be downloaded from our web page. // Generating random points along a plane np = 30; noise = 0.5*(rand(np,1)-0.5); // Extract data x = rand(np,1); y = rand(np,1); znoise = -x+2*y+noise; // Vandermonde matrix for P(x,y) = a+b*x+c*y V = [ones(np,1),x,y]; // Find coefficient i.e. minimize error norm coeff = Vznoise; Example of least square approximation in 2D
  • 13. Data Fitting www.openeering.com page 13/16 Step 20: nD linear approximation The previous example can be easily extended to an n-dimensional problem, i.e. we search for an approximation of the form . Here, we report the code for the problem where some coefficients may be equal to zero and could be deleted from the estimation problem. The idea here implemented consists of the following strategy: 1. Perform least square approximation of data; 2. Find the zero coefficients with some pre-fixed tolerance; 3. Re-perform least square approximation of the reduce problem where we have deleted the columns of the Vandermonde matrix corresponding to the zero coefficients. The code reported on the right estimates these coefficients. The full code can be downloaded from our website. n = 10; // Problem dimensions neval = 100; // Number of evaluation points pcoeff = 1:n+1; // Problem coefficient ([a0, a1, ..., an]) pcoeff([2,5,8]) = 0; // Some zero coefficients // Evaluation points [xdata,ydata] = generatedata(pcoeff, n, neval); // Estimate coefficients pstar = estimatecoeff(xdata, ydata); // Find "zero" coefficient (define tolerance) tol = 0.1; zeroindex = find(abs(pstar)<=tol); // re-estimate coefficients pzero = estimatecoeffzero(xdata, ydata, zeroindex); The code use the following functions: y=evaldata(pcoeff,x) that evaluates the polynomial defined by coefficients on point adding a uniform noise on the definition of the coefficients; [xdata,ydata]=generatedata(pcoeff,n,neval) that generates the evaluation points and their values; pstar=estimatecoeff(xdata,ydata) that estimates the polynomial coefficients using the least square method; pzero=estimatecoeffzero(xdata,ydata,zeroindex) that estimates the polynomial coefficients where some coefficients, specified by the vector zeroindex , are equal to zero.
  • 14. Data Fitting www.openeering.com page 14/16 Step 21: Another polyfit function The numerical solution of the polynomial interpolation problem requires to solve the linear system with the Vandermonde matrix. Numerically, this problem is ill-conditioned and requires an efficient strategy for a “correct” solution. This is performed by using, for example, QR factorization. The function “polyfit_fulldemo.sce”, provided by Konrad Kmieciak, and contained in the zip file, is an alternative to the polyfit function. function [u]=polyfit(x, y, n) // Vandermonde for k=n:-1:0 if k==n then w=0; end w=w+1; Xu(:,w)=[x.^k]; end // QR [q r k]=qr(Xu,'0'); s = inv(r) * (q' * y); // s = r (q' * y) for o=1:length(s) u(find(k(:,o)>0))=s(o); end endfunction Step 22: Industrial applications of data fitting Interpolation and approximation are two major techniques for constructing mathematical models. Mathematical models can substitute complex model in real-case applications. When the original model is complex, or when it requires long and costly evaluations (for example with finite element analysis – FEA), a simplified model of the original model is required. The model of the model is often called metamodel (a.k.a. response surfaces) and the metamodeling technique is widely used in industrial applications. In real-case applications, when optimizing product or services, we need to evaluate several times the model. This means that having a metamodel that can be evaluated faster is, most of the time, the only way for finding optimal solutions. Metamodeling + Response surface + Self-Organizing Maps + Neural Networks = Industrial applications
  • 15. Data Fitting www.openeering.com page 15/16 Step 23: Exercise on exponential decay As an exercise, try to estimate the coefficient of a decay function of the form: using a linear least square fitting of data. Hits: Use a logarithmic change of variable to reduce the problem in the following form Exponential decay fitting of data Step 24: Concluding remarks and References In this Scilab tutorial we have shown how to apply data fitting in Scilab starting from piece-wise interpolation, presenting polynomial fitting and cubic spline, and ending up with radial basis functions (RBF). In the case of polynomial interpolation we used the function "polyfit" which can be downloaded from the Scilab webpage and is included in the provided source codes. 1. Scilab Web Page: Available: www.scilab.org. 2. Openeering: www.openeering.com. 3. Javier I. Carrero is the author of the polyfit function. The original code is available on the Scilab.org web pages and is included in the zip file.
  • 16. Data Fitting www.openeering.com page 16/16 Step 25: Software content To report bugs or suggest improvements please contact the Openeering team. We thank Konrad Kmieciak for reporting us a new version of the polyfit function. www.openeering.com Thank you for your attention, Manolo Venturin Silvia Poles -------------- Main directory -------------- ex0.sce : Plotting of the first figure ex1.sce : Solution of exercise 1 interpolation.sce : Interpolation examples codes polyfit.sci : Polyfit function polyfit_manual.pdf : Polyfit manual polyfit_fulldemo.sce : Another version of polyfit estimate_lincoeff.sce : Estimantion of nD linear model license.txt : The license file