i have this two functions how can i plot them together

1 view (last 30 days)
Length=100000
mu=10
sigma=1
X=sigma*randn(Length,1)+mu;
plot(X)
histogram(X)
grid on
n=100
[f,x]=hist(X,n)
bar(x,f/trapz(x,f));hold off;
g = (1/(sqrt(2*pi)*sigma))*exp(-((x-mu).^2)/(2*sigma^2))
plot(X,g) ;hold on ;grid on
can anyone help me please , i don't where is my mistake , my final output should look like the attached picture

Answers (1)

Brattv
Brattv on 21 Apr 2016
Hi, Your problem is the last line plot(X,g). You are saying that you have a vector g with 100 point, that you want to plot versus a vector X with 100 000 points. Do the following
Use the histogram bin centers from "x" to find the first and last bin in the histogram. You wanted to plot a function with 100 points, which means that you find the step by finding the difference of the first and last bin and divide by 100.
if true
% Find the first and last valye
plotStep = (x(end) - x(1))/100;
% The steps put into a vector
lengthVect = x(1)+plotStep:plotStep:x(end);
% Plotting.
plot(lengthVect,g) ;hold on ;grid on
end

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!