SlideShare a Scribd company logo
1 of 18
Piecewise Functions
In Scilab
Piecewise Functions
• A piecewise function is a function which is
defined by multiple sub functions, each
sub function applying to a certain interval
of the main function's domain.
Piecewise Functions
• I’ll show you two ways to define and plot
them in Scilab:
1. With iterations, one element at a time.
2. Without iterations, the vectorized way.
(I’m using Scilab ver 5.4.0 for Win 7 - 64 bits)
Piecewise Functions
1.- Using Iterations
• First, you are going to define your piecewise
function, where you’ll consider a scalar number
as input. You’ll verify each interval and assign the
appropriate value.
• Second, you’ll call that function for all the
necessary elements.
• At the end, you’ll have two vectors, x and y, so
that you can plot your initial function.
1.- Using Iterations – Define PW
// Define your function assuming that you'll
// get a scalar as input
function y = pw1(x)
// Filter and evaluate your first interval
if x <= 1 then y = -x/3 + 4/3;
// Filter and evaluate your second interval
elseif (1 < x) & (x <= 3) then y = x^2/6 + x/3 + .5;
// Filter and define your remaining intervals
else y = .5*x + 1.5;
end
endfunction
1.- Using Iterations – Call Function
// Clear your command window and clear memory
clc, clear
// Make sure that Scilab can see your function.
// Load it into memory and add the full path if necessary
exec('C:UsersUsuarioDocumentsScilab_docspw1.sci');
// This is your range of interest
x = -2 : .2 : 5;
// Call the function and evaluate element-by-element
for ix = 1 : length(x)
y(ix) = pw1(x(ix));
end
// Now you have your vectors ready to plot
1.- Using Iterations – Call Function
// Plot and add labels if needed
plot(x, y, 'ro-')
title('Piecewise in Scilab - Example 1');
xlabel('x');
ylabel('y');
2.- Using Vectorization
• First, you are going to define your piecewise
function, where you’ll consider a vector as input.
You’ll find values for each interval and assign the
appropriate values.
• Second, you’ll call that function as you would for
any other function that takes vectors.
• At the end, you’ll have two arrays, x and y, so that
you can plot the function under study.
2.- Using Vectorization
x = -2 : .2 : 5;
y = pw2(x);
plot(x, y)
Ideally, you should type something like this
and get the same plot shown above
interval of interest
function to be defined
call the plot, just as it’s done
with any other function
2.- Using Vectorization
The interesting part is how to define the
piecewise function without going element-by-
element in the domain, but going instead
interval-by-interval.
To accomplish this, we’ll use two ideas:
• Specially selected indices.
• Function find.
2.- Using Vectorization
In Scilab, if we have vector x = [-2 -1 0 1 2 3 4 5 6 7 8 9], and do this:
i2 = x(0 < x & x <= 3)
we’ll take all the values in vector x that meet the condition 0 < x ≤ 3, that is,
i2 = [1 2 3]
If we do:
i3 = x(3 < x & x <= 8)
we’ll take all the values in vector x that meet the condition 3 < x ≤ 8, thus
i3 = [4 5 6 7 8]
First important concept: special indices
2.- Using Vectorization
In Scilab, if we have vector x = [-2 -1 0 1 2 3 4 5 6 7 8 9], and do this:
find(0 < x & x <= 3)
we’ll find the indices (not values) in vector x that meet 0 < x ≤ 3, so we’ll get
the vector [4 5 6].
If we do:
find(3 < x & x <= 8)
we’ll get the vector [7 8 9 10 11]
Second important concept: function find
2.- Using Vectorization – Define PW
Putting all together, defining the function under study:
// Define your function considering a vector as an input
function y = pw2(x)
// Find the indices for the first interval
ix1 = find(x <= 1);
// Assign the appropriate values to the correct values
y(ix1) = -x(ix1)/3 + 4/3;
// Now the second interval, repeat the concept
ix2 = find(1 < x & x <= 3);
y(ix2) = x(ix2).^2/6 + x(ix2)/3 + .5;
2.- Using Vectorization – Define PW
// Now, the last interval for this function
ix3 = find(x > 3);
y(ix3) = .5*x(ix3) + 1.5;
endfunction
2.- Using Vectorization– Call Function
// Clear your command window and clear memory
clc, clear
// Make sure that Scilab can see your function.
// Load it into memory and add the full path if necessary
exec('C:UsersUsuarioDocumentsScilab_docspw2.sci');
// This is your range of interest
x = -2 : .2 : 5;
// Call the function - don’t need iterations
y = pw2(x);
// Now you have your vectors ready to plot
2.- Using Vectorization– Call Function
// Plot and add labels if needed
plot(x, y, 'bo-')
title('Piecewise in Scilab - Example 2');
xlabel('x');
ylabel('f(x)');
For more examples and details, visit:
matrixlab-examples.com/scilab-piecewise-function.html

