Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is being done in here ? (Used math recognition)

Tags:

math

fortran

I know this isn't exactly programming related per se, but programmers are the most probable of all people who will recognize this maybe.

I have the following (X and Y are arrays, both with 3 elements), and I cannot recognize (although it reminds me of a few things, but none quite!) what is being done here. Does it ring any bells for anyone else ?

I gather you can disregard the lower part; the upper should probably give it away ... but I still cannot see it.

At first it reminded me of linear interpolation in 3d space ...

  SUBROUTINE TRII(X,Y,XR,YR)
DIMENSION X(3),Y(3)

D=X(1)*(X(2)**2-X(3)**2)+
 >    X(2)*(X(3)**2-X(1)**2)+
 >    X(3)*(X(1)**2-X(2)**2)

D1=Y(1)*(X(2)*X(3)**2-X(3)*X(2)**2)+
 >     Y(2)*(X(3)*X(1)**2-X(1)*X(3)**2)+
 >     Y(3)*(X(1)*X(2)**2-X(2)*X(1)**2)

D2=Y(1)*(X(2)**2-X(3)**2)+
 >     Y(2)*(X(3)**2-X(1)**2)+
 >     Y(3)*(X(1)**2-X(2)**2)

D3=X(2)*(Y(3)-Y(1))+
 >     X(1)*(Y(2)-Y(3))+
 >     X(3)*(Y(1)-Y(2))

A=D1/D
B=D2/D
C=D3/D

YR=A+B*XR+C*XR**2

RETURN
END

  SUBROUTINE TRIM(X,Y,XR,YR,XM,YM)
DIMENSION X(3),Y(3)

D=X(1)*(X(2)**2-X(3)**2)+
 >    X(2)*(X(3)**2-X(1)**2)+
 >    X(3)*(X(1)**2-X(2)**2)

D1=Y(1)*(X(2)*X(3)**2-X(3)*X(2)**2)+
 >     Y(2)*(X(3)*X(1)**2-X(1)*X(3)**2)+
 >     Y(3)*(X(1)*X(2)**2-X(2)*X(1)**2)

D2=Y(1)*(X(2)**2-X(3)**2)+
 >     Y(2)*(X(3)**2-X(1)**2)+
 >     Y(3)*(X(1)**2-X(2)**2)

D3=X(2)*(Y(3)-Y(1))+
 >     X(1)*(Y(2)-Y(3))+
 >     X(3)*(Y(1)-Y(2))

A=D1/D
B=D2/D
C=D3/D

XR=-B/(2.*C)
YR=A+B*XR+C*XR**2

XM=XR
IF(XR.GT.X(1).OR.XR.LT.X(3))XM=X(1)
YM=A+B*XM+C*XM**2
IF(YM.LT.Y(1))XM=X(1)
IF(YM.LT.Y(1))YM=Y(1)

RETURN
END

">" is a continuation sign.

like image 387
Rook Avatar asked Nov 27 '22 23:11

Rook


2 Answers

The code run as follows

Routine TRII takes as input the coordinates of three points (x,y) and interpolates a parabola using Lagrange interpolation. Also takes as input the coordinate XR. Returns in YR the value at XR for the interpolating parabola. I guess the name of the routine comes from "TRI" (Croatian for "three" (points)) and "I" for Interpolation.

Routine TRIM also calculates the same parabola, and returns the minimun value of the function in the interval {X(1),X(3)}.The name comes from "TRI" and "M" (minimum)

(I "really" executed the program) >)

Note that this is FORTRAN code and the parameters are passed by reference, so the results are returned back in the same parameters (very odd!)

Edit

Just for fun, let's run TRII

TRII[X_, Y_, XR_] := 
  Module[{D0, D1, D2, D3, A, B, C}, 
     D0 = X[[1]]*(X[[2]]^2 - X[[3]]^2) + 
          X[[2]]*(X[[3]]^2 - X[[1]]^2) + 
          X[[3]]*(X[[1]]^2 - X[[2]]^2);
     D1 = Y[[1]]*(X[[2]]*X[[3]]^2 - X[[3]]*X[[2]]^2) + 
          Y[[2]]*(X[[3]]*X[[1]]^2 - X[[1]]*X[[3]]^2) + 
          Y[[3]]*(X[[1]]*X[[2]]^2 - X[[2]]*X[[1]]^2);
     D2 = Y[[1]]*(X[[2]]^2 - X[[3]]^2) + 
          Y[[2]]*(X[[3]]^2 - X[[1]]^2) + 
          Y[[3]]*(X[[1]]^2 - X[[2]]^2);
     D3 = X[[2]]*(Y[[3]] - Y[[1]]) + 
          X[[1]]*(Y[[2]] - Y[[3]]) + 
          X[[3]]*(Y[[1]] - Y[[2]]);
   A = D1/D0;
   B = D2/D0;
   C = D3/D0;
   Return[A + B*XR + C*XR^2];];

X = RandomReal[1, 3];
Y = RandomReal[1, 3];
Show[Plot[TRII[X, Y, x], {x, 0, 1}], 
 ListPlot[Transpose[{X, Y}], PlotMarkers -> Automatic]]

enter image description here

like image 100
Dr. belisarius Avatar answered Dec 09 '22 09:12

Dr. belisarius


D is the determinant of the matrix:

        | x(1) x(1)² 1 |
D = det | x(2) x(2)² 1 |
        | x(3) x(3)² 1 |

In D1, the rightmost column has been replaced with Y:

         | x(1) x(1)² Y(1) |
D1 = det | x(2) x(2)² Y(2) |
         | x(3) x(3)² Y(3) |

In D2, and D3 it's the first and second columns, respectively. Is it easier to recognize now? Looks a lot like using Cramer's rule to solve a linear equation to me.

Edit: To be more precise: (A, B, C) is the solution to the system:

A + x(1)*B + x(1)²*C = Y(1)
A + x(2)*B + x(2)²*C = Y(2)
A + x(3)*B + x(3)²*C = Y(3)

YR is the square of the solution to the quadratic equation (nb, different x!):

C*x² + B*x + A = 0

I feel like this should be obvious now, but I can't quite grasp it...

like image 20
waxwing Avatar answered Dec 09 '22 11:12

waxwing