Bilinear interpolation: Difference between revisions

Content added Content deleted
(→‎{{header|J}}: generalization)
Line 105: Line 105:
<lang J>
<lang J>
Note 'FEA'
Note 'FEA'
Here we develop a general method to generate isoparametric interpolants.
The interpolant is the dot product of the four function values with the values at the nodes.

Sum of four linear functions of two variables (xi, eta) is one at each of 4 nodes.
The interpolant is the dot product of the four shape function values evaluated
Let the base element have nodal coordinates (corners) (x,y) with at +/-1.
at the coordinates within the element with the known values at the nodes.
The sum of four shape functions of two variables (xi, eta) is 1 at each of four nodes.
Let the base element have nodal coordinates (xi, eta) of +/-1.




Line 115: Line 118:
| |
| |
| (0,0) |
| (0,0) |
| * | (The usual finite element node order
| * |
| | is counter clockwise. My order to
| |
| | me is more reasonable.)
| |
| |
| |
+---------------+
+---------------+
Line 126: Line 129:
f1( 1,-1) = 1, f1(all other corners) is 0.
f1( 1,-1) = 1, f1(all other corners) is 0.
...
...
The coordinates (xi,eta) are a vector length 2 as argument y
Supply to interpolate the function values at nodes 0 1 2 and 3 as the x argument.
)


Choose a shape function.
f0 =: 1r4 * [: */ 1 1&- NB. f0(xi,eta) = (1-xi)(1-eta)/4
f1 =: _1r4 * [: */ _1 1&- NB. f1(xi,eta) = (1+xi)(1-eta)/4
Use shape functions C0 + C1*xi + C2*eta + C3*xi*eta .
Given (xi,eta) as the vector y form a vector of the
f2 =: _1r4 * [: */ 1 _1&- NB. ...
coefficients of the constants (1, xi, eta, and their product)
f3 =: 1r4 * [: */ _1 _1&-


shape_function =: 1 , {. , {: , */
functions =: f0`f1`f2`f3`:0


CORNERS NB. are the ordered coordinates of the corners
CORNERS =: 21 A. -.+:#:i.4
_1 _1
NB. prove the function values at the corners are correct.
1 _1
assert (=i.4) -: functions"1 CORNERS NB. 4 by 4 identity matrix
_1 1
1 1
(=i.4) NB. rows of the identity matrix are the values of each shape functions at each corner
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
(=i.4x) %. shape_function"1 x: CORNERS NB. Compute the values of the constants as rational numbers.
1r4 1r4 1r4 1r4
_1r4 1r4 _1r4 1r4
_1r4 _1r4 1r4 1r4
1r4 _1r4 _1r4 1r4


This method extends to higher order interpolants having more nodes or to other dimensions.
interpolate =: (+/ .*) functions
)

mp =: +/ .* NB. matrix product

CORNERS =: 21 A.-.+:#:i.4
shape_function =: 1 , {. , {: , */
COEFFICIENTS =: (=i.4) %. shape_function"1 CORNERS
shape_functions =: COEFFICIENTS mp shape_function
interpolate =: mp shape_functions
</lang>
</lang>
<pre>
<pre>
Note. 'demonstrate the interpolant with a saddle'
Note 'demonstrate the interpolant with a saddle'
lower left has value 1,
lower left has value 1,
lower right: 2
lower right: 2
upper left: 2.2
upper left: 2.2
upper right: 0.7
upper right: 0.7
GRID is an array of coordinate pairs (hence rank 3)
ordered so that _1 _1 is in the lower left of the picture,
directed with 1 _1 to lower right.
)
)


Line 158: Line 178:
viewmat SADDLE
viewmat SADDLE
</pre>
</pre>

[[Image:J_bilinear_interpolant.jpg|right]]
[[Image:J_bilinear_interpolant.jpg]]


=={{header|Racket}}==
=={{header|Racket}}==