Optimization of of multivariable code

3 views (last 30 days)
Hi I am trying to find a way to optimize a solution for a code that I wrote. I looked into the optimiztion toolkit and found no ideas of how to apply my code to the toolkit/functions. I was hoping someone may have an idea. I am trying to optimize the output "rsquare_sed" to be greater than lets say 0.8. Thanks.
here is the function:
function [alpha_sediment,alpha_sediment_linear, sediment_attn_Yint,rsquare_sed, SCB]=alpha_sediment(FCB,Dist)
[r,c]=size(FCB); for i=1:r sediment_coeff=regstats(FCB(i,:),Dist(i,:),'linear'); alpha_sediment(i,1)=-0.5.*sediment_coeff.beta(2); alpha_sediment_linear(i,1)=alpha_sediment(i,1)./8.686; sediment_attn_Yint(i,1)=sediment_coeff.beta(1); rsquare_sed(i,1)=sediment_coeff.rsquare; end for i=1:r
SCB(i,:)=FCB(i,:)+(2.*alpha_sediment(i,1).*Dist(i,:));
end
Here are two sample data sets:
FCB=[127.225598231299 125.713889547730 125.746676082287 124.661311292110 122.637987121217 120.995333928251 120.403253861318 119.070856008578 118.855117133774 117.945389702475 116.966063326231 115.928110649265 115.450130174110 115.538961063321 114.980031048106 114.387888131308 113.766275210812 113.118328817657 113.056865540407 112.363867281072 112.261648620817 111.531457656342 111.395314879336 111.244271589667 110.468968912169 109.680690619884 108.880251670799 108.068338646882 107.245511921634 107.024275442900 106.181448961217 105.328784780470 104.466303770959 103.593824995024 102.710915900427 102.436665206241 102.154825926296 101.865518621022 102.191911121141 102.512925820860 102.205715825632 102.516730783841 102.823089197539 102.499869247062 102.797526077198 103.091111672621 102.123119062573 101.775700417693 101.421012346759 101.699919337894 101.975511645450 102.884958247337 103.154291611097 104.054030634696 103.684191141268 103.307920972666 101.626172032329 100.555481783322 100.131819878133 98.9927674787427 98.5232158414117 98.0317343445441 97.5140422082562 96.9645624336199 96.3758773018091 95.7378622710716 95.9737389957589 96.2077478104923 95.5024423450584 95.7328738805740 95.9615916238879 96.1886427274705 95.3938307290227 94.4819885469138 94.7043039571784 94.9251231360403 95.1444846006288 95.3624253996483 96.7146750750571 96.9298801793180 97.1437676681410 97.3563694095762 96.4320222413535 93.7568531886000 93.9657777393199 94.1735331153962 94.3801459822740 94.5856421034331 96.3693214158514 96.5726579270640 96.7749499625218 96.9762200586395 97.1764900302152 97.3757810009140 95.9948383969206 94.1085077207726 94.3049819439802 91.1745542421628 94.6952458783812 94.8890711967385 95.0820482777232 95.2741936412762 95.4655233293854 92.3300518609303 92.5197965027449 92.7087709057022 92.8969893741918 93.0844658177884 93.2712137656492 93.4572463802611 93.6425764705718 93.8272165045392 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN]
Dist=[4.12548150979973 6.00497194706612 7.88446238433251 9.76395282159890 11.6434432588653 13.5229336961317 15.4024241333981 17.2819145706645 19.1614050079309 21.0408954451972 22.9203858824636 24.7998763197300 26.6793667569964 28.5588571942628 30.4383476315292 32.3178380687956 34.1973285060620 36.0768189433284 37.9563093805948 39.8357998178611 41.7152902551275 43.5947806923939 45.4742711296603 47.3537615669267 49.2332520041931 51.1127424414595 52.9922328787259 54.8717233159923 56.7512137532587 58.6307041905250 60.5101946277914 62.3896850650578 64.2691755023242 66.1486659395906 68.0281563768570 69.9076468141234 71.7871372513898 73.6666276886562 75.5461181259226 77.4256085631889 79.3050990004553 81.1845894377217 83.0640798749881 84.9435703122545 86.8230607495209 88.7025511867873 90.5820416240537 92.4615320613201 94.3410224985865 96.2205129358528 98.1000033731192 99.9794938103856 101.858984247652 103.738474684918 105.617965122185 107.497455559451 109.376945996718 111.256436433984 113.135926871250 115.015417308517 116.894907745783 118.774398183050 120.653888620316 122.533379057582 124.412869494849 126.292359932115 128.171850369381 130.051340806648 131.930831243914 133.810321681181 135.689812118447 137.569302555713 139.448792992980 141.328283430246 143.207773867513 145.087264304779 146.966754742045 148.846245179312 150.725735616578 152.605226053845 154.484716491111 156.364206928377 158.243697365644 160.123187802910 162.002678240177 163.882168677443 165.761659114709 167.641149551976 169.520639989242 171.400130426508 173.279620863775 175.159111301041 177.038601738308 178.918092175574 180.797582612840 182.677073050107 184.556563487373 186.436053924640 188.315544361906 190.195034799172 192.074525236439 193.954015673705 195.833506110972 197.712996548238 199.592486985504 201.471977422771 203.351467860037 205.230958297304 207.110448734570 208.989939171836 210.869429609103 212.748920046369 214.628410483635 216.507900920902 218.387391358168 220.266881795435 222.146372232701 224.025862669967 225.905353107234 227.784843544500 229.664333981767 231.543824419033 233.423314856299 235.302805293566 237.182295730832 239.061786168099 240.941276605365 242.820767042631];

