How to make a piecewise function? (2024)

17 views (last 30 days)

Show older comments

john warts on 14 Mar 2020

  • Link

    Direct link to this question

    https://au.mathworks.com/matlabcentral/answers/510838-how-to-make-a-piecewise-function

  • Link

    Direct link to this question

    https://au.mathworks.com/matlabcentral/answers/510838-how-to-make-a-piecewise-function

Answered: john warts on 15 Mar 2020

Accepted Answer: Ameer Hamza

Open in MATLAB Online

I just want to plot the 3D function that makes a rollercoaster track. I have done it succesfully in 2D in desmos using arcs of circles through both implicit and parametric functions which I have attached.

I can not create a piecewise function that represents what I have in either desmos images.

What should I be putting into MATLAB to get the desmos equivalent? Is my piecewise failing because of the domain restrictions?

I have tried to make a piecewise function for only the x axis. The plan was to make 3 piecewise functions for x y and z, then to plot the 3 piecewise functions. Using the following command:

pwx = piecewise(0<=t<=pi/2,f1x,pi<=t<=3*pi/2,f2x,3*pi/2<=2*pi,f3x,0<=t<=pi,f4x,pi<=t<=3*pi/2,f5x)

where

f1x = @(t) 2*cos(t);

f2x = @(t) cos(t);

f3x = @(t) 2*cos(t)+3;

f4x = @(t) cos(t)+4;

f5x = @(t) 2*cos(t)+5;

and

t = linspace(0,3*pi/2)

I get the error message:

Undefined function 'piecewise' for input arguments of type 'function_handle'.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Ameer Hamza on 14 Mar 2020

  • Link

    Direct link to this answer

    https://au.mathworks.com/matlabcentral/answers/510838-how-to-make-a-piecewise-function#answer_420108

  • Link

    Direct link to this answer

    https://au.mathworks.com/matlabcentral/answers/510838-how-to-make-a-piecewise-function#answer_420108

Open in MATLAB Online

Piecewise takes a symbolic variable as input. Try

syms t

f1x = 2*cos(t);

f2x = cos(t);

f3x = 2*cos(t)+3;

f4x = cos(t)+4;

f5x = 2*cos(t)+5;

pwx = piecewise(0<=t & t<pi/2,f1x, pi<=t & t<3*pi/2,f2x, 3*pi/2<=t & t<2*pi,f3x, 0<=t & t<pi,f4x, pi<=t & t<3*pi/2, f5x);

fplot(pwx)

For the 2D parametric curve question you posted earlier, you can follow the same method as above and make a piecewise function for y values. However, this method has the disadvantage that all the lines will have the same style. You can try the following code for an alternative way to draw similar plots.

fig = figure();

ax = gca();

hold(ax);

grid on

daspect([1 1 1]);

a = 2;

b = 1;

fplot(@(t) a*cos(t), @(t) a*sin(t), [0, pi/2], 'Color', 'r', 'LineWidth', 1.5);

fplot(@(t) b*cos(t)+3, @(t) b*sin(t), [pi, 3*pi/2], 'Color', 'b', 'LineWidth', 1.5);

fplot(@(t) a*cos(t)+3, @(t) a*sin(t)+1, [3*pi/2, 2*pi], 'Color', 'g', 'LineWidth', 1.5);

fplot(@(t) b*cos(t)+4, @(t) b*sin(t)+1, [0, pi], 'Color', 'k', 'LineStyle', '--', 'LineWidth', 1.5);

fplot(@(t) a*cos(t)+5, @(t) a*sin(t)+1, [pi, 3*pi/2], 'Color', 'k', 'LineWidth', 1.5)

fplot(@(t) t, @(t) -1*ones(size(t)), [5 6], 'Color', 'm', 'LineWidth', 1.5)

How to make a piecewise function? (3)

3 Comments

Show 1 older commentHide 1 older comment

john warts on 14 Mar 2020

Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/510838-how-to-make-a-piecewise-function#comment_809904

  • Link

    Direct link to this comment

    https://au.mathworks.com/matlabcentral/answers/510838-how-to-make-a-piecewise-function#comment_809904

Open in MATLAB Online

  • weird image.bmp

Thank you very much for helping me

I attempted to recreate what you have done in the y axis as well with a function:

pwy = piecewise(0<=t & t<pi/2,f1y, pi<=t & t<3*pi/2,f2y, 3*pi/2<=t & t<2*pi,f3y, 0<=t & t<pi,f4y, pi<=t & t<3*pi/2, f5y);

However I get a weird plot from

fplot(pwx,pwy)

I have attached an image of what i got. Is it me plotting something wrong or is that actually what it is meant to be? I don't mind it all having the same colour and line type, I just need to eventually use pwx, pwy and pwz in combination for a 3D track.

