Conditionally defined expression or function (2024)

Conditionally defined expression or function

collapse all in page

Syntax

pw = piecewise(cond1,val1,cond2,val2,...)

pw = piecewise(cond1,val1,cond2,val2,...,otherwiseVal)

Description

example

pw = piecewise(cond1,val1,cond2,val2,...) returns the piecewise expression or function pw whose value is val1 when condition cond1 is true, is val2 when cond2 is true, and so on. If no condition is true, the value of pw is NaN.

example

pw = piecewise(cond1,val1,cond2,val2,...,otherwiseVal) returns the piecewise expression or function pw that has the value otherwiseVal if no condition is true.

Examples

collapse all

Define and Evaluate Piecewise Expression

Open Live Script

Define the following piecewise expression by using piecewise.

y={-1x<01x>0

syms xy = piecewise(x < 0,-1,x > 0,1)
y =

{-1ifx<01if0<x

Evaluate y at -2, 0, and 2 by using subs to substitute for x. Because y is undefined at x = 0, the value is NaN.

subs(y,x,[-2 0 2])
ans =(-1NaN1)

Define Piecewise Function

Open Live Script

Define the following function symbolically.

y(x)={-1x<01x>0

syms y(x)y(x) = piecewise(x < 0,-1,x > 0,1)
y(x) =

{-1ifx<01if0<x

Because y(x) is a symbolic function, you can directly evaluate it for values of x. Evaluate y(x) at -2, 0, and 2. Because y(x) is undefined at x = 0, the value is NaN. For details, see Create Symbolic Functions.

y([-2 0 2])
ans =(-1NaN1)

Set Value When No Condition Is True

Set the value of a piecewise function when no condition is true (called otherwise value) by specifying an additional input argument. If an additional argument is not specified, the default otherwise value of the function is NaN.

Define the piecewise function

y={-2x<-20-2<x<01otherwise.

syms y(x)y(x) = piecewise(x < -2,-2,(-2 < x) & (x < 0),0,1)
y(x) =

{-2ifx<-20ifx(-2,0)1otherwise

Evaluate y(x) between -3 and 1 by generating values of x using linspace. At -2 and 0, y(x) evaluates to 1 because the other conditions are not true.

xvalues = linspace(-3,1,5)
xvalues = 1×5 -3 -2 -1 0 1
yvalues = y(xvalues)
yvalues =(-21011)

Plot Piecewise Expression

Open Live Script

Plot the following piecewise expression by using fplot.

y={-2x<-2x-2<x<22x>2.

syms xy = piecewise(x < -2,-2,-2 < x < 2,x,x > 2,2);fplot(y)

Conditionally defined expression or function (1)

Assumptions and Piecewise Expressions

Open Live Script

On creation, a piecewise expression applies existing assumptions. Apply assumptions set after creating the piecewise expression by using simplify on the expression.

Assume x > 0. Then define a piecewise expression with the same condition x > 0. piecewise automatically applies the assumption to simplify the condition.

syms xassume(x > 0)pw = piecewise(x < 0,-1,x > 0,1)
pw =1

Clear the assumption on x for further computations.

assume(x,'clear')

Create a piecewise expression pw with the condition x > 0. Then set the assumption that x > 0. Apply the assumption to pw by using simplify.

pw = piecewise(x < 0,-1,x > 0,1);assume(x > 0)pw = simplify(pw)
pw =1

Clear the assumption on x for further computations.

assume(x,'clear')

Differentiate, Integrate, and Find Limits of Piecewise Expression

Open Live Script

Differentiate, integrate, and find limits of a piecewise expression by using diff, int, and limit respectively.

Differentiate the following piecewise expression by using diff.

y={1/xx<-1sin(x)/xx-1

syms xy = piecewise(x < -1,1/x,x >= -1,sin(x)/x);diffy = diff(y,x)
diffy =

{-1x2ifx<-1cos(x)x-sin(x)x2if-1<x

Integrate y by using int.

inty = int(y,x)
inty =

{log(x)ifx<-1sinint(x)if-1x

Find the limits of y at 0 by using limit.

limit(y,x,0)
ans =1

Find the right- and left-sided limits of y at -1. For details, see limit.

limit(y,x,-1,'right')
ans =sin(1)
limit(y,x,-1,'left')
ans =-1

Elementary Operations on Piecewise Expressions

Open Live Script

Add, subtract, divide, and multiply two piecewise expressions. The resulting piecewise expression is only defined where the initial piecewise expressions are defined.

syms xpw1 = piecewise(x < -1,-1,x >= -1,1);pw2 = piecewise(x < 0,-2,x >= 0,2);add = pw1+pw2
add =

{-3ifx<-1-1ifx[-1,0)3if0x

sub = pw1-pw2
sub =

{1ifx<-13ifx[-1,0)-1if0x

mul = pw1*pw2
mul =

{2ifx<-1-2ifx[-1,0)2if0x

div = pw1/pw2
div =

{12ifx<-1-12ifx[-1,0)12if0x

Modify or Extend Piecewise Expression

Open Live Script

Modify a piecewise expression by replacing part of the expression using subs. Extend a piecewise expression by specifying the expression as the otherwise value of a new piecewise expression. This action combines the two piecewise expressions. piecewise does not check for overlapping or conflicting conditions. Instead, like an if-else ladder, piecewise returns the value for the first true condition.

Change the condition x < 2 in a piecewise expression to x < 0 by using subs.

syms xpw = piecewise(x < 2,-1,x > 0,1);pw = subs(pw,x < 2,x < 0)
pw =

{-1ifx<01if0<x

Add the condition x > 5 with the value 1/x to pw by creating a new piecewise expression with pw as the otherwise value.

pw = piecewise(x > 5,1/x,pw)
pw =

{1xif5<x-1ifx<01if0<x

Input Arguments

collapse all

condCondition
symbolic condition | symbolic variable

Condition, specified as a symbolic condition or variable. A symbolic variable represents an unknown condition.

Example: x > 2

valValue when condition is satisfied
number | vector | matrix | multidimensional array | symbolic number | symbolic variable | symbolic vector | symbolic matrix | symbolic multidimensional array | symbolic function | symbolic expression

Value when condition is satisfied, specified as a number, vector, matrix, or multidimensional array, or as a symbolic number, variable, vector, matrix, multidimensional array, function, or expression.

otherwiseValValue if no conditions are true
number | vector | matrix | multidimensional array | symbolic number | symbolic variable | symbolic vector | symbolic matrix | symbolic multidimensional array | symbolic function | symbolic expression

Value if no conditions are true, specified as a number, vector, matrix, or multidimensional array, or as a symbolic number, variable, vector, matrix, multidimensional array, function, or expression. If otherwiseVal is not specified, its value is NaN.

Output Arguments

collapse all

pw — Piecewise expression or function
symbolic expression | symbolic function

Piecewise expression or function, returned as a symbolic expression or function. The value of pw is the value val of the first condition cond that is true. To find the value of pw, use subs to substitute for variables in pw.

Tips

  • piecewise does not check for overlapping or conflicting conditions. A piecewise expression returns the value of the first true condition and disregards any following true expressions. Thus, piecewise mimics an if-else ladder.

Version History

Introduced in R2016b

See Also

and | assume | assumeAlso | assumptions | if | in | isAlways | not | or

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Conditionally defined expression or function (2)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

Contact your local office

Conditionally defined expression or function (2024)

FAQs

Conditionally defined expression or function? ›

Conditional expressions impose constraints on the evaluation order of their inputs. In essence, they are evaluated left to right, with short-circuiting, and only evaluate the output value that was chosen. In contrast, all inputs to regular functions are evaluated before calling the function.

How to write a conditional function in MATLAB? ›

Description. pw = piecewise( cond 1, val 1, cond 2, val 2,...) returns the piecewise expression or function pw whose value is val1 when condition cond1 is true, is val2 when cond2 is true, and so on. If no condition is true, the value of pw is NaN .

What is a condition in a piecewise function? ›

Functions that use conditions are called piecewise functions, because each condition defines a separate piece of the function. Why are piecewise functions useful? Think about the player in your game: you'd like the player to move one way if you hit the "up" key, and another way if you hit the "down" key.

What is an example of a piecewise function? ›

Another example of a piecewise function is the absolute value function. The absolute value function can be written as f ( x ) = | x | . This function has three different pieces, x for all values less than 1, x 2 for all values between 1 and 3 (including 1 and 3) and finally, x + 3 for all values greater than three.

What is the Syms function in MATLAB? ›

The syms function creates a variable dynamically. For example, the command syms x creates the symbolic variable x and automatically assigns it to a MATLAB variable with the same name. You can then use the variable x in the MATLAB workspace for symbolic workflow, such as finding the roots of a polynomial.

How do you create a conditional function? ›

You can use the AND, OR, NOT, and IF functions to create conditional formulas. For example, the IF function uses the following arguments. logical_test: The condition that you want to check. value_if_true: The value to return if the condition is True.

What is conditional mean in MATLAB? ›

A dynamic conditional mean model specifies the expected value of a response process yt as a function of historical information.

What is a function condition? ›

Given x∈A, its associated element in B is called its image under f. In other words, a function is a relation from A to B with the condition that for every element in the domain, there exists a unique image in the codomain (this is really two conditions: existence of an image and uniqueness of an image).

What is the difference between a function and a piecewise function? ›

A piecewise function is just a function which is made up of multiple functions and is defined by exactly one of them in every part of its domain. f(x) is just the notation for a function. It can also be used for a piecewise function.

Which piecewise function defines a function? ›

A piecewise function is a function that is defined on a sequence of intervals. A common example is the absolute value, Additional piecewise functions include the Heaviside step function, rectangle function, and triangle function.

How do you know if it's a piecewise function? ›

A piecewise function is a function that is defined in separate "pieces" or intervals. For each region or interval, the function may have a different equation or rule that describes it. We can graph a piecewise function by graphing each individual piece.

What is a piecewise function for dummies? ›

A piecewise function consists of two or more function rules (function equations) pieced together (listed separately for different x values) to form one bigger function. A change in the function equation occurs for different values in the domain.

What is a piecewise-defined function easy? ›

A piecewise defined function (which is also known as a piecewise function) is a function that has different definitions over different intervals of inputs. An example of a piecewise function is f(x)=⎧⎪⎨⎪⎩2x−3 if x<−2−|x|+5 if −2≤x<3x2−2 if x≥3 f ( x ) = { 2 x − 3 if x < − 2 − | x | + 5 if − 2 ≤ x < 3 x 2 − 2 if x ≥ 3 .

What does Dirac mean in MATLAB? ›

dirac returns floating-point results for numeric arguments that are not symbolic objects. dirac acts element-wise on nonscalar inputs. The input arguments x and n must be vectors or matrices of the same size, or else one of them must be a scalar.

What is heaviside in MATLAB? ›

H = heaviside( x ) evaluates the Heaviside step function (also known as the unit step function) at x . The Heaviside function is a discontinuous function that returns 0 for x < 0 , 1/2 for x = 0 , and 1 for x > 0 .

What is pi function in MATLAB? ›

p = pi returns the floating-point number nearest to the value of π in IEEE® double-precision.

Is there an if () function in MATLAB? ›

if (MATLAB Functions) MATLAB evaluates the expression and, if the evaluation yields a logical true or nonzero result, executes one or more MATLAB commands denoted here as statements . When nesting if s, each if must be paired with a matching end .

How to use conditional statements in MATLAB? ›

Conditional Statements
  1. % Generate a random number a = randi(100, 1); % If it is even, divide by 2 if rem(a, 2) == 0 disp('a is even') b = a/2; end. ...
  2. a = randi(100, 1); if a < 30 disp('small') elseif a < 80 disp('medium') else disp('large') end.

How do you write two if conditions in MATLAB? ›

Accepted Answer

You can combine multiple conditions using & (and) and | (or) (&& and || are also things). Using this method I think it's worth combining the first two levels of your if statement.

How do you write an absolute function in MATLAB? ›

Y = abs( X ) returns the absolute value of each element in input X . If X is complex, abs(X) returns the complex magnitude.

References

Top Articles
Latest Posts
Article information

Author: Gregorio Kreiger

Last Updated:

Views: 5684

Rating: 4.7 / 5 (77 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Gregorio Kreiger

Birthday: 1994-12-18

Address: 89212 Tracey Ramp, Sunside, MT 08453-0951

Phone: +9014805370218

Job: Customer Designer

Hobby: Mountain biking, Orienteering, Hiking, Sewing, Backpacking, Mushroom hunting, Backpacking

Introduction: My name is Gregorio Kreiger, I am a tender, brainy, enthusiastic, combative, agreeable, gentle, gentle person who loves writing and wants to share my knowledge and understanding with you.