More Related Content

What's hot

Piecewise Functions in Matlab
Piecewise Functions in MatlabPiecewise Functions in Matlab
Piecewise Functions in MatlabJorge Jasso
 
Cauchy riemann equations
Cauchy riemann equationsCauchy riemann equations
Cauchy riemann equationssajidpk92
 
Math powerpoint- Polynomial equations and graph of polynomial functions
Math powerpoint- Polynomial equations and graph of polynomial functionsMath powerpoint- Polynomial equations and graph of polynomial functions
Math powerpoint- Polynomial equations and graph of polynomial functionsJana Marie Aguilar
 
Lesson 5: Continuity (slides)
Lesson 5: Continuity (slides)Lesson 5: Continuity (slides)
Lesson 5: Continuity (slides)Matthew Leingang
 
Advanced Engineering Mathematics Solutions Manual.pdf
Advanced Engineering Mathematics Solutions Manual.pdfAdvanced Engineering Mathematics Solutions Manual.pdf
Advanced Engineering Mathematics Solutions Manual.pdfWhitney Anderson
 
Bisection & Regual falsi methods
Bisection & Regual falsi methodsBisection & Regual falsi methods
Bisection & Regual falsi methodsDivya Bhatia
 
industrial electronics notes by Ali Azam.
industrial electronics notes by Ali Azam.industrial electronics notes by Ali Azam.
industrial electronics notes by Ali Azam.Ali Azam
 
Introduction to Counters
Introduction to CountersIntroduction to Counters
Introduction to CountersISMT College
 
5.8 Graphing quadratic inequalities
5.8 Graphing quadratic inequalities5.8 Graphing quadratic inequalities
5.8 Graphing quadratic inequalitiesswartzje
 
Parallel Adder and Subtractor
Parallel Adder and SubtractorParallel Adder and Subtractor
Parallel Adder and SubtractorSmit Shah
 
Piecewise Functions
Piecewise FunctionsPiecewise Functions
Piecewise Functionsktini
 
Degital 1
Degital 1Degital 1
Degital 1hnaita
 
Numerical integration
Numerical integrationNumerical integration
Numerical integrationTarun Gehlot
 
adder and subtractor
 adder and subtractor adder and subtractor
adder and subtractorUnsa Shakir
 
Lesson 15: Inverse Trigonometric Functions
Lesson 15: Inverse Trigonometric FunctionsLesson 15: Inverse Trigonometric Functions
Lesson 15: Inverse Trigonometric FunctionsMatthew Leingang
 

What's hot (20)

Ripple Carry Adder
Ripple Carry AdderRipple Carry Adder
Ripple Carry Adder
 
Piecewise Functions in Matlab
Piecewise Functions in MatlabPiecewise Functions in Matlab
Piecewise Functions in Matlab
 
Vector analysis
Vector analysisVector analysis
Vector analysis
 
Cauchy riemann equations
Cauchy riemann equationsCauchy riemann equations
Cauchy riemann equations
 
virtuoso
virtuosovirtuoso
virtuoso
 
Math powerpoint- Polynomial equations and graph of polynomial functions
Math powerpoint- Polynomial equations and graph of polynomial functionsMath powerpoint- Polynomial equations and graph of polynomial functions
Math powerpoint- Polynomial equations and graph of polynomial functions
 
Lesson 5: Continuity (slides)
Lesson 5: Continuity (slides)Lesson 5: Continuity (slides)
Lesson 5: Continuity (slides)
 
Advanced Engineering Mathematics Solutions Manual.pdf
Advanced Engineering Mathematics Solutions Manual.pdfAdvanced Engineering Mathematics Solutions Manual.pdf
Advanced Engineering Mathematics Solutions Manual.pdf
 
Bisection & Regual falsi methods
Bisection & Regual falsi methodsBisection & Regual falsi methods
Bisection & Regual falsi methods
 
industrial electronics notes by Ali Azam.
industrial electronics notes by Ali Azam.industrial electronics notes by Ali Azam.
industrial electronics notes by Ali Azam.
 
Introduction to Counters
Introduction to CountersIntroduction to Counters
Introduction to Counters
 
5.8 Graphing quadratic inequalities
5.8 Graphing quadratic inequalities5.8 Graphing quadratic inequalities
5.8 Graphing quadratic inequalities
 
Parallel Adder and Subtractor
Parallel Adder and SubtractorParallel Adder and Subtractor
Parallel Adder and Subtractor
 
Riemann sumsdefiniteintegrals
Riemann sumsdefiniteintegralsRiemann sumsdefiniteintegrals
Riemann sumsdefiniteintegrals
 
Piecewise Functions
Piecewise FunctionsPiecewise Functions
Piecewise Functions
 
