Help with piecewise function? Can't use else/if?
Ältere Kommentare anzeigen
Hi, I am having trouble on a piece-wise homework problem I am having.

This is my code. I plot it and in the middle of the graph from negative pi to positive pi where y should be the cosine of x isn't right. I just have a straight line at y=-1 across the graph. I know I need to make it a vector or a loop it so it doesn't use if/else and skip the middle cosine function.

Whenever I try to get a function command I get this: Function definitions are not permitted in this context.
code so far:
clear all
close all
clc
x=-2*pi:.01:2*pi
if (x<-pi)
y=-1;
elseif(x>=-pi&x<=pi)
y=cos(x);
else(x>pi)
y=-1;
plot(x,y)
end
This is what I'm getting: The middle part (cosine function) is wrong. I was told it's just graphing the part after else.

Help please?
Thank you.
2 Kommentare
Matt Tearle
am 18 Feb. 2014
- Homework problem clearly stated as being a homework problem
- Own effort and work shown
- Problem explained and results shown
Congratulations on writing a great (homework) question!
M. Y. Najjar
am 28 Dez. 2016
Don't you need a double '&' sign in the If statement?
Akzeptierte Antwort
Weitere Antworten (3)
AJ
am 18 Feb. 2014
3 Stimmen
2 Kommentare
Jos (10584)
am 18 Feb. 2014
Take a look at this:
% Step 1: initialise x and y
x = -10:2:10
y = zeros(size(x))
% Step 2: selective processing
tf = x < -5
y(tf) = -5
tf = x > -5 & x < 5
y(tf) = -2 + 2 * x(tf)
tf = x > 5
y(tf) = 5
% step 3: visualisation
plot(x,y,'bo-')
burak ergocmen
am 1 Dez. 2016
it really works . thanks for the codes.
Image Analyst
am 18 Feb. 2014
Bearbeitet: Image Analyst
am 18 Feb. 2014
Use two &:
elseif x(k) >=-pi && x(k) <= pi
and you need to make an array out of y=-1:
y(k) = -1;
otherwise it's just a single number, not an array of -1s. Plus you need to have it in a loop over k like I mentioned
for k = 1 : length(x)
then everything inside the look has a (k) index. Of course there is a vectorized way to do it, if you want that.
6 Kommentare
Matt Tearle
am 18 Feb. 2014
I just want to add that you should definitely pay attention to the orange squiggly underlines in the Editor. If you read the messages you get when you hover over them, you'll see that MATLAB is hinting at what Image Analyst is saying: you're comparing the vector x with a scalar pi -- that's perfectly valid but is probably not what you're trying to do in an if statement.
Image Analyst also mentioned a vectorized solution. In this case, you could start with y being a vector (the same size as x) of -1s, then change some of them to cos(x). The ones to change are those where x is between -pi and pi. MATLAB has a beautiful syntax for this kind of thing.
Image Analyst
am 18 Feb. 2014
Five hours!? I told you exactly how to do it. Just use && and wrap in a for loop. You'll get this:
x = -2*pi : 0.01 : 2 * pi;
for k = 1 : length(x)
if x(k) < -pi
y(k) = -1;
elseif x(k) >= -pi && x(k) < pi
y(k) = cos(x(k));
else
y(k) = -1;
end
end
plot(x, y, 'r*-');
grid on;

Image Analyst
am 18 Feb. 2014
So did I get an A on my homework?
Taehun Kim
am 24 Jul. 2017
yeah i got answer thx
I would use this construction without the "for" loop:
x = -2*pi : pi/5 : 2 * pi;
y = NaN * ones(size(x));
y(x < -pi) = -1;
y((x >= -pi) & (x < pi)) = cos(x((x >= -pi) & (x < pi)));
y(x >= pi) = -1;
plot(x, y, 'r*-'); grid on;
Steven Lord
am 26 Mär. 2019
FYI you can use NaN to create an array without needing to first create a ones array.
y = NaN(size(x));
Carlos Guerrero García
am 13 Nov. 2022
1 Stimme
And what about this code ???
x=-5:0.1:5; g=-1+(abs(x)<=pi).*(1+cos(x)); plot(x,g)
Do you like it ???
4 Kommentare
Bonus points for succinctness
Though you might consider adjusting your limits to [-1 1]*2*pi (what the original problem used) instead of [-5 5]. That said, it's an old question; I suppose you're free to choose how much you want to bend the original requirements. After all, it's a very minor detail, and I doubt OP will mind the difference.
That, and putting your code into a code formatting block helps with readability. Once you do that, you can also hit the run button and your answer will include the generated plot as well. That makes it easy for future readers to see that your code does what you say it does.
Carlos Guerrero García
am 14 Nov. 2022
Thanks for the edition and the suggestions!!!!
I guess you are choosing not to take the suggestions, but I'll do them here:
x = -2 * pi : 0.01 : 2 * pi;
g = -1 + (abs(x) <= pi) .* (1 + cos(x));
plot(x, g, 'b-', 'LineWidth', 2)
grid on;
xlabel('x');
ylabel('g');
Carlos Guerrero García
am 14 Nov. 2022
Thanks to Image Analyst....I have no time enough to make the corrections suggested but thanks to your improvemets to my basic script!!!!
Kategorien
Mehr zu Logical finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

