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))

 Accepted Answer

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 Comments

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
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.
.
Thank you so much @Star Strider!
It really helps a lot. Thanks for sharing your thinking!
As always, my pleasure!
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)
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')
.
Very Nice @Star Strider!
Thanks a lot for sharing your knowledge!
I really apprecciate your effort.
As always, my pleasure!

Sign in to comment.

More Answers (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 Comments

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
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.
Very Nice @Sam Chak!
This is what I'm looking for. That you very much for sharing your thinking!
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)
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)
Thanks a lot @Torsten!
I appreciate your effort!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!