08-02-14.mw

 > data:=[[1,2], [2,2], [5,6], [7,9], [8,10]];

 (1)

 >

 > plot(data,style=point);

How far away is y=x+1.2 from this data?   need some idea of distance(line, data)

bad idea: sum of differences in y values

 > f:=x->x+1.2;

 (2)

 > f(3);

 (3)

 > g:=x+1.2;

 (4)

 > g(3);

 (5)

can reference elements in a list using []

 > data;

 (6)

 > data[3];

 (7)

 > data[3][1];

 (8)

 > data[3][2];

 (9)

 > f(data[3][1]);

 (10)

 > f(data[3][1])-data[3][2];

 (11)

 > seq( f(data[i][1])-data[i][2], i=1..5);

 (12)

 > sum( f(data[i][1])-data[i][2], i=1..5);

 (13)

 > h:=x->0.9*x+1.2;

 (14)

 > sum( h(data[i][1])-data[i][2], i=1..5); seq( h(data[i][1])-data[i][2], i=1..5);

 (15)

 >

Bad, gives 0 when fit isn't perfect.

next try, consider sum(absvalue)

 > sum( abs(h(data[i][1])-data[i][2]), i=1..5);

 (16)

 > sum( abs(f(data[i][1])-data[i][2]), i=1..5);

 (17)

 >

want to write a function that gives me abs difference between  f and a point.  This is  |f(p_x) - p_y|

 > err := (f, p) -> abs( f(p[1]) - p[2]);

 (18)

 > err( h, [5,6]);

 (19)

 > err( h, data[2]);

 (20)

Note that garbage in gives a garbage answer.

 > err(3,7);

 (21)

now distance is the sum of the errors

 > nops(data);

 (22)

 > nops( {1, 2,3,4,5,6,7});

 (23)

 > nops(f);

 (24)

 > sum( abs(h(data[i][1])-data[i][2]), i=1..nops(data));

 (25)

 > sum( err(h,data[i]), i=1..nops(data));

 (26)

 > dist:= h-> sum( err(h,data[i]), i=1..nops(data));

 (27)

 > dist(h);

 (28)

 > dist(f);

 (29)

 > dist(exp);evalf(%);

 (30)

 > dist(x->3*x+2);

 (31)

 >

 >

Try distance for various lines of slope 1, varying intercept.

You should think about how to do this, and we'll go over it next week.