Professional Documents
Culture Documents
% Define variables:
% m -- Number of columns
% n -- Number of rows
% ran -- Output array
% Record of revisions:
% Date Programmer Description of change
% ==== ========== =====================
% 04/10/15 S. J. Chapman Original code
185
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
% Define variables:
% ii -- Index variable
% ISEED -- Random number seed (global)
% jj -- Index variable
% m -- Number of columns
% msg -- Error message
% n -- Number of rows
% ran -- Output array
%
% Record of revisions:
% Date Programmer Description of change
% ==== ========== =====================
% 02/04/14 S. J. Chapman Original code
% 1. 04/05/15 S. J. Chapman Modified for 0 arguments
186
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
7.2 Function randomn produces samples from a uniform random distribution on the range [low,high). If
low and high are not supplied, they default to 0 and 1 respectively. Note that function random0 is
placed in a folder named private below the folder containing randomn. Function random0 is only
accessible from function in the parent directory, not from other functions. A listing of the directory is
shown below:
C:\Data\book\matlab\5e\soln\Ex7.2>dir /s
Volume in drive C is SYSTEM
Volume Serial Number is 9084-C7B1
Directory of C:\Data\book\matlab\5e\soln\Ex7.2
Directory of C:\Data\book\matlab\5e\soln\Ex7.2\private
C:\Data\book\matlab\5e\soln\Ex7.2>
% Define variables:
% high -- Upper end of range
% low -- Lower end of range
% m -- Number of columns
% n -- Number of rows
% ran -- Output array
% Record of revisions:
% Date Programmer Description of change
187
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
% ==== ========== =====================
% 04/10/15 S. J. Chapman Original code
The following function appears in a directory name private below the directory containing function
randomn:
% Define variables:
% ii -- Index variable
% ISEED -- Random number seed (global)
% jj -- Index variable
% m -- Number of columns
% msg -- Error message
% n -- Number of rows
% ran -- Output array
%
% Record of revisions:
% Date Programmer Description of change
% ==== ========== =====================
% 02/04/14 S. J. Chapman Original code
% 1. 04/05/15 S. J. Chapman Modified for 0 arguments
188
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
% Declare global values
global ISEED % Seed for random number generator
When this function is used to generate a 4 x 4 array of random numbers between 3 and 4 the results are: :
>> randomn(4,4,3,4)
ans =
3.7858 3.0238 3.4879 3.5630
3.5319 3.0040 3.3435 3.0988
3.4300 3.5385 3.0946 3.6670
3.1507 3.1958 3.6362 3.9179
7.3 A single function hyperbolic that calculates the hyperbolic sine, cosine, and tangent functions is shown
below. Note that sinh, cosh, and tanh are subfunctions here.
% Define variables:
% fun -- Function to evaluate
189
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
% x -- Input value
% result -- Result of calculation
% Record of revisions:
% Date Programmer Description of change
% ==== ========== =====================
% 04/10/15 S. J. Chapman Original code
% Calculate function
switch (fun)
case 'sinh',
result = sinh(x);
case 'cosh',
result = cosh(x);
case 'tanh',
result = tanh(x);
otherwise,
msg = ['Invalid input function: ' fun];
error(msg);
end
% Define variables:
% x -- input value
% result -- Result of calculation
% Record of revisions:
% Date Programmer Description of change
% ==== ========== =====================
% 04/10/15 S. J. Chapman Original code
% Calculate value.
result = (exp(x) - exp(-x)) / 2;
190
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
% Define variables:
% x -- input value
% result -- Result of calculation
% Record of revisions:
% Date Programmer Description of change
% ==== ========== =====================
% 04/10/15 S. J. Chapman Original code
% Calculate value.
result = (exp(x) + exp(-x)) / 2;
% Define variables:
% x -- input value
% result -- Result of calculation
% Record of revisions:
% Date Programmer Description of change
% ==== ========== =====================
% 04/10/15 S. J. Chapman Original code
7.4 A program to create the three specified anonymous functions, and then to plot h ( f ( x ) , g ( x) ) over the
range −10 ≤ x ≤ 10 is shown below:
% Script file: test_anonymous.m
%
% Purpose:
% To create three anonymous functions, and then create a
% plot using them.
%
191
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
% Record of revisions:
% Date Programmer Description of change
% ==== ========== =====================
% 04/10/15 S. J. Chapman Original code
%
% Define variables:
% f -- Function handle
% g -- Function handle
% h -- Function handle
% x -- Input data samples
7.5 The commands to plot the function f ( x) = 1/ x over the range 0.1 ≤ x ≤ 10.0 using function fplot
are shown below:
192
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
xlabel('\bfx');
ylabel('\bff(x)');
grid on;
7.6 A script file to find the minimum of the function y ( x ) = x 4 − 3 x 2 + 2 x over the interval [0.5 1.5] using
function fminbnd is shown below:
193
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
% Find the minimum of the function in the range
% 0.5 <= x <= 1.5.
[minloc,minval] = fminbnd(y,0.5,1.5);
disp(['The minimum is at location ' num2str(minloc)]);
>> test_fminbnd
The minimum is at location 0.99999
7.7 A script file to find the minimum of the function y ( x ) = x 4 − 3 x 2 + 2 x over the interval [0.5 1.5] using
function fminbnd is shown below:
>> test_fminbnd
The minimum in the range (-2,2) is at location -1.366
The minimum in the range (-1.5,0.5) is at location -1.366
195
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
7.8 A script file to create a histogram of samples from a random normal distribution is shown below:
% Create histogram
hist(dist,21);
title('\bfHistogram of random values');
xlabel('\bfValue');
ylabel('\bfCount');
196
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
When this program is executed, the results are:
>> test_randn
7.9 A Rose Plot is a circular histogram, where the angle data theta is divided into the specified number of
bins, and the count of values in each bin is accumulated. A script file to create a rose plot of samples from
a random normal distribution is shown below:
% Create histogram
rose(dist,21);
title('\bfRose Plot of random values');
197
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
xlabel('\bfValue');
ylabel('\bfCount');
>> test_rose
7.10 A function that finds the maximum and minimum values of a user-supplied function over a specified
interval is given below:
198
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
% xmin = location of smallest value found
% min_value = smallest value found
% xman = location of largest value found
% max_value = largest value found
% Define variables:
% dx -- Step size
% x -- Input values to evaluate fun at
% y -- Function output
% Record of revisions:
% Date Programmer Description of change
% ==== ========== =====================
% 04/12/15 S. J. Chapman Original code
% Evaluate function
y = feval(func,x);
% Display results
fprintf('The maximum value is %.4f at %.2f\n',max_value,xmax);
fprintf('The minimum value is %.4f at %.2f\n',min_value,xmin);
function y = fun(x)
%FUN Function to test extremes
% Record of revisions:
% Date Programmer Description of change
% ==== ========== =====================
% 07/21/11 S. J. Chapman Original code
When this program is run, the results are as shown below. The plot of the function below shows that the
program has correctly identified the extremes over the specified interval.
» test_extremes
The maximum value is 3.4163 at 0.62
The minimum value is -9.0000 at -1.00
200
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
7.12 The function fzero locates a zero in a function either near to a user-specified point or within a user-
specified range. If a range is used, then the sign of the function must be different at the beginning and end
of the range. This suggests a strategy for solving this problem—we will search along the function between
0 and 2 π at regular steps, and if the function changes sign between two steps, we will call fzero to
home in on the location of the root.
Note that we can determine whether the function has changed sign between two points by multiplying them
together and seeing the resulting sign. If the sign of the product is negative, then the sign must have
changed between those two points.
% Create function
hndl = @ (x) cos(x).^2 - 0.25;
% Find zeros
zero_index = zero_index + 1;
zero_loc(zero_index) = fzero(hndl,[x(ii) x(ii+1)]);
% Display results
disp(['There is a zero at at ' num2str(zero_loc(zero_index))]);
end
end
% Now plot the function to demonstrate that the zeros are correct
figure(1);
plot(x,y,'b-','LineWidth',2);
201
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
hold on;
plot(zero_loc,zeros(size(zero_loc)),'ro');
hold off;
title('\bfLocation of Function Zeros');
xlabel('\bfx');
ylabel('\bfy');
legend('Function','Zeros found by fzero');
grid on;
When this program is run, the results are as shown below. The plot of the function below shows that the
program has correctly identified the zeros over the specified interval.
>> find_zeros
There is a zero at at 1.0472
There is a zero at at 2.0944
There is a zero at at 4.1888
There is a zero at at 5.236
7.13 A program to evaluate and plot the function f ( x ) = tan 2 x + x − 2 between −2π and 2π in steps of
π /10 is shown below:
% Script file: eval_and_plot_fn.m
%
% Purpose:
% To find the zeros of the function f(x) = (cos (x))^2 - 0.25
% between 0 and 2*PI.
%
% Record of revisions:
202
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
% Date Programmer Description of change
% ==== ========== =====================
% 04/12/15 S. J. Chapman Original code
%
% Define variables:
% hndl -- Function handle
% x -- Input values to examine
% y -- Value of the function at x
When this program is run, the results are as shown below. The plot is shown twice, once at full scale and
once with the maximum y axis value limited to 50, so that the details of the plot can be observed.
>> eval_and_plot_fn
203
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
7.14 A program to find potential targets in a radar’s range/Doppler space is shown below. This program finds
potential targets by looking for points that are higher than all of the neighboring points.
% Calculate histogram
[nvals, amp_levels] = hist(amp(:), 31);
204
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
[max_val, max_loc] = max(nvals);
% Tell user
fprintf('%7.1f %6.1f %6.1f %6.1f\n', ...
range(jj), velocity(ii), amp(ii,jj), snr);
end
end
end
>> find_radar_targets
Range Vel Amp SNR
-161.9 1.7 -100.9 4.1
-89.9 1.7 -98.8 6.1
125.9 1.7 -95.2 9.7
-143.9 2.5 -107.1 -2.2
-54.0 2.5 -96.8 8.1
-125.9 3.3 -106.3 -1.4
-89.9 4.1 -98.0 6.9
0.0 4.1 -72.3 32.6
125.9 4.1 -102.0 2.9
89.9 5.0 -101.9 3.0
143.9 5.0 -101.3 3.6
-125.9 5.8 -98.6 6.3
107.9 5.8 -104.7 0.2
-72.0 6.6 -96.5 8.4
0.0 6.6 -73.2 31.8
143.9 6.6 -101.3 3.6
-107.9 7.5 -100.7 4.2
-54.0 7.5 -97.7 7.2
107.9 7.5 -102.7 2.2
125.9 8.3 -102.3 2.6
0.0 9.1 -73.9 31.0
205
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
72.0 9.1 -100.5 4.5
107.9 9.1 -102.2 2.8
-161.9 10.8 -99.3 5.6
0.0 10.8 -74.3 30.6
-72.0 11.6 -102.0 2.9
89.9 11.6 -99.5 5.4
0.0 12.4 -74.6 30.3
107.9 12.4 -102.4 2.5
143.9 12.4 -99.2 5.7
-125.9 13.3 -98.7 6.2
89.9 14.1 -106.7 -1.8
-72.0 14.9 -99.7 5.2
0.0 14.9 -74.9 30.0
-125.9 15.8 -97.9 7.0
-89.9 15.8 -98.0 6.9
143.9 15.8 -101.8 3.1
-143.9 17.4 -99.2 5.7
125.9 17.4 -97.4 7.5
143.9 18.3 -99.3 5.7
107.9 19.1 -102.9 2.0
-125.9 19.9 -101.3 3.6
0.0 19.9 -75.2 29.7
-72.0 20.8 -100.5 4.4
125.9 20.8 -95.7 9.2
-54.0 21.6 -99.8 5.2
-161.9 22.4 -99.9 5.1
-125.9 22.4 -103.1 1.8
0.0 22.4 -75.4 29.5
-107.9 23.2 -103.2 1.7
107.9 23.2 -103.8 1.1
143.9 23.2 -97.7 7.2
-125.9 24.1 -102.5 2.4
0.0 24.1 -75.4 29.5
-72.0 24.9 -103.0 1.9
89.9 24.9 -111.1 -6.2
-161.9 25.7 -99.1 5.8
-107.9 25.7 -96.7 8.3
-54.0 25.7 -97.7 7.2
0.0 25.7 -75.7 29.2
143.9 25.7 -96.5 8.5
107.9 26.6 -99.8 5.1
-161.9 28.2 -98.4 6.5
-125.9 28.2 -100.1 4.8
-72.0 28.2 -105.1 -0.1
0.0 28.2 -75.5 29.4
72.0 28.2 -95.9 9.0
-89.9 29.0 -104.4 0.5
107.9 29.0 -102.0 2.9
-125.9 29.9 -101.4 3.5
0.0 29.9 -75.1 29.8
-72.0 30.7 -96.7 8.2
-143.9 31.5 -100.6 4.4
0.0 31.5 -75.0 29.9
206
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
-125.9 32.4 -102.7 2.2
125.9 32.4 -103.4 1.6
-143.9 33.2 -102.0 3.0
-107.9 34.0 -105.1 -0.2
-72.0 34.0 -101.7 3.3
89.9 34.0 -103.3 1.6
-125.9 34.9 -103.8 1.1
0.0 34.9 -74.8 30.1
125.9 34.9 -100.0 4.9
107.9 35.7 -98.0 6.9
-161.9 36.5 -99.5 5.4
-89.9 36.5 -103.8 1.2
143.9 37.4 -104.3 0.6
-143.9 38.2 -103.7 1.2
-107.9 38.2 -102.0 2.9
-72.0 38.2 -102.4 2.5
0.0 38.2 -74.1 30.8
-161.9 39.0 -98.7 6.2
-125.9 39.8 -103.1 1.8
-54.0 39.8 -98.2 6.7
125.9 40.7 -100.4 4.6
-125.9 42.3 -101.3 3.7
-89.9 42.3 -96.7 8.3
143.9 42.3 -98.2 6.8
107.9 43.2 -103.2 1.8
143.9 44.0 -98.6 6.4
-125.9 44.8 -95.8 9.1
125.9 45.6 -97.5 7.5
-107.9 46.5 -96.4 8.5
-72.0 46.5 -100.9 4.1
-54.0 47.3 -97.4 7.5
107.9 47.3 -102.8 2.1
-143.9 48.1 -100.1 4.8
-107.9 48.1 -99.2 5.7
-161.9 49.0 -98.6 6.3
-125.9 49.0 -98.5 6.4
125.9 49.0 -100.0 4.9
18.0 49.8 -65.4 39.5
-161.9 52.3 -53.5 51.4
-89.9 52.3 -51.7 53.2
18.0 52.3 -10.0 94.9
107.9 52.3 -50.8 54.1
-143.9 55.6 -98.9 6.0
-72.0 56.4 -105.0 -0.0
-89.9 57.3 -98.4 6.5
107.9 57.3 -99.3 5.6
143.9 57.3 -102.7 2.3
-125.9 58.1 -97.9 7.0
89.9 58.1 -104.3 0.6
125.9 58.9 -101.7 3.2
-161.9 59.8 -100.6 4.3
89.9 60.6 -99.8 5.1
143.9 60.6 -100.5 4.4
207
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
-125.9 61.4 -98.6 6.3
-161.9 62.3 -100.1 4.8
89.9 62.3 -102.6 2.4
143.9 62.3 -100.2 4.8
-89.9 63.1 -101.5 3.4
107.9 63.1 -100.8 4.1
-161.9 63.9 -100.0 4.9
-125.9 63.9 -102.3 2.6
0.0 63.9 -62.6 42.4
89.9 64.7 -97.1 7.8
125.9 64.7 -102.8 2.1
0.0 67.2 -21.0 83.9
107.9 69.7 -99.0 6.0
-143.9 70.5 -100.2 4.7
125.9 70.5 -98.9 6.0
-125.9 71.4 -99.2 5.7
-72.0 71.4 -110.0 -5.1
0.0 71.4 -62.5 42.4
89.9 71.4 -101.5 3.4
-107.9 72.2 -104.7 0.2
143.9 72.2 -105.2 -0.3
-161.9 73.0 -101.3 3.7
-89.9 73.0 -102.9 2.0
125.9 73.0 -97.3 7.6
-125.9 73.9 -101.1 3.8
-72.0 73.9 -101.3 3.7
-161.9 74.7 -102.1 2.8
-107.9 74.7 -100.0 4.9
-143.9 75.5 -102.3 2.6
107.9 75.5 -100.5 4.4
-72.0 76.4 -100.3 4.6
-161.9 77.2 -103.7 1.2
-107.9 77.2 -100.7 4.3
125.9 77.2 -103.8 1.1
-125.9 78.0 -102.0 3.0
143.9 78.0 -103.3 1.6
89.9 78.8 -102.7 2.2
-143.9 79.7 -98.0 6.9
-89.9 79.7 -100.4 4.5
125.9 79.7 -96.3 8.6
-107.9 80.5 -100.1 4.8
-54.0 80.5 -102.5 2.4
-125.9 82.2 -98.6 6.3
-89.9 82.2 -99.2 5.8
107.9 82.2 -100.3 4.6
143.9 82.2 -99.4 5.5
Which target has the highest signal to noise ratio? What is the relative range and velocity of that target?
208
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
7.15 A function to calculate the derivative of a sampled function is shown below:
% Define variables:
% dx -- Step size
% x -- Input values to evaluate fun at
% y -- Function output
% Record of revisions:
% Date Programmer Description of change
% ==== ========== =====================
% 04/12/15 S. J. Chapman Original code
% Calculate sin(x)
dx = 0.05;
x = 0:dx:99*dx;
sinx = sin(x);
% Calculate cos(x)
cosx = cos(x);
210
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
The resulting plot is shown below. The derivative of sin(x) as calculated by the function is indeed very
close to cos(x).
7.16 A program that explores the effect of noise on a derivative is shown below:
% Calculate sin(x)
dx = 0.05;
x = 0:dx:99*dx;
sinx = sin(x);
211
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
% Calculate sin(x) plus noise
noise = 0.04 * random0(1,length(sinx)) - 0.02;
sinxn = sinx + noise;
212
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
The resulting plot is shown below. The noise in the input signal is greatly amplified by the process of
taking the derivative.
7.17 A program that explores the effect of noise on a derivative is shown below:
% Find roots
root_index = root_index + 1;
root_loc(root_index) = fzero(f,[x(ii) x(ii+1)]);
% Display results
disp(['There is a root at at ' num2str(root_loc(root_index))]);
end
end
% Now plot the function to demonstrate that the roots are correct
figure(1);
plot(x,y,'b-','LineWidth',2);
hold on;
plot(root_loc,zeros(size(root_loc)),'ro');
hold off;
title('\bfLocation of Function roots');
xlabel('\bfx');
ylabel('\bfy');
legend('Function','roots found by froot');
grid on;
>> find_roots
There is a root at at 4.6052
214
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
The resulting plot is shown below.
% Define variables:
% n -- Non-negative integer input
% result -- Result of calculation
%
% Record of revisions:
% Date Programmer Description of change
% ==== ========== =====================
% 07/07/14 S. J. Chapman Original code
% 1. 04/13/15 S. J. Chapman CHeck for invalid input
>> fact(5)
ans =
120
>> fact(-4)
??? Error using ==> fact at 22
n must be a non-negative integer
>> fact(5.5)
??? Error using ==> fact at 22
n must be a non-negative integer
7.19 A function to calculate the nth Fibonacci number recursively is shown below.
function fn = fibonacci(n)
%FIBONACCI evaluates the nth Fibonacci number
% Function FIBONACCI evaluates the nth Fibonacci number
% recursively, where n is >= 0.
%
% Define variables:
% fn -- Fibonacci number
% n -- The item in the sequence to calculate
%
% Record of revisions:
% Date Programmer Description of change
% ==== ========== =====================
% 04/13/15 S. J. Chapman Original code
When this program is executed, the results are as shown below. Note that the error checks are working
properly, and the main calculations are also correct.
>> fibonacci(-1)
??? Error using ==> fibonacci at 21
n must be a non-negative integer
>> fibonacci(1)
ans =
1
>> fibonacci(5)
ans =
5
>> fibonacci(10)
ans =
55
7.20 A function that calculates the probability that two or more people from a group of n will have the same
birthday is shown below. This function calculates the probability by taking 100,000 random samples of n
people, and seeing if any two or more have the same birthday. In general, the resulting probabilities should
be accurate to within 1%. (Note: If you have a slow computer, you can reduce the number of trials that the
function is performing to improve execution time.)
% Define variables:
% birthdays -- Array of birthdays
% ii -- Loop index
% jj -- Loop index
% n_matches -- Number of matches that have occurred
% n_trials -- The number of trials to perform
%
% Record of revisions:
% Date Programmer Description of change
% ==== ========== =====================
% 04/13/15 S. J. Chapman Original code
A test program to determine the probability for groups of 2-40 people is shown below:
% Plot probabilities
plot(2:40,prob(2:40),'b-','LineWidth',2);
title('\bfProbability of 2 or more people with same birthday');
xlabel('\bfNumber of people in group');
ylabel('\bfProbability');
grid on;
>> test_birthday
n prob
= ====
2 0.0032
3 0.0082
4 0.0160
5 0.0275
6 0.0410
7 0.0569
8 0.0739
9 0.0937
10 0.1152
11 0.1418
12 0.1666
13 0.1975
14 0.2252
15 0.2527
16 0.2856
17 0.3180
18 0.3482
19 0.3811
20 0.4104
21 0.4462
22 0.4784
23 0.5115
24 0.5377
25 0.5703
26 0.5969
27 0.6267
28 0.6585
29 0.6807
30 0.7066
31 0.7292
219
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
32 0.7524
33 0.7730
34 0.7948
35 0.8148
36 0.8309
37 0.8487
38 0.8630
39 0.8777
40 0.8898
7.21 A program to calculate the false alarm rate as a function of the threshold setting above the mean noise level
is shown below. This program generates 10,000,000 Rayleigh-distributed noise samples2, and determines
the fraction of those samples which exceed each of 9 threshold levels. (Caution: The ten million-sample
array in this program may take a long time to execute on a memory-limited computer. If necessary, you
can cut down the sample size. However at least 100,000 (105) samples will be necessary to approximate a
false alarm rate of 10-4.)
When this program is executed, the results are as shown below. To get a false alarm rate below 10-4, we
would need a threshold of 11 dB.
>> calc_cfar
The false alarm rate versus threshold level is:
Threshold = 5.0 dB Pfa = 8.42700e-002
Threshold = 6.0 dB Pfa = 4.45740e-002
Threshold = 7.0 dB Pfa = 2.00360e-002
Threshold = 8.0 dB Pfa = 7.27600e-003
Threshold = 9.0 dB Pfa = 2.01500e-003
Threshold = 10.0 dB Pfa = 4.27000e-004
Threshold = 11.0 dB Pfa = 5.90000e-005
Threshold = 12.0 dB Pfa = 7.00000e-006
Threshold = 13.0 dB Pfa = 1.00000e-006
221
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
7.22 A function gen_func that generates functions to evaluate polynomials of the form f ( x ) = ax 2 + bx + c ,
where a, b, and c are specified by the user, is shown below. This function creates and returns a handle to a
nested function eval_func, which evaluates the polynomial function for a given x and the values of a,
b, and c specified when the function handle was created.
% Define variables:
% a -- Coefficient of x^2
% b -- Coefficient of x
% c -- Constant coefficient
% hndl -- Handle to function "eval_func"
% msg -- Error message
%
% Record of revisions:
% Date Programmer Description of change
% ==== ========== =====================
% 04/20/15 S. J. Chapman Original code
A function to plot a function, specified by a function handle, between specified limits is shown below:
% Define variables:
% hndl -- Handle to function "eval_func"
% msg -- Error message
% str -- Title string
% x -- X values to plot
222
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
% x1 -- Starting value to plot
% x2 -- Ending value to plot
% y -- Y values to plot
%
% Record of revisions:
% Date Programmer Description of change
% ==== ========== =====================
% 04/20/15 S. J. Chapman Original code
224
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
7.23 To solve this problem, we must first express the differential equation in the form where the derivative of
vout ( t ) is equal to a function. This is Equation (7.15) from the problem statement:
dvout 1 1
=− vout ( t ) + vin ( t ) (7.15)
dt RC RC
We will use function ode45 to solve for the solution to this differential equation. The input voltage as a
function of time is calculated in the following function:
% Define variables:
% t -- Time
% result -- Input voltage
%
% Record of revisions:
% Date Programmer Description of change
% ==== ========== =====================
% 04/20/15 S. J. Chapman Original code
% Calculate derivative
if t >= 0
else
% Before time 0
result = 0;
end
% Define variables:
% t -- Time (in days)
% y -- Vector of states (voltage)
%
% Record of revisions:
% Date Programmer Description of change
% ==== ========== =====================
% 04/20/15 S. J. Chapman Original code
% Calculate derivative
yprime = lambda * vin(t) - lambda * y;
227
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
7.24 To solve this problem, we must first express the differential equation in the form where the derivative of
v( t ) is equal to a function. This equation is
dv 1
=− v ( t ) + vin ( t )
dt RC
% Define variables:
% t -- Time
% result -- Input voltage
%
% Record of revisions:
% Date Programmer Description of change
% ==== ========== =====================
% 04/20/15 S. J. Chapman Original code
% Calculate derivative
if (t >= 0) & (t <= 5)
result = t;
else
result = 0;
end
229
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
7.25 A function to calculate random values from a Rayleigh distribution is shown below:
% Define variables:
% arr1 -- Normally-distributed array
% arr2 -- Normally-distributed array
% res -- Results
% Record of revisions:
% Date Programmer Description of change
% ==== ========== =====================
% 04/23/15 S. J. Chapman Original code
% Calculate data
arr1 = randn(n,m);
arr2 = randn(n,m);
res = sqrt( arr1.^2 + arr2.^2 );
The following program tests random_rayleigh by creating 20,0000 random samples, and checking the
mean and standard deviation of the distribution.
% Tell user
fprintf('Average = %.4f\n',ave);
fprintf('Std Dev = %.4f\n',sd);
% Create histogram
hist(dist,21);
title('\bfHistogram of random values');
xlabel('\bfValue');
ylabel('\bfCount');
>> test_random_rayleigh
Average = 1.2490
Std Dev = 0.6517
231
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
232
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.