In math, a piecewisefunction (or piecewise-defined function)is a role whose an interpretation changes depending upon the value of theindependent variable. 

Afunction f ofa variable x (noted f(x))is a partnership whose an interpretation is given differently on differentsubsets ofits domain.Piecewise is a termalso offered to describe any kind of property that apiecewise function that is true because that each piece yet may not be true forthewhole domain that the function. The role doesn’t should becontinuous, itcan be defined arbitrarily.

You are watching: How to plot piecewise functions in matlab

We’re going come experimentin Matlab with this form offunctions. We’re walking to develop three methods to define and also graph them.The firstmethod requires if-statements come classify element-by-element, in avector. Thesecond technique uses switch-case statements, and the third technique usesindices todefine various sections of the domain.Let’s say that this isour piecewise-defined function  
and we’d choose to graph iton the domain of -8 ≤ x ≤ 4 

Method 1. If-else statements

Notice that this is notthe best way to execute it inMatlab, however I point out the idea because this is the fundamentalconcept, and in some programming language it can not be excellent in adfferent way. It"s for study, not for real implementation...

So, let"s define ourfunction with if-else statements for the moment. Wejust usage different problems for the different ranges, and assignappropriatevalues.

function y =piecewise1(x)if x y= 3;elseif -4 y= -4*x - 13;elseif -3 y= x^2 + 6*x + 8;else y = 8;end Now, we have the right to use scalarsor arrays to call this function, ina classical way:

%Define elementsx = <-5-4 -3 0 3>%Submit element-by-element come the functionfor ix = 1: length(x) y(ix)= piecewise1(x(ix))end%Plot discrete valuesplot(x,y, "ro")%Define x and y arrays to displayaxis(<-84 -2 10>) The result is:

Let’s to speak that us needmore values. We can contact our functionthis wayx = -8 :.01 : 4;for ix = 1: length(x) y(ix)= piecewise1(x(ix));endplot(x,y)axis(<-84 -2 9>) The brand-new graph (whichobviously gives an ext information) is:


Method 2. Switch-case statements

Our 2nd methodclassifies the elements using switch-case statements.We different the various ranges in various cases. If that conditionis true,then the switch-expression will enhance the case-expression, and theappropriatestatement(s) will be executed.function y = piecewise2(x)switch x % Ascalar switch_expr matchesa case_expr if %switch_expr == case_expr % Thefirst case must coverthe x = 0 situation case x * (-3 y = x^2 + 6*x + 8; situation x * (x y = 3; case x * (-4 y = -4*x - 13; otherwise y = 8;endOne necessary note is that once x = 0 the result of thecase-expression is likewise 0, and also the very first case is executed. That’s whyweneed toplace our x = 0 case an initial in the structure, otherwise we gain wrongthat point.We have the right to test our secondpiecewise function definition, likethis: x= -8 :.01 : 4;for ix = 1: length(x) y(ix)= piecewise2(x(ix));endplot(x,y)axis(<-84 -2 9>) and gain the previousshown graph, too. 

Method 3. Vectorized way

The above routines assumethat we’re entering scalars asinput parameters. Now, we’ll assume the we’re submitting wholevectors, andwe’ll manage indices for that.This video clip will present youhow to carry out it without using loops. After ~ the video, one more example isgiven with full code.

See more: Gwen Stefani Rare Lyrics - Lyrics For Rare By Gwen Stefani

 The line x2 = x(-4assigns come x2 the valuesof vector x that satisfy the criteriaof -4 ≤ -3. The line  y(-4) = -4*x2 - 13;workswith the worths in x2, calculates the appropriate duty y(x) andplaces the values in the correct indices of vector y. We can integrate those twoideas to work-related out all the appropriatevalues for the entirety function. The following code isjust one way to take full vectors andpack them right into piecewise functions:function y = piecewise3(x)%first rangey(x %second rangex2 =x(-4 y(-4 )= -4*x2 - 13; %third rangex3 =x(-3 y(-3 )= x3.^2 + 6*x3 + 8; %fourth rangey(0 Now, we can avoidfor-loops before graphing this kind offunctions...We enter the whole array of the live independence variable asparameterx = -8 :.01 : 4;y1 =piecewise3(x);plot(x,y1)axis(<-84 -2 9>)this to produce what weknow andexpect...

 From "Piecewise Functions"to home From "PiecewiseFunctions" to circulation ControlTopPeriodicFunctions