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

10 次查看(过去 30 天)
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)

采纳的回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2018-3-25
x=-10:0.1:10;
y1=exp(-x);
plot(x,y1);
hold on;
y2=exp(x);
plot(x,y2);
  1 个评论
Walter Roberson
Walter Roberson 2018-3-25
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.

请先登录,再进行评论。

更多回答(5 个)

Kristy Kwok
Kristy Kwok 2018-3-25
编辑:Walter Roberson 2018-3-25
Thanks Walter!
This is what I have:
x = 0:0.1:10;
y1=exp(-x);
y2=exp(x);
plot(x,y1,x,y2)
  1 个评论
Walter Roberson
Walter Roberson 2018-3-25
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.

请先登录,再进行评论。


Kristy Kwok
Kristy Kwok 2018-3-25
编辑:Walter Roberson 2018-3-25
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 2018-3-25
编辑:Walter Roberson 2018-3-25
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 个评论
Walter Roberson
Walter Roberson 2018-3-25
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 2018-3-25
编辑:Kristy Kwok 2018-3-25
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);

请先登录,再进行评论。


Kristy Kwok
Kristy Kwok 2018-3-25
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 2018-3-25
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)));

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by