08-02-14.mw

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

[[1, 2], [2, 2], [5, 6], [7, 9], [8, 10]] (1)
 

 

>
 

> plot(data,style=point);
 

Plot_2d
 

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;
 

proc (x) options operator, arrow; `+`(x, 1.2) end proc (2)
 

> f(3);
 

4.2 (3)
 

> g:=x+1.2;
 

`+`(x, 1.2) (4)
 

> g(3);
 

`+`(x(3), 1.2) (5)
 

can reference elements in a list using [] 

> data;
 

[[1, 2], [2, 2], [5, 6], [7, 9], [8, 10]] (6)
 

> data[3];
 

[5, 6] (7)
 

> data[3][1];
 

5 (8)
 

> data[3][2];
 

6 (9)
 

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

6.2 (10)
 

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

.2 (11)
 

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

.2, 1.2, .2, -.8, -.8 (12)
 

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

0. (13)
 

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

proc (x) options operator, arrow; `+`(`*`(.9, `*`(x)), 1.2) end proc (14)
 

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

 

-2.300000000
.1, 1.0, -.3, -1.5, -1.6 (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);
 

4.500000000 (16)
 

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

3.200000000 (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]);
 

proc (f, p) options operator, arrow; abs(`+`(f(p[1]), `-`(p[2]))) end proc (18)
 

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

.3 (19)
 

> err( h, data[2]);
 

1.0 (20)
 

Note that garbage in gives a garbage answer. 

> err(3,7);
 

abs(`+`(7[2], `-`(3))) (21)
 

now distance is the sum of the errors 

 

> nops(data);
 

5 (22)
 

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

7 (23)
 

> nops(f);
 

1 (24)
 

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

4.500000000 (25)
 

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

4.500000000 (26)
 

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

proc (h) options operator, arrow; sum(err(h, data[i]), i = 1 .. nops(data)) end proc (27)
 

> dist(h);
 

4.500000000 (28)
 

> dist(f);
 

3.200000000 (29)
 

> dist(exp);evalf(%);
 

 

`+`(exp(1), `-`(29), exp(2), exp(5), exp(7), exp(8))
4207.111642 (30)
 

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

50 (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.