# MATLAB Help

• 02-23-2003
Lrnr
MATLAB Help
I have been trying to write an m file in matlab which can plot a
function which changes within the limits.

I can plot a function within a given limit. But the function I
have to plot is discontinuous.

The function is

y = exp(x+1) x < -1
y = 2 + cos(pi*x) -1 <= x < 5
y = 10(x - 5) + 1 x >=5

And I want to plot y for -2 < x < 6

I can plot if it is simply y = exp(x+1) for -2 < x < 6
but there are three conditions on the top. how do i
write the m file ?

thanks
• 02-23-2003
Shiro
Using the different intervals perhaps? First plot from -2 to -1, then from -1 to 5 and finally from 5 to 6. You could put that into the array in which you store the signal and then plot the array.
• 02-23-2003
Lrnr
Here's what I tried so far
Here's what I tried so far... it doesn't work though.. it says the vectors are not equal. i am a beginner, i dont know what it is talking about.

for x = [-2 : 1 : 6]
if x < -1
i = [-2 : 0.1 : -9.9];
u = polyval(exp(i+1), i);
end

if x >= -1
if x < 5
j = [-1 : 0.1 : 4.8];
v = polyval(2 + cos(pi*j), j);
end
end

if x > 5
k = [5 : 0.1 : 6];
w = polyval(10*(k-5)+1, k);
end
end

x = [-2 : .1 : 5.9];

plot (x, u, x, v, x, w)