Ameer Hamza on 14 Mar 2020

Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/510838-how-to-make-a-piecewise-function#comment_809933

  • Link

    Direct link to this comment

    https://au.mathworks.com/matlabcentral/answers/510838-how-to-make-a-piecewise-function#comment_809933

Open in MATLAB Online

I guess the problem is happening because two different functions are mapped on the same range, for example, f1y and f4y are mapped between [0, pi/2]. This causes issues for the fplot function. I couldn't find a way without creating two separate piecewise functions.

syms t

a = 2;

b = 1;

f1x = a*cos(t);

f2x = b*cos(t)+3;

f3x = a*cos(t)+3;

f4x = b*cos(t)+4;

f5x = a*cos(t)+5;

f1y = a*sin(t);

f2y = b*sin(t);

f3y = a*sin(t)+1;

f4y = b*sin(t)+1;

f5y = a*sin(t)+1;

ax = axes();

hold(ax);

pwx = piecewise(0<t & t<pi/2,f1x, pi<t & t<3*pi/2,f2x, 3*pi/2<t & t<2*pi,f3x);

pwy = piecewise(0<t & t<pi/2,f1y, pi<t & t<3*pi/2,f2y, 3*pi/2<t & t<2*pi,f3y);

fplot(pwx, pwy, [0 2*pi])

pwx = piecewise(0<t & t<pi,f4x, pi<t & t<3*pi/2, f5x);

pwy = piecewise(0<t & t<pi,f4y, pi<t & t<3*pi/2, f5y);

fplot(pwx, pwy, [0 2*pi])

john warts on 15 Mar 2020

Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/510838-how-to-make-a-piecewise-function#comment_810158

  • Link

    Direct link to this comment

    https://au.mathworks.com/matlabcentral/answers/510838-how-to-make-a-piecewise-function#comment_810158

Thank you very much for your help. It works finally!

Sign in to comment.

More Answers (1)

john warts on 15 Mar 2020

  • Link

    Direct link to this answer

    https://au.mathworks.com/matlabcentral/answers/510838-how-to-make-a-piecewise-function#answer_420174

  • Link

    Direct link to this answer

    https://au.mathworks.com/matlabcentral/answers/510838-how-to-make-a-piecewise-function#answer_420174

Open in MATLAB Online

For anyone who wants to know the final script to make the rollercoaster in 3D:

(The figure command is essential)

syms t

a = 2;

b = 1;

f1x = a*cos(t);

f2x = b*cos(t)+3;

f3x = a*cos(t)+3;

f4x = b*cos(t)+4;

f5x = a*cos(t)+5;

f1y = a*sin(t);

f2y = b*sin(t);

f3y = a*sin(t)+1;

f4y = b*sin(t)+1;

f5y = a*sin(t)+1;

f1z = @(t) 0;

f2z = @(t) 0;

f3z = @(t) 0;

f4z = @(t) t;

f5z = @(t) pi;

figure

hold on;

pwx = piecewise(0<t & t<pi/2,f1x, pi<t & t<3*pi/2,f2x, 3*pi/2<t & t<2*pi,f3x);

pwy = piecewise(0<t & t<pi/2,f1y, pi<t & t<3*pi/2,f2y, 3*pi/2<t & t<2*pi,f3y);

pwz = piecewise(0<=t & t<pi/2,f1z, pi<=t & t<3*pi/2,f2z, 3*pi/2<=t & t<2*pi,f3z);

fplot3(pwx, pwy, pwz, [0 2*pi])

pwx = piecewise(0<t & t<pi,f4x, pi<t & t<3*pi/2, f5x);

pwy = piecewise(0<t & t<pi,f4y, pi<t & t<3*pi/2, f5y);

pwz = piecewise(0<t & t<pi,f4z, pi<t & t<3*pi/2, f5z);

fplot3(pwx, pwy, pwz, [0 2*pi])

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABGraphics2-D and 3-D PlotsLine Plots

Find more on Line Plots in Help Center and File Exchange

Tags

  • piecewise
  • parametric
  • function
  • 3d plots

Products

  • MATLAB

Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How to make a piecewise function? (8)

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

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

How to make a piecewise function? (2024)

FAQs

How do you write a piecewise function answer? ›

Writing Piecewise-Defined Functions
  1. Identify the intervals for which different rules apply.
  2. Determine formulas that describe how to calculate an output from an input in each interval.
  3. Use braces and if-statements to write the function.
Mar 6, 2022

How to evaluate a function piecewise? ›

The first step in evaluating a piecewise function is to determine which function definition applies depending on the value of x that is being input. Once that has been determined, we evaluate the function as usual by substituting in the given value of x.

How do you graph piecewise functions easily? ›

