1 / 12

How to debug

How to debug. You need to do this for the homework You need to do this for your career When we ask students what they wish there ESCI degree had given them, it was this & programming (the same thing really!) You want to be a maker, not a user, of tools. The golden rules

ghazi
Télécharger la présentation

How to debug

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. How to debug You need to do this for the homework You need to do this for your career When we ask students what they wish there ESCI degree had given them, it was this & programming (the same thing really!) You want to be a maker, not a user, of tools

  2. The golden rules • Outline your plans on paper first. • And make your plans from the top down! (example from this weeks homework; why is lag_corr.m a function?) • If you are looking at the screen and thinking “I don’t know what to do first,” you failed to do this. • The ideas you need are not the same as the computer commands you need! Do not confuse the two! • Not as easy as it seems • More important than it seems.

  3. The golden rules; part the deux • Keep things as simple as you can • Keep things as separate as you can! if (nlag>0)   x=xin(1:end-nlag);   y=yin(1+nlag:end);   index=find(isfinite(x) & isfinite(y));   index_x=find(isfinite(x));   index_y=find(isfinite(y));   jnk=corrcoef(index_x,index_y);   corr=jnk(1,2);else (nlag<0)  x=xin(1-nlag:end);  y=yin(1:end+nlag);  index=find(isfinite(x) & isfinite(y));  index_x=find(isfinite(x));  index_y=find(isfinite(y));  jnk=corrcoef(index_x,index_y);  corr=jnk(1,2)end

  4. The golden rules; part the IIIIIIIX; build from the bottom up. • Build the little foundation things first, and then test them. • lag_corr.m before compute_lagged_corr.m • Test on simple data you understand! • Debug backwards from the following • Examples on following page!

  5. I can’t get the shift right  function lagcorr=lag_corr(xin,yin,nlag); % function corr=lag_corr(xin,yin,nlag); % % this function takes two vectors of equal length, xin and yin, and % computes the correlation between xin and yin lagged by nlag, % where nlag is amount of the lag in indices. % % if nlag>0, the lag is computed so that xin is EARLIER in time than yin % % Thus if xin=[1,2,3,4,5] and yin=[6,7,8,9,10] and nlag=2, this code % would correlate [1,2,3] with [8,9,10]. % % if nlag<0, the lag is computed so that xin is LATER in time than yin % % Thus if xin=[1,2,3,4,5] and yin=[6,7,8,9,10] and nlag=-2, this code % would correlate [3,4,5] with [6,7,8]. % % The correlation is returned in lagcorr. % % xin and yin may contain NaN's, and this code will still return a correlation. % if (nlag>0) x=xin(1:end-nlag);   y=yin(nlag:end); ... How do I test this???? How do I start to think about it? Start with simple tests! On paper!

  6. I get an error: “nlag does not exist”  function lagcorr=lag_corr(xin,yin,nlag); % function corr=lag_corr(xin,yin,nlag); % % this function takes two vectors of equal length, xin and yin, and % computes the correlation between xin and yin lagged by nlag, % where nlag is amount of the lag in indices. % % if nlag>0, the lag is computed so that xin is EARLIER in time than yin % % Thus if xin=[1,2,3,4,5] and yin=[6,7,8,9,10] and nlag=2, this code % would correlate [1,2,3] with [8,9,10]. % % if nlag<0, the lag is computed so that xin is LATER in time than yin % % Thus if xin=[1,2,3,4,5] and yin=[6,7,8,9,10] and nlag=-2, this code % would correlate [3,4,5] with [6,7,8]. % % The correlation is returned in lagcorr. % % xin and yin may contain NaN's, and this code will still return a correlation. % if (nlag>0) x=xin(1:end-nlag);   y=yin(nlag:end); ... where does nlag come from?

  7. I got the question: • I have talked to XXX and he has helped me through a little of it ( the shifting parts) and he also said that you wanted an index that calculated all the numbers that arent NaN's together, which is why i have index=find(isfinite(x) & isfinite(y)). However, I feel like this is redundant since i have an index_x and an index_y following it. I only have these two separately because I put them into the corrcoef function...but can i just do jnk=corrcoef(index) and it will take care of both of them?! I'm not sure. When i run it as is, the graphs don't show up so I know I am not getting something... • [name] had not thought out what s/he wanted to do before starting to write! • [name] was trying to fix everything at once; this can’t be done! What do do next?

  8. I said • “You are not thinking this through clearly.  Please go through each line above, and given what you think will be in them when you start with the inputsnlag=2xin=[1,2,3,4,5,nan,7,8,9]yin=[10,11,nan,13,14,15,16,17,18,19]Either you will see where you are confused, or I will see where you are confused.” • Why did I say this?

  9. And [mystery student] wrote back: “This is what i came up with....x=xin(1:end-nlag); %x=[1,2,3,4,5,nan,7]   y=yin(1+nlag:end); %y=[nan,13,14,15,16,17,18,19]   index=find(isfinite(x) & isfinite(y)); %x=[1,2,3,4,5,7]  %y=[13,14,15,16,17,18,19]   index_x=find(isfinite(x)); %x=[1,2,3,4,5,7]    index_y=find(isfinite(y)); %y=[13,14,15,16,17,18,19]   jnk=corrcoef(index_x,index_y);    corr=jnk(1,2); I can see that the lengths of vectors are different, which i feel like i have to adjust to get them to correlate correctly? Am i shifting the numbers OK? What should [anon] have done at this point? verify. Do it now on board

  10. Lets try all these steps on the following code. I gave the student the advice that they should run their function on the following simple casenlag=0;xin=[1,2,nan,nan,5,6,7];yin=[1,nan,3,4,5,6,7];lag_corr(xin,yin,0) if (nlag>=0)    xin(1:end-nlag);    yin(1+nlag:end);    xin_real=find(isfinite(xin));    yin_real=find(isfinite(yin));    lagcorr=corrcoef(xin_real,yin_real);    lag_corr=lagcorr(1,2)else (nlag<=0)    xin(1-nlag:end);    yin(1:end+nlag);    xin_real=find(isfinite(xin));    yin_real=find(isfinite(yin));    lagcorr=corrcoef(xin_real,yin_real);    lag_corr=lagcorr(1,2)end

  11. Debugging • is hard • starts before you start writing code • is part science part art • where you will spend much (most) of your time • requires methodical but creative thinking! • Randomly changing code (almost) always fails!

More Related