Chebyshev coefficients: Difference between revisions

Added Algol 68
(Added Algol 68)
Line 95:
0.950 0.5816830895 0.5816830895 -8.98e-14
</pre>
=={{header|ALGOL 68}}==
{{Trans|Java}}... using nested procedures and returning the coefficient array instead of using a reference parameter.
<syntaxhighlight lang="algol68">
BEGIN # Chebyshev Coefficients #
 
PROC chebyshev coef = ( PROC( REAL )REAL func, REAL min, max, INT n )[]REAL:
BEGIN
 
PROC map = ( REAL x, min x, max x, min to, max to )REAL:
( x - min x ) / ( max x - min x ) * ( max to - min to ) + min to;
 
[ 0 : n - 1 ]REAL coef; FOR i FROM LWB coef TO UPB coef DO coef[ i ] := 0 OD;
FOR i FROM 0 TO UPB coef DO
REAL m = map( cos( pi * ( i + 0.5 ) / n ), -1, 1, min, max );
REAL f = func( m ) * 2 / n;
FOR j FROM 0 TO UPB coef DO
coef[ j ] +:= f * cos( pi * j * ( i + 0.5 ) / n )
OD
OD;
coef
END # chebyshev coef # ;
 
BEGIN
INT n = 10;
REAL min := 0, max := 1;
[]REAL c = chebyshev coef( cos, min, max, n );
print( ( "Coefficients:", newline ) );
FOR i FROM LWB c TO UPB c DO
print( ( fixed( c[ i ], -18, 14 ), newline ) )
OD
END
 
END
</syntaxhighlight>
{{out}}
<pre>
Coefficients:
1.64716947539031
-0.23229937161517
-0.05371511462205
0.00245823526698
0.00028211905743
-0.00000772222916
-0.00000058985565
0.00000001152143
0.00000000065963
-0.00000000001002
</pre>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
Line 223 ⟶ 272:
8 : 6.5963e-10
9 : -1.0022e-11</pre>
 
=={{header|C}}==
C99.
3,038

edits