How to plot two exponential functions on Matlab?

I need to plot the two exponential functions on same graph. Please help me to write code. Thanks in advance.
f(x) = exp(-(((x-2)/3)^2)/2)
g(x) = 1-exp(-(((x-2)/3)^2))

 採用された回答

Star Strider
Star Strider 2022 年 3 月 28 日

1 投票

Another approach —
x = linspace(0, 10);
f = @(x) exp(-(((x-2)/3).^2)/2);
g = @(x) 1-exp(-(((x-2)/3).^2));
figure
plot(x, [f(x); g(x)])
grid
legend('f(x)','g(x)', 'Location','best')
.

8 件のコメント

Amna Habib
Amna Habib 2022 年 3 月 28 日
Thanks a lot dear @Star Strider
Please tell me that how can we check that f^2 + g^2 <= 1?
Do you have any idea? Kindly share
Star Strider
Star Strider 2022 年 3 月 28 日
The sum of the squared functions becomes asymptotic to 1 beginning at about 10. Prior to that, it oscillates.
x = linspace(0, 50, 1E+3);
f = @(x) exp(-(((x-2)/3).^2)/2);
g = @(x) 1-exp(-(((x-2)/3).^2));
dsum2dx = gradient(f(x).^2 + g(x).^2) ./ gradient(x); % Derivative
figure
plot(x, f(x).^2 + g(x).^2)
hold on
plot(x, dsum2dx)
hold off
grid
legend('f(x)^2+g(x)^2', 'derivative', 'Location','best')
ylim([-0.5 1.5])
syms f(x) g(x) x
f(x) = exp(-(((x-2)/3)^2)/2);
g(x) = 1-exp(-(((x-2)/3)^2));
sumsq = f(x)^2 + g(x)^2
sumsq = 
sumsq = simplify(sumsq, 500)
sumsq = 
lim_sumdq = limit(sumsq, x, Inf)
lim_sumdq = 
1
So the squared sum does not always equal 1. However, it does in the limit. It can be seen that as ‘x’ goes to infinity, the exponential terms go to zero, and the only term left is , which is uniformly 1.
That is about a close to a ‘proof’ as I can get.
.
Amna Habib
Amna Habib 2022 年 3 月 28 日
Thank you so much @Star Strider!
It really helps a lot. Thanks for sharing your thinking!
Star Strider
Star Strider 2022 年 3 月 28 日
As always, my pleasure!
Amna Habib
Amna Habib 2022 年 3 月 29 日
I'm trying to plot this Gaussian distribution as a piecewise function. Can you please help me?
Kindly check this code and mark the mistake please.
X = -12:0.01:12;
f = double(and(X<=0)).* (exp(-(((x)/3).^2)/2)) + ...
double(and(X>0)).*(exp(-(((x)/2).^2)/2));
g = double(and(X<=0)).* (1-exp(-(((x)/3).^2)) + ...
double(and(X>0)).*(1-exp(-((x/2).^2)));
h = f.^2 + g.^2;
plot(x, f, x, g, x, h, 'linewidth', 1.5)
Star Strider
Star Strider 2022 年 3 月 29 日
I would do it slightly differently.
x = linspace(-12, 12);
f = @(x) (x<=0) .* (exp(-(((x)/3).^2)/2)) + (x>0).*(exp(-(((x)/2).^2)/2));
g = @(x) (x<=0).* (1-exp(-(((x)/3).^2))) + (x>0).*(1-exp(-((x/2).^2)));
h = @(x) f(x).^2 + g(x).^2;
figure
plot(x, [f(x); g(x)], x, h(x), 'linewidth', 1.5)
grid
axis([min(x) max(x) 0 1.1])
xlabel('x')
ylabel('Function Value')
legend('f(x)','g(x)','h(x)', 'Location','best')
.
Amna Habib
Amna Habib 2022 年 3 月 29 日
Very Nice @Star Strider!
Thanks a lot for sharing your knowledge!
I really apprecciate your effort.
Star Strider
Star Strider 2022 年 3 月 29 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (1 件)

Sam Chak
Sam Chak 2022 年 3 月 28 日
編集済み: Sam Chak 2022 年 3 月 28 日

1 投票

Try this:
x = -10:0.01:12;
f = exp(-(((x-2)/3).^2)/2);
g = 1-exp(-(((x-2)/3).^2));
plot(x, f, x, g)
xlabel('x')
legend('f(x)', 'g(x)')
grid on

6 件のコメント

Amna Habib
Amna Habib 2022 年 3 月 28 日
Thanks a lot dear @Sam Chak
Please tell me that how can we check that f^2 + g^2 <= 1?
Do you have any idea? Kindly share
Sam Chak
Sam Chak 2022 年 3 月 28 日
First, we plot to see how it looks like.
x = -10:0.01:12;
f = exp(-(((x-2)/3).^2)/2);
g = 1-exp(-(((x-2)/3).^2));
h = f.^2 + g.^2;
plot(x, f, x, g, x, h, 'linewidth', 1.5)
Mathematically speaking, there is only one point where , and this point is at the center of the function, that is , because the Gaussian distribution function will never truly reach zero, unless you consider the trivial solutions at .
To show you this, type this:
index = find(h == 1);
x(index)
and it returns the value of 2.
Hence, when you asked about , the answer is naturally all real values of x. But I guess this is probably not what you are looking for. If you want to find x when is approaching 1, then try this:
index = find(h < 0.999);
x_min = x(min(index))
x_max = x(max(index))
x_min = -5.8800
x_max = 9.8800
Compare these values with the plot of above, and decide if the results are satisfactory.
Amna Habib
Amna Habib 2022 年 3 月 28 日
Very Nice @Sam Chak!
This is what I'm looking for. That you very much for sharing your thinking!
Amna Habib
Amna Habib 2022 年 3 月 29 日
Hi,
I'm trying to plot this Gaussian distribution as a piecewise function. Can you please help me?
Kindly check this code and mark the mistake please.
X = -12:0.01:12;
f = double(and(X<=0)).* (exp(-(((x)/3).^2)/2)) + ...
double(and(X>0)).*(exp(-(((x)/2).^2)/2));
g = double(and(X<=0)).* (1-exp(-(((x)/3).^2)) + ...
double(and(X>0)).*(1-exp(-((x/2).^2)));
h = f.^2 + g.^2;
plot(x, f, x, g, x, h, 'linewidth', 1.5)
Torsten
Torsten 2022 年 3 月 29 日
X = -12:0.01:12;
f = zeros(size(X));
g = zeros(size(X));
f(X<=0) = exp(-((X(X<=0)/3).^2)/2);
f(X>0) = exp(-((X(X>0)/2).^2)/2);
g(X<=0) = 1 - exp(-((X(X<=0)/3).^2));
g(X>0) = 1 - exp(-((X(X>0)/2).^2));
h = f.^2 + g.^2;
plot(X,[f;g;h],'linewidth',1.5)
Amna Habib
Amna Habib 2022 年 3 月 30 日
Thanks a lot @Torsten!
I appreciate your effort!

サインインしてコメントする。

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by