File: waterpipes.pcm

package info (click to toggle)
extrema 4.3.6-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 19,212 kB
  • ctags: 6,452
  • sloc: cpp: 86,428; sh: 8,229; makefile: 814
file content (38 lines) | stat: -rw-r--r-- 2,177 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
! Suppose we were given the job of figuring out the minimum amount of water
! pipe necessary to connect up the houses on a street to a water main. We
! have to determine where to dig a straight trench down the street for the
! water main pipe. Suppose that the houses are set back from the street by
! varying distances, and that each house should be about 5 meters from the
! water main.  The problem is to find the trench which would result in the
! minimum usage of water pipe segments from the main to the houses.  We also
! should determine the total length of the pipe we will need.
!
! Suppose the (x,y) coordinates of the connections to the houses are stored
! in a file, HOUSES.DAT.  Remember from plane geometry that the distance from
! point (x,y) to a straight line with slope A and y-intercept B is given by
! D = |A*x+B-y|/sqrt(A^2+1), see the figure (waterpipe-fig.gif).  We want
! to minimize the connecting pipe lengths by varying the free parameters A
! and B.  The following small script does what we want.
!
! Since least squares is used for fitting, we take the square root of the
! expression.  Note that we have more than one independent variable in the
! expression.  Extension to multidimensional cases is straightforward.
! The problem of fitting to a scattered set of points with a straight line
! which is, on the average, a fixed number of units away will, in general,
! have two solutions and which one you get will depend on the starting values
! of the free parameters.
!
!
FILE = 'houses.dat'
LENGTH_GOAL = 5                       ! try to achieve this goal
READ\-MESSAGES FILE X Y               ! read in the house positions
SCALAR\FIT A B                        ! declare the free parameters
A = 1                                 ! initialize
B = 1                                 ! initialize
D[1:LEN(X)] = LENGTH_GOAL             ! 
DISTANCE = 'ABS(A*X+B-Y)/SQRT(A*A+1)' !
FIT\-MESSAGES D=SQRT(DISTANCE)        ! do the fit
FIT\UPDATE PIECES                     ! redo using calculated A and B
STATISTICS PIECES PSUM\SUM            ! the total length of pipe needed
DISPLAY 'Total length of pipe = '//RCHAR(PSUM,'%4.2f')
waterpipesDone = 1