Help with piecewise function? Can't use else/if?

3 Ansichten (letzte 30 Tage)
AJ
AJ am 18 Feb. 2014
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
Matt Tearle am 18 Feb. 2014
  1. Homework problem clearly stated as being a homework problem
  2. Own effort and work shown
  3. Problem explained and results shown
Congratulations on writing a great (homework) question!
M. Y. Najjar
M. Y. Najjar am 28 Dez. 2016
Don't you need a double '&' sign in the If statement?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Karan Gill
Karan Gill am 1 Dez. 2016
Bearbeitet: Karan Gill am 17 Okt. 2017
If you have R2016b, you can just use the piecewise function: https://www.mathworks.com/help/symbolic/piecewise.html.
  2 Kommentare
Sarah Palfreyman
Sarah Palfreyman am 14 Dez. 2016
Bearbeitet: Sarah Palfreyman am 14 Dez. 2016
Here is the example.
john Snori
john Snori am 1 Sep. 2021
The second method involves the use of if-else statements along with for loop. In this method, we’ll define all the sub-functions along with the constraints using if-else statements and then we will plot the piecewise function.
Source : https://www.entechin.com/how-to-plot-a-piecewise-function-in-matlab/

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

AJ
AJ am 18 Feb. 2014
If there any possible way anyone can give me the exact code?
I looked at your answer Image Analyst and I appreciate it but I still can't figure out and I think I will understand it after seeing how it's supposed to be done. I've been trying for 5 hours now and I have other work to do :(
  2 Kommentare
Jos (10584)
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
burak ergocmen am 1 Dez. 2016
it really works . thanks for the codes.

Melden Sie sich an, um zu kommentieren.


Image Analyst
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
Jenik Skapa
Jenik Skapa am 26 Mär. 2019
Bearbeitet: Jenik Skapa am 26 Mär. 2019
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
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));

Melden Sie sich an, um zu kommentieren.


Carlos Guerrero García
Carlos Guerrero García am 13 Nov. 2022
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
Image Analyst
Image Analyst am 14 Nov. 2022
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
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!!!!

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by