Degital 1
Degital 1Degital 1
Degital 1
 
kmap
kmapkmap
kmap
 
Numerical integration
Numerical integrationNumerical integration
Numerical integration
 
adder and subtractor
 adder and subtractor adder and subtractor
adder and subtractor
 
Lesson 15: Inverse Trigonometric Functions
Lesson 15: Inverse Trigonometric FunctionsLesson 15: Inverse Trigonometric Functions
Lesson 15: Inverse Trigonometric Functions
 

Similar to Scilab - Piecewise Functions

Principles of functional progrmming in scala
Principles of functional progrmming in scalaPrinciples of functional progrmming in scala
Principles of functional progrmming in scalaehsoon
 
Functional Objects & Function and Closures
Functional Objects  & Function and ClosuresFunctional Objects  & Function and Closures
Functional Objects & Function and ClosuresSandip Kumar
 
Functional Objects & Function and Closures
Functional Objects  & Function and ClosuresFunctional Objects  & Function and Closures
Functional Objects & Function and ClosuresSandip Kumar
 
Learn a language : LISP
Learn a language : LISPLearn a language : LISP
Learn a language : LISPDevnology
 
ScalaLanguage_ch_4_5.pptx
ScalaLanguage_ch_4_5.pptxScalaLanguage_ch_4_5.pptx
ScalaLanguage_ch_4_5.pptxjkapardhi
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with ScalaNeelkanth Sachdeva
 
C++ and Data Structure.ppt
C++ and Data Structure.pptC++ and Data Structure.ppt
C++ and Data Structure.pptRich Alex
 
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 - part3Scilab
 
Functional Objects & Function and Closures
Functional Objects  & Function and ClosuresFunctional Objects  & Function and Closures
Functional Objects & Function and ClosuresSandip Kumar
 
Database structure Structures Link list and trees and Recurison complete
Database structure Structures Link list and trees and Recurison complete  Database structure Structures Link list and trees and Recurison complete
Database structure Structures Link list and trees and Recurison complete Adnan abid
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With ScalaKnoldus Inc.
 
Python programming- Part IV(Functions)
Python programming- Part IV(Functions)Python programming- Part IV(Functions)
Python programming- Part IV(Functions)Megha V
 
COMPANION TO MATRICES SESSION II.pptx
COMPANION TO MATRICES SESSION II.pptxCOMPANION TO MATRICES SESSION II.pptx
COMPANION TO MATRICES SESSION II.pptximman gwu
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...Philip Schwarz
 

Similar to Scilab - Piecewise Functions (20)

Principles of functional progrmming in scala
Principles of functional progrmming in scalaPrinciples of functional progrmming in scala
Principles of functional progrmming in scala
 
Scala oo (1)
Scala oo (1)Scala oo (1)
Scala oo (1)
 
Functional Objects & Function and Closures
Functional Objects  & Function and ClosuresFunctional Objects  & Function and Closures
Functional Objects & Function and Closures
 
Functional Objects & Function and Closures
Functional Objects  & Function and ClosuresFunctional Objects  & Function and Closures
Functional Objects & Function and Closures
 
Learn a language : LISP
Learn a language : LISPLearn a language : LISP
Learn a language : LISP
 
ScalaLanguage_ch_4_5.pptx
ScalaLanguage_ch_4_5.pptxScalaLanguage_ch_4_5.pptx
ScalaLanguage_ch_4_5.pptx
 
2D Plot Matlab
2D Plot Matlab2D Plot Matlab
2D Plot Matlab
 
FUNDAMETAL ALG.ppt
FUNDAMETAL ALG.pptFUNDAMETAL ALG.ppt
FUNDAMETAL ALG.ppt
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with Scala
 
C++ and Data Structure.ppt
C++ and Data Structure.pptC++ and Data Structure.ppt
C++ and Data Structure.ppt
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
An introduction to scala
An introduction to scalaAn introduction to scala
An introduction to scala
 
Ch6
Ch6Ch6
Ch6
 
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
 
Functional Objects & Function and Closures
Functional Objects  & Function and ClosuresFunctional Objects  & Function and Closures
Functional Objects & Function and Closures
 
Database structure Structures Link list and trees and Recurison complete
Database structure Structures Link list and trees and Recurison complete  Database structure Structures Link list and trees and Recurison complete
Database structure Structures Link list and trees and Recurison complete
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
 
Python programming- Part IV(Functions)
Python programming- Part IV(Functions)Python programming- Part IV(Functions)
Python programming- Part IV(Functions)
 
COMPANION TO MATRICES SESSION II.pptx
COMPANION TO MATRICES SESSION II.pptxCOMPANION TO MATRICES SESSION II.pptx
COMPANION TO MATRICES SESSION II.pptx
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
 

Recently uploaded

Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 

