# fitLogLawV2

## 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,cod] = fitLogLawV2(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:

- ADCP_DispCoef This program computes the longitudinal dispersion coefficient from ADCP
- STA_MeanProfileV2 Compute the mean profile from stationary measurements at a point.

## SOURCE CODE

    0001 function [ustar,zo,cod] = fitLogLawV2(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, 11-16-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] = fitLogLawV2(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 %figure(1); clf; plot(u,z,'ko-'); xlim([0 max(u)]); ylim([0 max(z)]);pause
    0019
    0020 if (nargin < 3)
    0021     h = max(z);
    0022 end
    0023
    0024 zpred = linspace(0,h,100);
    0025
    0026 kappa = 0.41; % Von Karman constant
    0027
    0028 [p,S] = polyfit(log(z),u,1);
    0029
    0030 ustar = kappa*p(1);
    0031
    0032 zo = exp(-p(2)/p(1));
    0033
    0034 ssr = S.normr.^2;
    0035
    0036 sstot = sum((u - mean(u)).^2);
    0037
    0038 cod = 1 - ssr./sstot; %Coefficient of determination (r^2)
    0039
    0040
    0041
    0042
    0043
    0044
    0045
    0046
    0047
    0048
    0049