Plot Piecewise function graph

조회 수: 1 (최근 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.
  댓글 수: 1
L
L 2024년 3월 10일
편집: L 2024년 3월 10일
How to create asymptotes?

댓글을 달려면 로그인하십시오.

답변 (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.
Alexander
Alexander 2024년 3월 10일
Thanks.

댓글을 달려면 로그인하십시오.


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에서 Calculus에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by