Plot Piecewise function graph

2 次查看(过去 30 天)
L
L 2024-3-9
评论: Alexander 2024-3-10
Here's my code but i can't get the graph as shown in the picture.
% Use conditional statements and loops to calculate function values for
% different input ranges
x = -2.99*pi: 0.01: 3*pi; % Create domain from -2.99pi to 3pi, at increment of 0.01
y = zeros(1,length(x));
for i = 1: length(x) % For each statement i, it will pass through each value of x
if x(i) > -3*pi && x(i) < -pi % If x lies at −3pi < x <-pi,
y(i) = 1./log(2)*sin(x(i)); % the function is y = 1/(ln2)(sinx)
elseif x(i) >= -pi && x(i) <=2 % If x lies at -pi <= x <= 2,
y(i) = abs(x(i)) - 3; % the function is y = |x| -3
else
y(i) = exp(1); % If x doesn't lies at above interval, the function is y = e
end
end
plot(x, y);
text(3.2\pi, 0.2 , 'x'); text(0.2\pi, 3.5, 'y');
axis([-4\pi 4\pi -4 4]);
xticklabels({'-3\pi', '-2\pi', '-\pi', '0', '\pi', '2\pi', '3\pi'}); % Display tick marks along the x-axis by specifying the text to show pi symbol
yticks(-3: 1: 3); % Display tick marks along the y-axis 0.5,1 and 1.5
box off;
**This is the graph should be look like.

回答(3 个)

Paul
Paul 2024-3-9
This line
y(i) = 1./log(2)*sin(x(i));
is incorrect.
The order of operations for multiplication and division is that they are evaluated in order from left to right. So that line could also be expressed as
y(i) = ( 1./log(2) ) * sin(x(i));
i.e., the division is done first, and the result of the division becomes the first operand for the multiplication.
Hopefully that gives you a clue as to how to fix the code (at least that part of it).
  5 个评论
Paul
Paul 2024-3-10
Yes, those two lines do yield the same result. The first line is used in the code in the question. I used the extra parentheses in the second to emphasize the order of operations used to evaluate the first.
Neither of those lines are a correct implementation of
which was the point I was trying to make to lead the OP to the correct Matlab expression. I think the OP did eventually get the correct expression, but that comment has since been deleted.

请先登录,再进行评论。


Star Strider
Star Strider 2024-3-9
To do this in the Symbolic Math Toolbox, just write it essentially as in your original post —
syms x
f(x) = piecewise((-3*pi<x<-pi), 1/(log(2)*sin(x)), (-pi<=x<=2), abs(x)-3, (2<x<=3*pi), exp(1))
f(x) = 
figure
fplot(f, [-4*pi 4*pi], 'MeshDensity',1E2)
grid
ylim([-1 1]*5)
.
  2 个评论
L
L 2024-3-10
how to remove the asymptote for x=-2pi?
Star Strider
Star Strider 2024-3-10
First, turn all of them off, then re-draw the ones you want using the xline funciton —
syms x
f(x) = piecewise((-3*pi<x<-pi), 1/(log(2)*sin(x)), (-pi<=x<=2), abs(x)-3, (2<x<=3*pi), exp(1))
f(x) = 
figure
hfp = fplot(f, [-4*pi 4*pi], 'MeshDensity',1E2);
grid
hfp.ShowPoles = 'off'; % Turn Off All Asymptotes
xline(-3*pi, '--k') % Draw Asymptote At -3*pi
ylim([-1 1]*5)
.

请先登录,再进行评论。


Alexander
Alexander 2024-3-10
编辑:Alexander 2024-3-10
You should change
axis([-4\pi 4\pi -4 4]);
to
axis([-4*pi 4*pi -4 4]);
in the first step. Than you can see your complete function.

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by