04 March 2008

Fun with y=mx+b

In the beginning, there were two dots. And I needed to find the line between these two dots, so I could find where a third dot would be if it were on that same line somewhere in between.

But wait. It gets worse. There aren't two dots in the beginning. There are two proto-dots, and first I must run them through some complex equations to find the two dots. (Said equations being deeply and obscurely buried in someone's paper, where they obviously didn't want them found.)

But wait. It gets worse. The line they're on isn't a simple straight line. It's all curvy, so I have to invoke logarithms.

But wait. It gets worse. Because there aren't just two proto-dots. There are several gazillion pairs of these dots, each of which has a separate curvy line to find.

And so I wrote some Matlab code. And lo, I saw that it was good.

    B1=data(:,1);
    B2=data(:,2);

    negsB1=find(B1<=0);
    adj_B1=B1;
    adj_B1(negsB1)=NaN;

    ratio=adj_B1./B2;
    A323=log(.904*(.781*(ratio.^-1.07))-.00714);
    A338=log(.858*(.604*(ratio.^-1.12))-.019);

    slope=(A323-A338)/(323-338);
    intercept=A323-slope*323;
    RESULT=exp(slope*443+intercept);

But wait. My boss doesn't want it in Matlab. He wants it in IDL!

And so I wrote some more code. And lo, it was also good.

    OK = WHERE(B1 GT 0.0)
    SIZEB = SIZE(B1)
    SX = SIZEB(1)
    SY = SIZEB(2)
    A323 = ALOG(0.904*(0.781*[B1/B2]^(-1.07))-0.00714)
    A338 = ALOG(0.858*(0.604*[B1/B2]^(-1.12))-0.019)

    SLOPE=[A323-A338]/(323-338)
    INTERCEPT=A323-(SLOPE*323)
    RESULT = REPLICATE(0.0,SX,SY)
    RESULT(OK) = EXP((SLOPE(OK)*443)+INTERCEPT(OK))

Then when I was done with that, instead of doing any more real work I got carried away writing versions of the same code...

PHP style:

    $data=explode("\n",$input);
    $numrows=count($data);
    $RESULT = array();

    foreach ($data as $row) {
    $columns = explode("\t", $row);
    $B1=$columns[0];
    $B2=$columns[1];

    if ($B1 > 0) {
    $A323 = log(0.904*(0.781*($B1/$B2)^(-1.07))-0.00714);
    $A338 = log(0.858*(0.604*($B1/$B2)^(-1.12))-0.019);
    $slope = ($A323-$A338)/(323-338);
    $intercept = $A323-slope*323;
    $RESULT[] = exp($slope*443+$intercept);
    } #ends if loop

    else {
    $RESULT[] = 'NaN';
    } #ends else

    } #ends for loop

SQL style:

    SELECT ln(0.904*(0.781*(B1/B2)^(-1.07))-0.00714) AS A323,
    ln(0.858*(0.604*(B1/B2)^(-1.12))-0.019) AS A338,
    (A323-A338)/(323-338) AS slope,
    (A323-slope*323) AS intercept,
    exp(slope*443+intercept) AS result
    FROM data_table WHERE B1>0;

(Technically I don't know if these latter two work or not. They may not be so good.)

In the end, I've managed to solve for every variable in y=mx+b (x=(y-b)/m, b=y-mx, m=(y-b)/x, e^y=e^(mx+b), ...) and graphed slopes as if they were dots themselves. And that's about half of what I've been doing for the past month. Now the boss is successfully off to his conference with all the pretty pictures I made for him, and I'm back to wrangling with the last part of the Saga of the Three PHP Scripts.


Disclaimer: This version of events is highly fictionalized. I didn't do all that by myself - there was mucho input from my boss, a colleague, and a random guy in an IRC chatroom. No dots were harmed in the writing of this documentary.

13 comments:

Megadeus said...

o_O

HOLYCRAP.

That is all.

Tania said...

Kewl.

Please don't ask me to write any SAS scripts for you, my brain will seize up.

:genuflects:

John the Scientist said...

Good work.

"(Said equations being deeply and obscurely buried in someone's paper, where they obviously didn't want them found.)"

If this occurs in a Chemistry paper, then said equations are bullshit: eitherl ikely to be wrong or correct equations applied to the wrong problem.

Random Michelle K said...

My brain now hurts. :)

John the Scientist said...

I forgot to add: in Biology a third option appears. Most biologists aer so math unsavvy, their eyes will glaze over and they will rapidly skip the parts wioth equations. This allows the few biologists who are math savvy to get their names on lots of papers because they get called to help apply the math in the new paper that a freshman physics student could have helped them with.

(Ducks clue-by-four)

Nathan said...

I was lost at the title. :D

Random Michelle K said...

John: Ha ha.

Actually, I'm okay with my math ineptitude. It's become a running joke with my family, so at least it's amusing.

Janiece said...

MWT, you are very odd.

And we really like that about you.

Unknown said...

The proto dots seemed to be hanging out in the equation B2=data(:,2);

...> (:,

Anne C. said...

There do seem to be lots of dots there

Tania said...

C'mon guys. Obviously the ...s stand for "then a miracle occurs".

MWT said...

Tania: SAS! I remember that. I was quite good at it ten years ago too, but can't remember a thing now.

John: Umm, hello? Marine biologist here. Doing math. In the same post that you're commenting on. Ahem. :p

I'm not actually sure what field the paper is in. Basically, there are satellites that look at the Earth in a bunch of different wavelengths, and they're trying to figure out how to extract useful info by combining the different wavelengths in different ways. In our case, we're trying to figure out a good way to estimate how much colored dissolved organic matter is in the water. There are three algorithms for it already, and the new paper presents a fourth.

My boss and his colleague stared at the paper for a really long time before splicing together scattered bits of equations from different pages into a couple of big ones, which they then passed over to me. My bit part in it was just to rearrange y=mx+b a lot. And to plot up what the satellites say against what our boat says to see how well they match (this is called "ground truthing").

Brenda: What? No love for B1=data(:,1); ?

Tania (again): there also needs to be a part that says "...profit!" :D

Anonymous said...

While I can fix html bugs, write super basic SQL queries, and scan router configs for problems... the level of logic you're attempting is WAYYY beyond me.

I am not worthy. :)