## Introduction to Piecewise Function in Matlab

A piecewise function is a function, which is defined by various multiple functions. In this other multiple functions are used to apply on specific intervals of the main function. Piecewise function is also used to describe the property of any equation or function. It represents various conditions in functions or equations. In this topic, we are going to learn about Piecewise Function in Matlab.

It can be implemented in two ways one is by using loops ( if-else statement and switch statement ) and the other one is without using loops. In the loops method, victories way is used. As we see there are two ways, with loops and without using loops.

In the first method again there are two ways

1. By using the if-else method

2. By using the switch statement

In second method function represent in vectorize way

3. The vectorized method

### By using If-Else statements

This is one of the basic terminologies to implement piecewise functions but, this is not a good practice to implement piecewise function.

**Syntax :**

`If condition1`

Statement 1 ;

else

statement 2;

end

plot ( input variable , output variable )

function output variable = piecewise ( input variable )

**Example #1**

Now let us consider one example

`f ( x ) = - 2 for x < 0`

2 for x > 0

To implement the above function in Matlab first we need to create one function with keyword ‘ piecewise ’

4.5 (4,812 ratings)

View Course

`> > function f x = piecewise ( x )`

In the above statement ‘ f x ’ is the name of the output variable, ‘ piecewise ’ is keyword used for the above function and ‘ x ’ is the input variable.

After declaring function now we need to define the conditions of ranges of input variable ‘ x’.

`>> If x < = 0`

>> fx = -2

>> else

>> fx = 2

In above statements if-else statement is used to define the range .it shows that if the value of x is less than or equal to ‘ 0 ’ then out will be ‘ – 2 ’ and if the value of ‘ x ’ is more than ‘ 0 ’ then the output will be ‘ 2’.

**Matlab program:**

`If x < = 0`

f x = -2 ;

else

f x = 2 ;

end

plot ( x , f x )

function f x = piecewise ( x )

**output :**

#### Switch-case statement

The second method in loops is driven by switch-case statements. In this method we represent different conditions in different methods, we can specify multiple cases in one switch loop.

**Example #2**

Let us assume the above example,

`f x = - 2 for x <= 0`

2 for x > 0

In this example there are two conditions in function f x, one is less than equal to ‘ 0 ’ and the other one is greater than ‘ 0’.

To implement the above example by using the switch – case statement first, we need to declare the function statement ( piecewise function).

`>> function f x = piecewise (x )`

The above statements show f x is piecewise function concerning input variable ‘ x’, after declaring the function we will start with the switch statement.

`>> switch (x)`

The above statement is the keyword for the switch case for changing values of variable ‘ x’. Now inside the switch, there will be different cases, our requirement is only cases so we will write 2 cases.

`Case 1: x < = 0`

F x = - 2 ;

Case 2 : x > 0

F x = 2 ;

The above statements represent ranges of x and respective expected function values.

#### Matlab program

`function F x = piecewise (x )`

switch ( x )

Case 1 : x < = 0

F x=-2 ;

Case 2 : x > 0

F x = 2 ;

end

Plot ( F x , x )

**Output :**

### Vectorized Method

This method is the second approach of piecewise functions without using loops. In this method, the input is the whole vector of sequences(conditions) as well as we can combine two conditions by using ‘ & ’ operator. This is the most popular method in piecewise functions.

Let us assume the same example ;

`f x=-2 … x<=0`

2 … x > 0

Now we will illustrate the above example by using the vectorize approach, First, we need to declare piecewise function like the above examples.

`function f x = piecewise (x)`

After declaring the piecewise function we will define ranges of input variable ‘ x ’. In the above example as we know there are two conditions, therefore, we need to define two ranges.

`fx (x<=0)=-2 ;`

and

fx (x>0)=2;

Now, as the ranges are known we need to declare the total range of input variable ‘ x’.

x = – 5 : 1 : 5

this shows that x will take the values from – 5 to + 5.

#### Matlab program

`function f x = piecewise ( x )`

fx(x<= 0) = - 2 ;

fx(x>0) = 2 ;

x = - 5 : 1 : 5

f x = piecewise ( x )

plot (f x , x )

**Output:**

### Conclusion – Piecewise Function in Matlab

Piecewise functions are mainly used to represent functions that have various input ranges with different conditions. As we see above there are three approaches to represent piecewise functions. But, the if-else (loop) approach not used for real-time implementations. And the vectorized approach used in many applications.

### Recommended Articles

This is a guide to Piecewise Function in Matlab. Here we discuss the Methods of using Piecewise Function in Matlab with various statements and examples. You may also look at the following article to learn more –