Piecewise Functions 
In Matlab
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 
• We’ll show one way to define and plot 
them in Matlab without using loops. 
• We’ll use a vectorized way: no scalar 
values or iterations will be used.
Piecewise Functions 
Let’s assume a function with 4 intervals 
ì 
ï ï í 
2 0 
6 2 0 3 
( ) 2 
ï ï 
î 
£ 
if x 
x if x 
+ < £ 
x x if x 
- + + < £ 
6 11 3 8 
- < 
= 
if x 
f x 
5 8
Piecewise Functions 
This is the plot of that 4-interval function 
x 
y = f(x)
Piecewise Functions 
Ideally, we should type something like this 
and get the plot shown above 
x = -2 : .01 : 9; 
y = piecewise(x); 
plot(x, y) 
interval of interest 
function to be defined 
call the plot, just as it’s done 
with any other function
Piecewise Functions 
The interesting part is how to define the 
piecewise function without going number-by-number 
in the domain, but going instead 
interval-by-interval. 
To accomplish this, we’ll use two ideas: 
• Specially selected indices. 
• Function find.
Piecewise Functions 
First important concept: special indices 
In Matlab, 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]
Piecewise Functions 
Second important concept: function find 
In Matlab, 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]
Piecewise Functions 
Putting all together, defining the function: 
function y = piecewise(x) 
y(find(x <= -0)) = 2; 
Name: piecewice 
Input: vector x 
Output: vector y 
First interval 
Find the indices of values of x 
that meet criterion x ≤ 0 
Assign the corresponding value, 
according to your f(x)
Piecewise Functions 
Putting all together, defining the function 
Second inteval 
Take the values of x that meet 
criterion of 0 < x ≤ 3 
Find the corresponding indices 
Assign the corresponding value, 
according to your f(x) 
x2 = x(0<x & x<=3); 
y(find(0<x & x<=3)) = 6*x2 + 2;
Piecewise Functions 
Putting all together, defining the function 
Third interval 
Take the values of x that meet 
criterion of 3 < x ≤ 8 
Find the corresponding indices 
Assign the corresponding value, 
according to your f(x) 
x3 = x(3<x & x<=8); 
y(find(3<x & x<=8)) = -x3.^2 + 
6*x3 + 11;
Piecewise Functions 
Putting all together, defining the function 
Fourth inteval 
Take the corresponding indices 
for 8 < x 
We don’t need the values of x 
now, because they’re not 
needed for the assignation in 
this interval 
y(find(8 < x)) = -5;
Piecewise Functions 
Putting all together. This is the final code… 
function y = piecewise(x) 
y(find(x <= -0)) = 2; 
x2 = x(0 < x & x <= 3); 
y(find(0 < x & x <= 3)) = 6*x2 + 2; 
x3 = x(3 < x & x <= 8); 
y(find(3 < x & x <= 8)) = -x3.^2 + 6*x3 + 11; 
y(find(8 < x)) = -5;
Piecewise Functions 
Now, if we type this code… 
clc, clear all, close all 
x = -2 : .01 : 9; 
y = piecewise(x); 
plot(x, y) 
axis([-2 9 -10 25]) 
grid on 
We get this plot…
Piecewise Functions 
For more examples and details, visit: 
matrixlab-examples.com/piecewise-function.html

More Related Content

PPTX
Bisection method
PPTX
Graphing rational functions
PPTX
Application of partial derivatives with two variables
PPTX
Continuous Random Variables
PPTX
Different types of functions
PPTX
Taylor series
PPT
Graphs: Hamiltonian Path and Circuit
PPTX
Limits and continuity powerpoint
Bisection method
Graphing rational functions
Application of partial derivatives with two variables
Continuous Random Variables
Different types of functions
Taylor series
Graphs: Hamiltonian Path and Circuit
Limits and continuity powerpoint

What's hot (20)

PPT
Graphs - Discrete Math
PPTX
Differential calculus maxima minima
PPTX
Domain-and-Range-of-a-Function
PPTX
Vector Spaces,subspaces,Span,Basis
PPTX
Spline interpolation numerical methods presentation
PPTX
Network flows
PPTX
Network flows
PPTX
Inverse function
PPTX
01 Knapsack using Dynamic Programming
PDF
Topological Sort
PPT
1.1 binary tree
PDF
Math lecture 10 (Introduction to Integration)
PPT
minimum spanning tree
PPT
11. transaction sql
PPTX
IMPROPER INTEGRALS AND APPLICATION OF INTEGRATION
PPT
5.2 first and second derivative test
PPTX
False Point Method / Regula falsi method
PPTX
Inner product spaces
PPTX
Presentation on Solution to non linear equations
PDF
Chapter 2: Relations
Graphs - Discrete Math
Differential calculus maxima minima
Domain-and-Range-of-a-Function
Vector Spaces,subspaces,Span,Basis
Spline interpolation numerical methods presentation
Network flows
Network flows
Inverse function
01 Knapsack using Dynamic Programming
Topological Sort
1.1 binary tree
Math lecture 10 (Introduction to Integration)
minimum spanning tree
11. transaction sql
IMPROPER INTEGRALS AND APPLICATION OF INTEGRATION
5.2 first and second derivative test
False Point Method / Regula falsi method
Inner product spaces
Presentation on Solution to non linear equations
Chapter 2: Relations
Ad

Similar to Piecewise Functions in Matlab (20)