Accepted Answer

John D'Errico
John D'Errico on 10 Dec 2014
What do you mean by optimization here, since there is no additional magic to be gained from an optimization? The linear regression you will get from regstats already effectively is an optimization.
From what I've seen, it looks like you are doing one or more simple linear regressions, and you WANT the R^2 coefficient to be greater than 0.8. Nasty data, that does not look very linear either. Merely wanting something to be true is insufficient for that to happen, else I might have gotten that chemistry set for Christmas many years ago.
Sorry, but no optimization technique will make a linear fit to data be better than it is.
  2 Comments
Dan Haught
Dan Haught on 10 Dec 2014
Yes I should have been more clear. I want to optimize the parameter "Dist" to obtain the best start and end points, thus optimizing the linear regression via the metric coefficient of determination. So the input to the code would be various start/end points. Such as this rough attempt:
for loop=1:500
n1a = randi([1,10]);
n2a=randi([40,128]);
[Acoustic.alpha_sediment1(i,1),Acoustic.alpha_sediment_linear1(i,1), Acoustic.sediment_attn_Yint1(i,1),Acoustic.rsquare_sed1(i,1), Acoustic.SCB1(i,n1a:n2a )]=alpha_sediment(Acoustic.FCB1(i,n1a:n2a ),Acoustic.Range(i,n1a:n2a ));
if Acoustic.rsquare_sed1(i,1)>0.85 , break, end
end
John D'Errico
John D'Errico on 10 Dec 2014
You just want to choose a pair of start and end points such that R^2 is greater than 0.8 (or 0.85)? You still need to work on clarity. How do you define "best"?
You can pick any two points, and get a very nice, perfect fit. So just choose a very short interval. So is your goal the longest interval that satisfies your R^2 requirement? If so, then it is possible you would find multiple such intervals to possibly satisfy that goal. Regardless, this is not a true optimization anyway, but a simple combinatoric one. Just test every pair of points to see if the included data fits your criterion.
Anyway, using regstats to do this computation is using a Mack truck to carry a pea to Boston. Polyfit will do the same computation, and do so far more efficiently, since it does not do the rest of the work done by regstats, nothing of which you are using. A quick test on your data looks like it gives a 10-1 reduction in time.
If you really wanted to do this as efficiently as possible, my first thought was to use a nested loop on the start and end points, coupled with qrinsert to compute a regression that is updated at each step. I honestly doubt that qrinsert would be worth the effort though. A better approach is to just use the simple book formulas for the regression coefficients of a linear fit. Find them in wikipedia, for example. They will be written in terms of sums which are trivially written, saving quite a bit more time over even polyfit.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!