To graph piecewise functions, first identify where the domain is partitioned by boundary values. Graph functions on the domain using tools such as plotting points or transformations. Be sure to use open or closed circles on the endpoints of each domain based on whether the endpoint is included.

What is piecewise function 3 examples? ›

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.

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 grade is piecewise function? ›

1.07 Piecewise functions | Grades 9-12 Math | Common Core Precalculus - 2020 Edition | Mathspace.

How do you evaluate functions? ›

Evaluating a function means finding the value of f(x) =… or y =… that corresponds to a given value of x. To do this, simply replace all the x variables with whatever x has been assigned. For example, if we are asked to evaluate f(4), then x has been assigned the value of 4.

How to find the domain of a piecewise function? ›

To find the domain of a piecewise function, first look at the domains of each function independently. Then check to see if any undefined areas are inside the restraints of the piecewise function. Then check for any other undefined areas. If no undefined areas are there, then the domain is all real x-values.

How to write a piecewise function in Desmos? ›

To graph a piecewise function in Desmos, you can follow these steps. Define your piecewise function using curly braces and separate each piece with a comma. Each piece should be defined using the if function, which has the syntax: if(condition, value_if_true, value_if_false) .

How to write a piecewise function in standard form? ›

The equation of a piecewise function is written with a curly bracket to indicate that it is comprised of more than one subfunction. An example of a piecewise function is 𝑓 ( 𝑥 ) =  𝑥 , 𝑥 < 0 , 𝑥 + 1 , 𝑥 ≥ 0 , where 𝑓 ( 𝑥 ) = 𝑥 when 𝑥 < 0 and 𝑓 ( 𝑥 ) = 𝑥 + 1 when 𝑥 ≥ 0 .

References

Top Articles
Salisbury Steak with Mushroom Gravy Recipe
Ash Reshteh (Persian Greens, Bean and Noodle Soup) Recipe
2018 Jeep Wrangler Unlimited All New for sale - Portland, OR - craigslist
Dairy Queen Lobby Hours
Libiyi Sawsharpener
Visitor Information | Medical Center
What to Do For Dog Upset Stomach
Bhad Bhabie Shares Footage Of Her Child's Father Beating Her Up, Wants Him To 'Get Help'
Ecers-3 Cheat Sheet Free
Geometry Escape Challenge A Answer Key
Caroline Cps.powerschool.com
Voyeuragency
Babyrainbow Private
Dutchess Cleaners Boardman Ohio
978-0137606801
D10 Wrestling Facebook
Byte Delta Dental
De beste uitvaartdiensten die goede rituele diensten aanbieden voor de laatste rituelen
My Homework Lesson 11 Volume Of Composite Figures Answer Key
Melissababy
Ups Drop Off Newton Ks
Governor Brown Signs Legislation Supporting California Legislative Women's Caucus Priorities
Talkstreamlive
Which Sentence is Punctuated Correctly?
Lbrands Login Aces
Cfv Mychart
Visit the UK as a Standard Visitor
Rainfall Map Oklahoma
Does Royal Honey Work For Erectile Dysfunction - SCOBES-AR
Kleinerer: in Sinntal | markt.de
Dailymotion
Unm Hsc Zoom
Ixlggusd
Litter-Robot 3 Pinch Contact & DFI Kit
Craigslist Hamilton Al
Hermann Memorial Urgent Care Near Me
Louisville Volleyball Team Leaks
Metra Schedule Ravinia To Chicago
Avance Primary Care Morrisville
Telegram update adds quote formatting and new linking options
Srg Senior Living Yardi Elearning Login
Raisya Crow on LinkedIn: Breckie Hill Shower Video viral Cucumber Leaks VIDEO Click to watch full…
Heelyqutii
Weather Underground Bonita Springs
Me Tv Quizzes
Babykeilani
Hdmovie2 Sbs
UNC Charlotte Admission Requirements
Ty Glass Sentenced
Phunextra
Tweedehands camper te koop - camper occasion kopen
Www.card-Data.com/Comerica Prepaid Balance
Latest Posts
Article information

Author: Gov. Deandrea McKenzie

Last Updated:

Views: 6026

Rating: 4.6 / 5 (46 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Gov. Deandrea McKenzie

Birthday: 2001-01-17

Address: Suite 769 2454 Marsha Coves, Debbieton, MS 95002

Phone: +813077629322

Job: Real-Estate Executive

Hobby: Archery, Metal detecting, Kitesurfing, Genealogy, Kitesurfing, Calligraphy, Roller skating

Introduction: My name is Gov. Deandrea McKenzie, I am a spotless, clean, glamorous, sparkling, adventurous, nice, brainy person who loves writing and wants to share my knowledge and understanding with you.