function homework_example %HOMEWORK_EXAMPLE Example MATLAB function for LaTeX homework templates. % This function produces a figure by using a number of MATLAB commands. It % should help generate the power of the \matlabscript macro for including % MATLAB functions and scripts within code. % % The figure generated by this function will be used to demonstrate how to % use \scalefig. % Generate 100 random data points with a normal distribution X = randn(1, 100); % Generate a histogram in N at 100 centers specified at Xcenters [N, Xcenters] = hist(X, length(X)/10); % Plot the sample CDF of the data clf; subplot(211); cdf_datafig = stairs( sort(X), linspace(0,1,length(X)) ); % Plot an expected CDF on top of it hold on; xlims = xlim; expX = linspace(xlims(1), xlims(2), 1000); cdf_expfig = plot( expX, normcdf(expX), 'r--' ); % Label the figure legend( [cdf_datafig, cdf_expfig], 'Data Distribution', ... 'Expected Distribution', 4 ); xlabel('x'); ylabel('F(x) = P(X \leq x)'); title('Normal Cummulative Distribution Function'); %%% Notice the use of TeX in the above ylabel. In MATLAB 7.0, LaTeX can %%% also be used if the interpreter on the text object is changed from %%% 'tex" to 'latex'. % Plot the sample pdf of the data subplot(212); pdf_datafig = bar( Xcenters, N/length(X)/(Xcenters(2)-Xcenters(1)) ); xlim( xlims ); % Plot an expected pdf on top of it hold on; pdf_expfig = plot( expX, normpdf(expX), 'r--' ); % Label the figure legend( [pdf_datafig, pdf_expfig], 'Data Density', ... 'Expected Density', 1 ); xlabel('x'); ylabel('dF(x)/dx'); title('Normal Probability Density Function');