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-Befehl

Sie haben auf einen Link geklickt, der diesem MATLAB-Befehl entspricht:

 

Führen Sie den Befehl durch Eingabe in das MATLAB-Befehlsfenster aus. Webbrowser unterstützen keine MATLAB-Befehle.

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

What is an example of a conditional statement? ›

Example: We have a conditional statement If it is raining, we will not play. Let, A: It is raining and B: we will not play. Then; If A is true, that is, it is raining and B is false, that is, we played, then the statement A implies B is false.

What is the conditional expression? ›

A conditional expression has the general form. where expr1 is an expression that yields a true or false result. If the result is true, the value of the conditional expression is set to expr2. If the result is false, the value of the conditional expression is set to expr3.

How do you write conditional expressions? ›

Writing Conditional Expressions
  1. Surround the expression in parentheses.
  2. Surround strings or exact values (such as digits pressed) in quotation marks.
  3. Enter variables without quotation marks.

What does conditional expression mean in mathematica? ›

The ConditionalExpression has a value only when the condition evaluates to True . If the condition evaluates to False then the expression is undefined.

What are the 4 examples of conditional? ›

English Conditionals: Quick Summary

Zero conditional: If you jump in a pool, you get wet. First conditional: If you run a mile in the scorching heat, you will sweat. Second conditional: If I were you, I would apply for that job. Third conditional: If I had known you were coming, I would have made more food.

What are 2 examples of conditional sentences? ›

Examples. If I were taller, I would buy this dress. If I were 20, I would travel the world. If I were you, I would give up smoking.

How can you identify a conditional expression? ›

The major clue to identifying conditional statements is the if (and usually a then). Consider the following: If that is an owl, then it is the weirdest bird I have seen. If it is after sundown, then I should get home and feed my cats.

What are the 3 ways of writing conditional statements? ›

The converse statement is, "You will pass the exam if you study well" (if q, then p). The inverse statement is, "If you do not study well then you will not pass the exam" (if not p, then not q). The contrapositive statement is, "If you did not pass the exam, then you did not study well" (if not q, then not p).

What are the 5 conditional statements? ›

5 Types of Conditional Sentences
Conditional sentence typeWhen to useIf-clause
Type ZeroDescribing known factsSimple present
Type 1A possible situation and the resultSimple present
Type 2A hypothetical condition and its possible resultSimple past
Type 3An impossible past situation and its result in the pastPast perfect
1 more row

How do you use conditional functions? ›

IF
  1. =if(condition,value_if_true,value_if_false)
  2. =if(A2>3,32,”Number too small”)
  3. =if (A2>20,”Number too large”, if (A2<10,”Number too small”, “Just right!”))
  4. =countif(range,criteria)
  5. =countif (F1:F230, “=red”)
  6. =sumif(range,criteria,sum_range)
  7. =sumif (D1:D230,”=television”, H1:H230)
  8. =AND(condition, condition)

What is a conditional expression also called? ›

The conditional expression is also known as the ternary operator. The ternary operator is a shorthand for an if-else statement and is represented by the symbol "?". It takes three operands: a condition, a value if the condition is true, and a value if the condition is false.

How is a conditional expression evaluated? ›

Expression is evaluated according to associativity of the operator. The associativity of the operator is a property that determines how operators of the same precedence are grouped in the absence of parenthesis.

What are the 3 conditional statements? ›

The conditional statements are also known as decision-making statements. They are of the type if statement , if-else , if else-if ladder , switch , etc. These statements determine the flow of the program execution.

What is an example of one conditional statement? ›

Form: If + simple present, simple present Example 1: If it rains, I take an umbrella with me to work. Example 2: If I wake up early, I always read in bed. The first conditional uses the present tense in the if clause and the future tense in the result clause.

What are examples of 3 conditionals? ›

Examples
  • If I'd known you were in hospital, I'd have visited you.
  • If I had known you were in hospital, I would have visited you.
  • I'd have bought you a present if I'd known it was your birthday.
  • I would have bought you a present if I had known it was your birthday.
  • If you'd given me your e-mail, I'd have written to you.

References

Top Articles
Latest Posts
Article information

Author: Pres. Carey Rath

Last Updated:

Views: 5692

Rating: 4 / 5 (41 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Pres. Carey Rath

Birthday: 1997-03-06

Address: 14955 Ledner Trail, East Rodrickfort, NE 85127-8369

Phone: +18682428114917

Job: National Technology Representative

Hobby: Sand art, Drama, Web surfing, Cycling, Brazilian jiu-jitsu, Leather crafting, Creative writing

Introduction: My name is Pres. Carey Rath, I am a faithful, funny, vast, joyous, lively, brave, glamorous person who loves writing and wants to share my knowledge and understanding with you.