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
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
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
2018-3-25
编辑:Walter Roberson
2018-3-25
1 个评论
Walter Roberson
2018-3-25
No, this is no right.
- You do not plot with negative x.
- 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
2018-3-25
编辑:Walter Roberson
2018-3-25
1 个评论
Walter Roberson
2018-3-25
Your plot has no negative x values.
If you use abs(x) you do not need two different functions.
Kristy Kwok
2018-3-25
编辑:Walter Roberson
2018-3-25
2 个评论
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.
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)));
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!