Uploaded by neotide93

Keating BIOE241 HW5

HOMEWORK #05 (0b0101, 0x5)
Due: 03/09/2018
Student Name: Collin Keating
1. (2 points) We want to develop a script M-file to perform the following analysis and plotting of the contamination data used in class (download contamination_data.mat from the 'Matlab data' folder on ELMS). Assume we have determined the linear trend surface to be z=u1+u2*x+u3*y, where u1 = 1.8371, u2 = -0.0005, and u3 = -0.001. We use this linear trend as the expectation (i.e. µ) of the data. First, create a square grid over x coordinates ranging between [0, 1000] and y coordinates ranging between [0, 1000], with 101 points along each direction. Then calculate the theoretical probability (in percentage) that 2 ppb is exceeded as shown in the March 5th class now across the square grid you created. Finally, plot this probability surface over the square grid without gridlines.
The first three lines and the last three lines of the script are given below:
%------start of script-----------------------
u=[1.8371 -0.0005 -0.001];
approx_ppb = u(1)+u(2)*easting+u(3)*northing;
residuals = ppb - approx_ppb;
xlabel('Easting (m)') ylabel('Northing (m)') zlabel('Probability of Exceeding 2 ppb (%)')
%------end of script-------------------------
Fill in the rest of the script. (Tip: you need to use the linear trend equation to calculate µ across the grid.)
2. (1 point) True or False: variables defined and used in a MATLAB script (script M-file) become part of the MATLAB workspace once the script has been run at least once, and, the script also has access to variables defined in the MATLAB workspace. This is opposed to functions (defined in function M-files) for which variables are local to the function and for which workspace variables are not, by default, directly accessible.
3. (1 point) Develop a function M-file named gaussian that takes a vector of values (eg. x), a value of the mean (eg. LaTeX: \mu µ ) and a value of the standard deviation (eg. LaTeX: \sigma s ) as inputs, and returns as output a vector of values of the Gaussian function:HW5_Q3.png
You should verify that your function works using the following script, that compares the output of randn() to the gaussian (with the same mean and variance):
%------start of script-----------------------
mu = -28; sigma = 5; n = 15000;
x = linspace(mu-4*sigma,mu+4*sigma,n+1);
[count,bins] = hist(mu + sigma*randn(n,1),51);
hold on;
hold off
xlabel('value: x'); ylabel('probability: p(x)')
%------end of script-------------------------
Paste the code of your gaussian.m function M-file in the space below.
function g=gaussian(x,mu,sigma)
4. (1 point) Repeat problem 3 using an anonymous function (one-liner, inlined-function) rather than a function M-file. Store your anonymous function in the variable NF, below, and verify it using the script underneath (adapted from problem 3). %------start of script-----------------------
NF = @(x,mu,sigma)(1/sqrt(2*pi*sigma^2))*exp(-(x-mu).^2./(2*sigma^2));
mu = -28; sigma = 5; n = 15000;
x = linspace(mu-4*sigma,mu+4*sigma,n+1);
plot(x, NF(x,mu,sigma),'r');
[count,bins] = hist(mu + sigma*randn(n,1),51);
hold on;
hold off
xlabel('value: x'); ylabel('probability: p(x)')
%------end of script-------------------------
5. (3 points) Develop a function M-file named cal3ncm that takes a function handle (eg. fun), a starting point (eg. xs) and an end point (eg. xe) as inputs, and returns the first 3 normalized central moments of the input function, as calculated over the interval xs to xe. The first 3 moments of a distribution function, f(x), are calculated as:
m0 = integral(from xs to xe) of: f(x)
m1 = integral(from xs to xe) of: x * f(x)
m2 = integral(from xs to xe) of: x2 * f(x)
These moments are normalized and centralized using:
m0c = m0 -> mass of the distribution
m1c = m1/m0 -> mean of the distribution
m2c = m2/m0 – (m1c)2 -> variance of the distribution
Inspire yourself with the trpzint() function presented in class (it calculates m0). Use n = 10,001 points to compute the integrals, using the trapezoidal method (trapz()). You can test your cal3ncm() function using the script below that also relies on your gaussian() function of question 3. It should print: mass = 1 (approximately 1), mean = -28, variance = 25 (approximately 25) and std = 5 (approximately 5):
%------start of script-----------------------
% mu and sigma in the first line are the mean and standard deviation used to generate the gaussian:
mu = -28; sigma = 5;
xs = mu-4*sigma; xe = mu+4*sigma;
GPDF = @(x)gaussian(x,mu,sigma);
[mass mean variance] = cal3ncm(GPDF,xs,xe)
std = sqrt(variance)
%------end of script-------------------------
Make sure that your cal3ncm() function works and paste its code (your function M-file) in the space below.
function [m0c,m1c,m2c]=cal3ncm(fun,xs,xe)
s=['fun' '(x)'];
6. (2 points) Develop a function M-file named flogfit that takes a matrix of 2-D data points as input (x-coordinate in the 1st column and y-coordinate in the 2nd column) and:
(1) finds the least-square approximation of a logarithmic function through this data ( y = u1 + u2 * ln(x) ), using the equation-solving method based on LEFT DIVISION of the vector of constants (B) by the coefficient matrix (A), as shown in class.
(2) finds the coefficient of determination for the fit: R2 = 1 - SSE/SSTO.
(3) returns the estimated coefficients (as a column vector: U = [u1 u2]') as its first output and the coefficient of determination (R2) as its second output.
You should test your function using the following MATLAB script (try it several times):
%------start of script----------------------- n = 201;
x = linspace(0.01,8,n)';
y = 9.17+rand(1) - 2.22*log(x) + 1.25*randn(n,1);
[flogc, cdet] = flogfit( [x y] )
plot(x,y,'k*',x,[ones(n,1) log(x)]*flogc)
legend('data','fit'); xlabel('x'); ylabel('y')
%------end of script-------------------------
Verify that your function works then paste the code of your function M-file below.