1 function G = logpot(b) 2 % logpot accepts one row matrix "b" that contains 3 % all values of "b" for which a dataset should be generated 4 % It returns the "ideal log gain" followed by the generated 5 % datasets for the b values provided. 6 7 % setup pot position values 8 a = repmat(linspace(0,1,50)', size(b), 1); 9 10 % replicate b to match a's height 11 b = repmat(b, 50, 1); 12 13 % do the math 14 G = 1./ ((ones(size(a))-a)./a + (ones(size(a))-a)./b + ones(size(b))); 15 16 % create our "ideal log gain" (0-60dB) 17 glog = logspace(0,60/20,50); 18 glog = glog ./ max(glog); 19 20 % concatenate "ideal log gain" with the generated datasets 21 G = [ glog' G ]; 22 23 24 ---------------------------------------- 25 The command to do the optimisation is 26 27 fminunc(@myfun, 1/6) 28 29 This is the function used in the optimisation to calculate total error. 30 31 function f = myfun(b) 32 % function is 33 % 1 34 %--------------------- 35 % _1-a_ + _1-a_ + 1 36 % b a 37 38 % precalculated to make things faster. 39 % a = linspace(0,1,50); 40 % a1 = 1-a 41 % a2 = (1-a)/a 42 a1 =[ 1.0000 0.9796 0.9592 0.9388 0.9184 0.8980 0.8776 0.8571 0.8367 0.8163 0.7959 0.7755 0.7551 0.7347 0.7143 0.6939 0.6735 0.6531 0.6327 0.6122 0.5918 0.5714 0.5510 0.5306 0.5102 0.4898 0.4694 0.4490 0.4286 0.4082 0.3878 0.3673 0.3469 0.3265 0.3061 0.2857 0.2653 0.2449 0.2245 0.2041 0.1837 0.1633 0.1429 0.1224 0.1020 0.0816 0.0612 0.0408 0.0204 0 ]; 43 a2 =[ Inf 49.0004 24.5004 16.3337 12.2504 9.8004 8.1671 6.9997 6.1248 5.4443 4.8999 4.4545 4.0833 3.7693 3.5001 3.2667 3.0626 2.8825 2.7224 2.5788 2.4499 2.3333 2.2272 2.1304 2.0417 1.9600 1.8846 1.8149 1.7500 1.6897 1.6334 1.5806 1.5312 1.4848 1.4411 1.4000 1.3611 1.3243 1.2895 1.2564 1.2250 1.1952 1.1667 1.1395 1.1136 1.0889 1.0652 1.0425 1.0208 1.0000 ]; 44 45 % glog is a 0-60 db curve normalized to 0-1 46 % glog = logspace(0,60/20,50); glog = glog ./ max(glog) 47 glog =[ 0.0010 0.0012 0.0013 0.0015 0.0018 0.0020 0.0023 0.0027 0.0031 0.0036 0.0041 0.0047 0.0054 0.0063 0.0072 0.0083 0.0095 0.0110 0.0126 0.0146 0.0168 0.0193 0.0222 0.0256 0.0295 0.0339 0.0391 0.0450 0.0518 0.0596 0.0687 0.0791 0.0910 0.1048 0.1207 0.1389 0.1600 0.1842 0.2121 0.2442 0.2812 0.3237 0.3728 0.4292 0.4942 0.5690 0.6551 0.7543 0.8685 1.0000 ]; 48 49 %f = 1./ ( a1./b + a2 ); 50 f1 = 1./ ( a1./b + a2 ) - glog; 51 52 f = sum(abs(f1)); 53 %f = sum(f1.^2); 54 55 56 ---------------------------------------- 57 58 This is the code I used to generate the main results graph 59 60 plot(100*linspace(0,1,50)', 100*logpot([0.0574 0.0709])); 61 xlabel('Wiper Position (%)'); 62 ylabel('Gain (%)'); 63 legend('Real Log 0-60 dB', 'Simulated b=0.0574 (Least ABS)', 'Simulated b=0.0709 (Least Squares)', 'Location', 'NorthWest'); 64 title('Simulated Log Pots'); 65