# fitLogLaw

## PURPOSE

This function fits a log law of the form u/u* = 1/kappa*ln(z/zo) to the given data

## SYNOPSIS

function [ustar,zo,ks,cod,upred,zpred,delta] = fitLogLaw(u,z,h)

## DESCRIPTION

     This function fits a log law of the form u/u* = 1/kappa*ln(z/zo) to the given data
     and returns u*, zo, and the sum of the squared residuals ssr.

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

- STA_MeanProfile Compute the mean priole from stationary measurements at a point.

## SOURCE CODE

    0001 function [ustar,zo,ks,cod,upred,zpred,delta] = fitLogLaw(u,z,h)
    0002
    0003 % This function fits a log law of the form u/u* = 1/kappa*ln(z/zo) to the given data
    0004 % and returns u*, zo, and the sum of the squared residuals ssr.
    0005
    0006 % P.R. Jackson, 10-8-10
    0007
    0008 %Example:
    0009
    0010 % clear all
    0011 % z = 5:50; u = 0.046/0.41*log(z/0.008);
    0012 % u = u + (2*rand(size(u))-1).*0.05.*u;
    0013 % [ustar,zo,ks,ssr,upred,zpred,delta] = fitLogLaw(u,z);
    0014 % figure(1); clf; plot(u,z); hold on
    0015 % plot(upred,zpred,'r-'); hold on
    0016 % plot(upred+delta',zpred,'r:',upred-delta',zpred,'r:'); hold on
    0017
    0018 if (nargin < 3)
    0019     h = max(z);
    0020 end
    0021
    0022 zpred = linspace(0,h,100);
    0023
    0024 %kappa = 0.41; % Von Karman constant
    0025
    0026 coefinit   = [1 1]; %initial guess at coefficients
    0027 logfcn     = inline('coef(1)./0.41.*log(z./coef(2))', 'coef', 'z');
    0028 [coef,r,J,sig,mse] = nlinfit(z,u,logfcn,coefinit); %nonlinear fit
    0029 [upred,delta] = nlpredci(logfcn,zpred,coef,r,'covar',sig);
    0030 ustar = coef(1);
    0031 zo    = coef(2);
    0032
    0033 ks = 30*zo; %Nikuradse equivanelt sand roughness (for input in m)
    0034
    0035 ssr = sum(r.^2);
    0036
    0037 sstot = sum((u - mean(u)).^2);
    0038
    0039 cod = 1 - ssr./sstot;
    0040