0001 function [y,y_th,py_th] = randlin_yp(N,h,ybounds,display_mode)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024 if nargin < 1,
0025 N = 100000;
0026 h = 1;
0027 ybounds = [0,1];
0028 display_mode = 0;
0029 elseif nargin < 2,
0030 h = 1;
0031 ybounds = [0,1];
0032 display_mode = 0;
0033 elseif nargin < 3,
0034 ybounds = [0,1];
0035 display_mode = 0;
0036 elseif nargin < 4,
0037 display_mode = 0;
0038 end
0039
0040 if isnan(ybounds) | isempty(ybounds),
0041 ybounds = [0,1];
0042 end
0043
0044 if h == 1,
0045 x = rand(N,1);
0046 elseif h == 0,
0047 x1 = rand(N,1);
0048 x2 = rand(N,1);
0049
0050 x = abs(x2-x1);
0051 else,
0052
0053 x = NaN(N,1);
0054
0055 while isnan(sum(x)),
0056 x1 = rand(sum(isnan(x)),1);
0057 x2 = rand(sum(isnan(x)),1);
0058 x0 = NaN(sum(isnan(x)),1);
0059
0060 mask0 = x2 < (1 - (1-h)*x1);
0061
0062 x0(mask0) = x1(mask0);
0063
0064 inan = find(isnan(x) == 1);
0065 x(inan) = x0;
0066 end
0067 end
0068
0069 if display_mode < 10;
0070 display_mode = 10;
0071 warning('display_mode must exceed 10 for a correct histogramm.');
0072 end
0073
0074 y = (ybounds(2) - ybounds(1))*x + ybounds(1);
0075
0076 y_th = sort((ybounds(2) - ybounds(1))*linspace(0,1,N) + ybounds(1));
0077 py_th = (1 - (1 - h)*(y_th - ybounds(1))/(ybounds(2) - ybounds(1)))/abs((ybounds(2) - ybounds(1)))*2/(h+1);
0078 norm_th = trapz(y_th,py_th);
0079
0080 if display_mode,
0081 if N/display_mode > display_mode,
0082 [h1,y1] = hist(y,display_mode);
0083 py = h1*display_mode/N/(max(y1) - min(y1));
0084 norm = trapz(y1,py);
0085 close all
0086 figure(1),plot(y1,py,'b',y_th,py_th,'r')
0087 axis([min(ybounds(1),ybounds(2)),max(ybounds(1),ybounds(2)),min(py_th),max(py_th)])
0088 xlabel('y');ylabel('f(y)');
0089 if ybounds(2) > ybounds(1),
0090 title(['Linear distribution function between ',num2str(ybounds(1)),' and ',num2str(ybounds(2)),' -> f(',num2str(ybounds(1)),')/f(',num2str(ybounds(2)),') = ',num2str(1/h)])
0091 legend(['Numerical, norm. = ',num2str(norm)],['Theoretical, norm. = ',num2str(norm_th)],'Location','SouthWest')
0092 else
0093 title(['Linear distribution function between ',num2str(ybounds(1)),' and ',num2str(ybounds(2)),' -> f(',num2str(ybounds(1)),')/f(',num2str(ybounds(2)),') = ',num2str(h)])
0094 legend(['Numerical, norm. = ',num2str(norm)],['Theoretical, norm. = ',num2str(norm_th)],'Location','NorthWest')
0095 end
0096 else
0097 warning('Not enough points for histogram display -> set display_mode to ');
0098 end
0099 end