Recently uploaded (20)

Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 

Scilab - Piecewise Functions

  • 2. Piecewise Functions • A piecewise function is a function which is defined by multiple sub functions, each sub function applying to a certain interval of the main function's domain.
  • 3. Piecewise Functions • I’ll show you two ways to define and plot them in Scilab: 1. With iterations, one element at a time. 2. Without iterations, the vectorized way. (I’m using Scilab ver 5.4.0 for Win 7 - 64 bits)
  • 5. 1.- Using Iterations • First, you are going to define your piecewise function, where you’ll consider a scalar number as input. You’ll verify each interval and assign the appropriate value. • Second, you’ll call that function for all the necessary elements. • At the end, you’ll have two vectors, x and y, so that you can plot your initial function.
  • 6. 1.- Using Iterations – Define PW // Define your function assuming that you'll // get a scalar as input function y = pw1(x) // Filter and evaluate your first interval if x <= 1 then y = -x/3 + 4/3; // Filter and evaluate your second interval elseif (1 < x) & (x <= 3) then y = x^2/6 + x/3 + .5; // Filter and define your remaining intervals else y = .5*x + 1.5; end endfunction
  • 7. 1.- Using Iterations – Call Function // Clear your command window and clear memory clc, clear // Make sure that Scilab can see your function. // Load it into memory and add the full path if necessary exec('C:UsersUsuarioDocumentsScilab_docspw1.sci'); // This is your range of interest x = -2 : .2 : 5; // Call the function and evaluate element-by-element for ix = 1 : length(x) y(ix) = pw1(x(ix)); end // Now you have your vectors ready to plot
  • 8. 1.- Using Iterations – Call Function // Plot and add labels if needed plot(x, y, 'ro-') title('Piecewise in Scilab - Example 1'); xlabel('x'); ylabel('y');
  • 9. 2.- Using Vectorization • First, you are going to define your piecewise function, where you’ll consider a vector as input. You’ll find values for each interval and assign the appropriate values. • Second, you’ll call that function as you would for any other function that takes vectors. • At the end, you’ll have two arrays, x and y, so that you can plot the function under study.
  • 10. 2.- Using Vectorization x = -2 : .2 : 5; y = pw2(x); plot(x, y) Ideally, you should type something like this and get the same plot shown above interval of interest function to be defined call the plot, just as it’s done with any other function
  • 11. 2.- Using Vectorization The interesting part is how to define the piecewise function without going element-by- element in the domain, but going instead interval-by-interval. To accomplish this, we’ll use two ideas: • Specially selected indices. • Function find.
  • 12. 2.- Using Vectorization In Scilab, if we have vector x = [-2 -1 0 1 2 3 4 5 6 7 8 9], and do this: i2 = x(0 < x & x <= 3) we’ll take all the values in vector x that meet the condition 0 < x ≤ 3, that is, i2 = [1 2 3] If we do: i3 = x(3 < x & x <= 8) we’ll take all the values in vector x that meet the condition 3 < x ≤ 8, thus i3 = [4 5 6 7 8] First important concept: special indices
  • 13. 2.- Using Vectorization In Scilab, if we have vector x = [-2 -1 0 1 2 3 4 5 6 7 8 9], and do this: find(0 < x & x <= 3) we’ll find the indices (not values) in vector x that meet 0 < x ≤ 3, so we’ll get the vector [4 5 6]. If we do: find(3 < x & x <= 8) we’ll get the vector [7 8 9 10 11] Second important concept: function find
  • 14. 2.- Using Vectorization – Define PW Putting all together, defining the function under study: // Define your function considering a vector as an input function y = pw2(x) // Find the indices for the first interval ix1 = find(x <= 1); // Assign the appropriate values to the correct values y(ix1) = -x(ix1)/3 + 4/3; // Now the second interval, repeat the concept ix2 = find(1 < x & x <= 3); y(ix2) = x(ix2).^2/6 + x(ix2)/3 + .5;
  • 15. 2.- Using Vectorization – Define PW // Now, the last interval for this function ix3 = find(x > 3); y(ix3) = .5*x(ix3) + 1.5; endfunction
  • 16. 2.- Using Vectorization– Call Function // Clear your command window and clear memory clc, clear // Make sure that Scilab can see your function. // Load it into memory and add the full path if necessary exec('C:UsersUsuarioDocumentsScilab_docspw2.sci'); // This is your range of interest x = -2 : .2 : 5; // Call the function - don’t need iterations y = pw2(x); // Now you have your vectors ready to plot
  • 17. 2.- Using Vectorization– Call Function // Plot and add labels if needed plot(x, y, 'bo-') title('Piecewise in Scilab - Example 2'); xlabel('x'); ylabel('f(x)');
  • 18. For more examples and details, visit: matrixlab-examples.com/scilab-piecewise-function.html