PPT
Scilab - Piecewise Functions
PDF
PDF
Introduction to Functions
PPTX
COMPANION TO MATRICES SESSION II.pptx
PDF
Introduction to functions
PDF
Note introductions of functions
PPTX
Chapter 2 =Introduction to Mathematica.pptx
PPTX
Piecewise functions
PPTX
Relation and function pdf
PPT
Introduction to matlab
PDF
Lecture 3.1 to 3.2 bt
PPT
Introduction to matlab
PPT
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
PPT
Relations and functions
PDF
PPTX
20200830230859_PPT4-Lines, Parabolas and Systems.pptx
PDF
Matlab-free course by Mohd Esa
PPT
An introduction to scala
PDF
Matlab solved problems
PPTX
MATLAB Workshop for project and research
Scilab - Piecewise Functions
Introduction to Functions
COMPANION TO MATRICES SESSION II.pptx
Introduction to functions
Note introductions of functions
Chapter 2 =Introduction to Mathematica.pptx
Piecewise functions
Relation and function pdf
Introduction to matlab
Lecture 3.1 to 3.2 bt
Introduction to matlab
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
Relations and functions
20200830230859_PPT4-Lines, Parabolas and Systems.pptx
Matlab-free course by Mohd Esa
An introduction to scala
Matlab solved problems
MATLAB Workshop for project and research
Ad

Recently uploaded (20)

PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
PDF
Uderstanding digital marketing and marketing stratergie for engaging the digi...
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PPTX
Computer Architecture Input Output Memory.pptx
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PDF
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Complications of Minimal Access-Surgery.pdf
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
HVAC Specification 2024 according to central public works department
PDF
Hazard Identification & Risk Assessment .pdf
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PDF
My India Quiz Book_20210205121199924.pdf
PDF
Trump Administration's workforce development strategy
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
What if we spent less time fighting change, and more time building what’s rig...
PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
Chinmaya Tiranga quiz Grand Finale.pdf
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
Uderstanding digital marketing and marketing stratergie for engaging the digi...
Practical Manual AGRO-233 Principles and Practices of Natural Farming
Computer Architecture Input Output Memory.pptx
Paper A Mock Exam 9_ Attempt review.pdf.
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Complications of Minimal Access-Surgery.pdf
LDMMIA Reiki Yoga Finals Review Spring Summer
HVAC Specification 2024 according to central public works department
Hazard Identification & Risk Assessment .pdf
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
My India Quiz Book_20210205121199924.pdf
Trump Administration's workforce development strategy
Weekly quiz Compilation Jan -July 25.pdf
What if we spent less time fighting change, and more time building what’s rig...
B.Sc. DS Unit 2 Software Engineering.pptx
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...

Piecewise Functions in Matlab

  • 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 • We’ll show one way to define and plot them in Matlab without using loops. • We’ll use a vectorized way: no scalar values or iterations will be used.
  • 4. Piecewise Functions Let’s assume a function with 4 intervals ì ï ï í 2 0 6 2 0 3 ( ) 2 ï ï î £ if x x if x + < £ x x if x - + + < £ 6 11 3 8 - < = if x f x 5 8
  • 5. Piecewise Functions This is the plot of that 4-interval function x y = f(x)
  • 6. Piecewise Functions Ideally, we should type something like this and get the plot shown above x = -2 : .01 : 9; y = piecewise(x); plot(x, y) interval of interest function to be defined call the plot, just as it’s done with any other function
  • 7. Piecewise Functions The interesting part is how to define the piecewise function without going number-by-number in the domain, but going instead interval-by-interval. To accomplish this, we’ll use two ideas: • Specially selected indices. • Function find.
  • 8. Piecewise Functions First important concept: special indices In Matlab, 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]
  • 9. Piecewise Functions Second important concept: function find In Matlab, 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]
  • 10. Piecewise Functions Putting all together, defining the function: function y = piecewise(x) y(find(x <= -0)) = 2; Name: piecewice Input: vector x Output: vector y First interval Find the indices of values of x that meet criterion x ≤ 0 Assign the corresponding value, according to your f(x)
  • 11. Piecewise Functions Putting all together, defining the function Second inteval Take the values of x that meet criterion of 0 < x ≤ 3 Find the corresponding indices Assign the corresponding value, according to your f(x) x2 = x(0<x & x<=3); y(find(0<x & x<=3)) = 6*x2 + 2;
  • 12. Piecewise Functions Putting all together, defining the function Third interval Take the values of x that meet criterion of 3 < x ≤ 8 Find the corresponding indices Assign the corresponding value, according to your f(x) x3 = x(3<x & x<=8); y(find(3<x & x<=8)) = -x3.^2 + 6*x3 + 11;
  • 13. Piecewise Functions Putting all together, defining the function Fourth inteval Take the corresponding indices for 8 < x We don’t need the values of x now, because they’re not needed for the assignation in this interval y(find(8 < x)) = -5;
  • 14. Piecewise Functions Putting all together. This is the final code… function y = piecewise(x) y(find(x <= -0)) = 2; x2 = x(0 < x & x <= 3); y(find(0 < x & x <= 3)) = 6*x2 + 2; x3 = x(3 < x & x <= 8); y(find(3 < x & x <= 8)) = -x3.^2 + 6*x3 + 11; y(find(8 < x)) = -5;
  • 15. Piecewise Functions Now, if we type this code… clc, clear all, close all x = -2 : .01 : 9; y = piecewise(x); plot(x, y) axis([-2 9 -10 25]) grid on We get this plot…
  • 16. Piecewise Functions For more examples and details, visit: matrixlab-examples.com/piecewise-function.html