Hello how do I plot plot function exp^(-x), x <= 0 and exp^(x), x >0, Thanks alot!

10 views (last 30 days)
Hello how do I plot plot function exp^(-x), x <= 0 and exp^(x), x >0, Thanks alot!
This is what I have:
x = 0:0.1:10; y1=exp(-x); y2=exp(x); plot(x,y1,x,y2)

Accepted Answer

KALYAN ACHARJYA
KALYAN ACHARJYA on 25 Mar 2018
x=-10:0.1:10;
y1=exp(-x);
plot(x,y1);
hold on;
y2=exp(x);
plot(x,y2);
  1 Comment
Walter Roberson
Walter Roberson on 25 Mar 2018
This is not correct. Consider x = -2, then according to the problem definition since x < 0, y = exp(-x) which is exp(-(-2)) which is exp(2) -- and only exp(2), no exp(-2) involved. Each x has exactly one y, not two y values.

Sign in to comment.

More Answers (5)

Kristy Kwok
Kristy Kwok on 25 Mar 2018
Edited: Walter Roberson on 25 Mar 2018
Thanks Walter!
This is what I have:
x = 0:0.1:10;
y1=exp(-x);
y2=exp(x);
plot(x,y1,x,y2)
  1 Comment
Walter Roberson
Walter Roberson on 25 Mar 2018
No, this is no right.
  1. You do not plot with negative x.
  2. the equations from the question are set up such that exp() of a negative number is never taken, but you take exp(-x) for positive x.

Sign in to comment.


Kristy Kwok
Kristy Kwok on 25 Mar 2018
Edited: Walter Roberson on 25 Mar 2018
Thanks Walter!
I have come up with this:
x = 0:0.1:10;
y1=exp(abs(x));
y2=exp(x);
plot(x,y1,x,y2)
But I wasnt sure what to do with the x<=0 and X >0

Kristy Kwok
Kristy Kwok on 25 Mar 2018
Edited: Walter Roberson on 25 Mar 2018
Hi Walter,
I understand exp (abs(x)) is the same as exp(x) but I wasnt sure how to specify the limits x<=0 and X >0?
Would that be is the range of X? So it would be like this:
x = -3 :0.1:10;
y=exp(abs(x));
plot(x,y)
?
  2 Comments
Walter Roberson
Walter Roberson on 25 Mar 2018
Yes, like that.
The symmetry of it is not apparent because you are going only to 3 to the left of x = 0 but to 10 to the right of x = 0.
Kristy Kwok
Kristy Kwok on 25 Mar 2018
Edited: Kristy Kwok on 25 Mar 2018
Thank you so much Walter!! Have a great day!:D
I have the code like this: x=-10:0.1:10; y1=exp(-x); plot(x,y1);

Sign in to comment.


Kristy Kwok
Kristy Kwok on 25 Mar 2018
Hello Kalyan,
Thank you so much! I am sorry but I am still lost on how to specify the limits x<=0 and X >0, can you please briefly explain?
Many thanks!

Walter Roberson
Walter Roberson on 25 Mar 2018
Sigh. Once more with making the different ranges explicit:
x = -3 :0.1:10;
y1 = zeros(size(x));
for K = 1 : length(x)
this_x = x(K);
if this_x < 0
y1(K) = exp(-this_x);
else
y1(K) = exp(this_x);
end
end
%Which can also be written as
y2 = zeros(size(x));
mask = x < 0;
y2(mask) = exp(-x(mask));
y2(~mask) = exp(x(~mask));
%and can also be written
x = -3 :0.1:10;
y3 = exp(abs(x));
subplot(1,4,1)
plot(x, y1);
title('loop')
subplot(1,4,2)
plot(x, y2)
title('logical indexing');
subplot(1,4,3)
plot(x, y3)
title('abs()')
subplot(1,4,4)
plot(x, y1-y2, 'b*-', x, y1-y3, 'g.-')
title('difference between methods. Yes, it is EXACTLY 0')
legend('loop minus mask', 'loop minus abs')
fprintf('maximum difference between loop and mask: %g\n', max(abs(y1-y2)));
fprintf('maximum difference between loop and abs: %g\n', max(abs(y1-y3)));
fprintf('maximum difference between mask and abs: %g\n', max(abs(y2-y3)));

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!