# Piecewise function, ranges are variable

4 views (last 30 days)
Franziska on 6 Aug 2019
Commented: Franziska on 6 Aug 2019
Hi everyone
I want to plot this piecewise function:
a function in terms of ϵ, but where I can compare for different values of S. S is not only in some of the functions, but also defines the ranges of ϵ for which the function holds.
I tried to plot this: (but it didn't work).
function [y] = pwlaborsupply0(EPS, Beta, S)
if EPS<-(1+4*Beta)*S
y= 1 + (1/(1+4*Beta))*EPS;
elseif -(1+4*Beta)*S < EPS & EPS <= 4*Beta*S
y= 1+ (S+2*EPS)/(1+8*Beta);
elseif 4*Beta*S< EPS & EPS <=(1+4*Beta)*S
y=1+S;
else
y= 1 + (1/(1+4*Beta))*EPS;
end
% and then:
EPS=linspace(-0.5,0.5);
L0=pwlaborsupply0(EPS, 0.1,0.25);
plot(EPS, L0)
When I plot every part of the function separatly for a specific combinatin of β and S, (, ) it works: (but I calculated the ranges of ϵ manually, and I want to compare the result for different S, without going through this every time..)
Beta=0.1;
S=0.25;
EPS1=linspace(-0.5,-0.35);
EPS2=linspace(-0.35, 0.1);
EPS3=linspace(0.1, 0.35);
EPS4=linspace(0.35,0.5);
L01 = 1 + (1/(1+4*Beta))*EPS1;
L02 = 1+ (S+2*EPS2)/(1+8*Beta);
L03 = ones(size(EPS3))*(1+S);
L04 = 1 + (1/(1+4*Beta))*EPS4;
plot(EPS1,L01,EPS2,L02,EPS3,L03,EPS4,L04)
Any ideas how I can solve this, or where the problem in my definition of the function is?
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### Accepted Answer

Torsten on 6 Aug 2019
Edited: Torsten on 6 Aug 2019
EPS = linspace(-0.5,0.5);
L0 = arrayfun(@(C)pwlaborsupply0(C, 0.1,0.25),EPS);
plot(EPS,L0)
##### 1 CommentShow -1 older commentsHide -1 older comments
Franziska on 6 Aug 2019
Works perfectly, thanks!!

Sign in to comment.

### Categories

Find more on Legend in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!