Chebyshev coefficients: Difference between revisions

no edit summary
(Added Algol W)
No edit summary
 
(7 intermediate revisions by 7 users not shown)
Line 95:
0.950 0.5816830895 0.5816830895 -8.98e-14
</pre>
=={{header|ALGOL 60}}==
{{works with|GNU Marst|Any - tested with release 2.7}}
{{Trans|ALGOL W}}...which is{{Trans|Java}}
<syntaxhighlight lang="algol60">
begin comment Chebyshev coefficients ;
 
real PI;
 
procedure chebyshevCoef( func, min, max, coef, N )
; value min, max, N
; real procedure func
; real min, max
; real array coef
; integer N
;
begin
real procedure map( x, min x, max x, min to, max to )
; value x, min x, max x, min to, max to
; real x, min x, max x, min to, max to
;
begin
map := ( x - min x ) / ( max x - min x ) * ( max to - min to ) + min to
end map ;
 
integer i, j;
for i := 0 step 1 until N - 1 do begin
real m, f;
m := map( cos( PI * ( i + 0.5 ) / N ), -1, 1, min, max );
f := func( m ) * 2 / N;
for j := 0 step 1 until N - 1 do begin
coef[ j ] := coef[ j ] + f * cos( PI * j * ( i + 0.5 ) / N )
end j
end i
end chebyshevCoef ;
 
PI := arctan( 1 ) * 4;
begin
integer N;
N := 10;
begin
real array c [ 0 : N - 1 ];
integer i;
chebyshevCoef( cos, 0, 1, c, N );
outstring( 1, "Coefficients:\n" );
for i := 0 step 1 until N - 1 do begin
if c[ i ] >= 0 then outstring( 1, " " );
outstring( 1, " " );outreal( 1, c[ i ] );outstring( 1, "\n" )
end i
end
end
end
</syntaxhighlight>
{{out}}
<pre>
Coefficients:
1.64716947539
-0.232299371615
-0.053715114622
0.00245823526698
0.000282119057434
-7.72222915635e-006
-5.89855645675e-007
1.15214277563e-008
6.59630183808e-010
-1.00219138544e-011
</pre>
 
=={{header|ALGOL 68}}==
{{Trans|Java}}... using nested procedures and returning the coefficient array instead of using a reference parameter.
Line 196 ⟶ 263:
 
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
The [[#MSX-BASIC|MSX-BASIC]] solution works without any changes.
 
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
Line 225 ⟶ 295:
6 : -5.89855645106e-07
7 : 1.15214275009e-08</pre>
 
==={{header|Chipmunk Basic}}===
{{trans|FreeBASIC}}
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="qbasic">100 cls
110 rem pi = 4 * atn(1)
120 a = 0
130 b = 1
140 n = 10
150 dim cheby(n)
160 dim coef(n)
170 for i = 0 to n-1
180 coef(i) = cos(cos(pi/n*(i+1/2))*(b-a)/2+(b+a)/2)
190 next i
200 for i = 0 to n-1
210 w = 0
220 for j = 0 to n-1
230 w = w+coef(j)*cos(pi/n*i*(j+1/2))
240 next j
250 cheby(i) = w*2/n
260 print i;" : ";cheby(i)
270 next i
280 end</syntaxhighlight>
{{out}}
<pre>0 : 1.647169
1 : -0.232299
2 : -0.053715
3 : 2.458235E-03
4 : 2.821191E-04
5 : -7.722229E-06
6 : -5.898556E-07
7 : 1.152143E-08
8 : 6.596304E-10
9 : -1.002234E-11</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">Const pi As Double = 4 * Atn(1)
Dim As Integer i, j
Dim As Double w, a = 0, b = 1, n = 10
Dim As Double cheby(n), coef(n)
 
For i = 0 To n-1
coef(i) = Cos(Cos(pi/n*(i+1/2))*(b-a)/2+(b+a)/2)
Next i
 
For i = 0 To n-1
w = 0
For j = 0 To n-1
w += coef(j) * Cos(pi/n*i*(j+1/2))
Next j
cheby(i) = w*2/n
Print i; " : "; cheby(i)
Next i
Sleep</syntaxhighlight>
{{out}}
<pre> 0 : 1.647169475390314
1 : -0.2322993716151719
2 : -0.05371511462204768
3 : 0.002458235266981634
4 : 0.0002821190574339161
5 : -7.7222291556156e-006
6 : -5.898556451056081e-007
7 : 1.152142750093788e-008
8 : 6.596299062522348e-010
9 : -1.002201654998203e-011</pre>
 
==={{header|Gambas}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">Public coef[10] As Float
 
Public Sub Main()
Dim i As Integer, j As Integer
Dim w As Float, a As Float = 0, b As Float = 1, n As Float = 10
For i = 0 To n - 1
coef[i] = Cos(Cos(Pi / n * (i + 1 / 2)) * (b - a) / 2 + (b + a) / 2)
Next
For i = 0 To n - 1
w = 0
For j = 0 To n - 1
w += coef[j] * Cos(Pi / n * i * (j + 1 / 2))
Next
cheby[i] = w * 2 / n
Print i; " : "; cheby[i]
Next
End</syntaxhighlight>
{{out}}
<pre>0 : 1,64716947539031
1 : -0,232299371615172
2 : -0,053715114622048
3 : 0,002458235266982
4 : 0,000282119057434
5 : -7,7222291556156E-6
6 : -5,89855645105608E-7
7 : 1,15214275009379E-8
8 : 6,59629906252235E-10
9 : -1,0022016549982E-11</pre>
 
==={{header|GW-BASIC}}===
{{trans|FreeBASIC}}
{{works with|PC-BASIC|any}}
<syntaxhighlight lang="qbasic">100 CLS
110 PI# = 4 * ATN(1)
120 A# = 0
130 B# = 1
140 N# = 10
150 DIM CHEBY(N#)
160 DIM COEF(N#)
170 FOR I = 0 TO N#-1
180 COEF(I) = COS(COS(PI#/N#*(I+1/2))*(B#-A#)/2+(B#+A#)/2)
190 NEXT I
200 FOR I = 0 TO N#-1
210 W# = 0
220 FOR J = 0 TO N#-1
230 W# = W# + COEF(J) * COS(PI#/N#*I*(J+1/2))
240 NEXT J
250 CHEBY(I) = W# * 2 / N#
260 PRINT I; " : "; CHEBY(I)
270 NEXT I
280 END</syntaxhighlight>
{{out}}
<pre>0 : 1.647169
1 : -.2322993
2 : -5.371515E-02
3 : 2.458321E-03
4 : 2.820671E-04
5 : -7.766486E-06
6 : -5.857175E-07
7 : 9.834766E-08
8 : -1.788139E-07
9 : -9.089708E-08</pre>
 
==={{header|Minimal BASIC}}===
{{trans|FreeBASIC}}
{{works with|BASICA}}
<syntaxhighlight lang="qbasic">110 LET P = 4 * ATN(1)
120 LET A = 0
130 LET B = 1
140 LET N = 10
170 FOR I = 0 TO N-1
180 LET K(I) = COS(COS(P/N*(I+1/2))*(B-A)/2+(B+A)/2)
190 NEXT I
200 FOR I = 0 TO N-1
210 LET W = 0
220 FOR J = 0 TO N-1
230 LET W = W + K(J) * COS(P/N*I*(J+1/2))
240 NEXT J
250 LET C(I) = W * 2 / N
260 PRINT I; " : "; C(I)
270 NEXT I
280 END</syntaxhighlight>
{{out}}
<pre> 0 : 1.6471695
1 : -.23229937
2 : -5.3715115E-2
3 : 2.4582353E-3
4 : 2.8211906E-4
5 : -7.7222291E-6
6 : -5.8985565E-7
7 : 1.1521437E-8
8 : 6.5962449E-10
9 : -1.0018986E-11</pre>
 
==={{header|MSX Basic}}===
{{trans|FreeBASIC}}
{{works with|MSX BASIC|any}}
<syntaxhighlight lang="qbasic">100 CLS : rem 10 HOME for Applesoft BASIC
110 PI = 4 * ATN(1)
120 A = 0
130 B = 1
140 N = 10
150 DIM CHEBY(N)
160 DIM COEF(N)
170 FOR I = 0 TO N-1
180 COEF(I) = COS(COS(PI/N*(I+1/2))*(B-A)/2+(B+A)/2)
190 NEXT I
200 FOR I = 0 TO N-1
210 W = 0
220 FOR J = 0 TO N-1
230 W = W + COEF(J) * COS(PI/N*I*(J+1/2))
240 NEXT J
250 CHEBY(I) = W * 2 / N
260 PRINT I; " : "; CHEBY(I)
270 NEXT I
280 END</syntaxhighlight>
 
==={{header|QBasic}}===
Line 249 ⟶ 506:
END</syntaxhighlight>
{{out}}
<pre> 0 : 1.647169470787048000000
<pre>
0 : 1.647169470787048000000
1 : -0.232299402356147800000
2 : -0.053715050220489500000
Line 259 ⟶ 515:
7 : 0.000000053614126471757
8 : 0.000000079823998078155
9 : -0.000000070922546058227</pre>
</pre>
 
==={{header|FreeBASICQuite BASIC}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="freebasic">Const pi As Double = 4 * Atn(1)
<syntaxhighlight lang="qbasic">100 cls
Dim As Double i, w, j
Dim110 Asrem Double api = 0,4 b =* atn(1, n = 10)
120 let a = 0
Dim As Double cheby(10), coef(10)
130 let b = 1
140 let n = 10
150 array c
160 array k
170 for i = 0 to n-1
180 let k[i] = cos(cos(pi/n*(i+1/2))*(b-a)/2+(b+a)/2)
190 next i
200 for i = 0 to n-1
210 let w = 0
220 for j = 0 to n-1
230 let w = w + k[j] * cos(pi/n*i*(j+1/2))
240 next j
250 let c[i] = w * 2 / n
260 print i; " : "; c[i]
270 next i
280 end</syntaxhighlight>
{{out}}
<pre>0 : 1.6471694753903137
1 : -0.23229937161517186
2 : -0.05371511462204768
3 : 0.0024582352669816343
4 : 0.0002821190574339161
5 : -0.0000077222291556156
6 : -5.898556451056081e-7
7 : 1.1521427500937876e-8
8 : 6.59629917354465e-10
9 : -1.0022016549982027e-11</pre>
 
==={{header|Run BASIC}}===
For i = 0 To n-1
{{trans|FreeBASIC}}
coef(i) = Cos(Cos(pi/n*(i+1/2))*(b-a)/2+(b+a)/2)
{{works with|Just BASIC}}
Next i
{{works with|Liberty BASIC}}
 
<syntaxhighlight lang="vb">pi = 4 * atn(1)
For i = 0 To n-1
a = 0
b = 1
n = 10
dim cheby(n)
dim coef(n)
for i = 0 to n-1
coef(i) = cos(cos(pi/n*(i+1/2))*(b-a)/2+(b+a)/2)
next i
for i = 0 to n-1
w = 0
Forfor j = 0 Toto n-1
w += w + coef(j) * Coscos(pi/n*i*(j+1/2))
Nextnext j
cheby(i) = w * 2 / n
Printprint i; " : "; cheby(i)
Nextnext i
Sleepend</syntaxhighlight>
{{out}}
<pre> 0 : 1.647169475390314
1 : -0.2322993716151719
2 : -0.05371511462204768
3 : 0.002458235266981634
4 : 0.0002821190574339161
5 : -7.7222291556156e-006
6 : -5.898556451056081e-007
7 : 1.152142750093788e-008
8 : 6.596299062522348e-010
9 : -1.002201654998203e-011</pre>
 
==={{header|Yabasic}}===
Line 793 ⟶ 1,073:
=={{header|EasyLang}}==
<syntaxhighlight lang="text">
numfmt 012 50
a = 0
b = 1
Line 800 ⟶ 1,080:
len cheby[] n
for i = 0 to n - 1
coef[i + 1] = cos (180 / pi * (cos (180 / n * (i + 1 / 2)) * (b - a) / 2 + (b + a) / 2))
.
for i = 0 to n - 1
w = 0
for j = 0 to n - 1
w += coef[j + 1] * cos (180 / n * i * (j + 1 / 2))
.
cheby[i + 1] = w * 2 / n
print cheby[i + 1]
.
</syntaxhighlight>
Line 1,379 ⟶ 1,659:
9 : -0,0000000000100189955816952521
</pre>
=={{header|МК-61/52}}==
{{trans|BASIC}}
<syntaxhighlight lang="mk-61">0 ПA 1 ПB 8 ПC 0 ПD ИПC ИПD
- x#0 44 пи ИПC / ИПD 1 ^ 2
/ + * cos ИПB ИПA - 2 / *
ИПB ИПA + 2 / + cos KПD ИПD 1
+ ПD БП 08 0 ПD ИПC ИПD - x#0
95 0 ПB ПE ИПC ИПE - x#0 83 пи
ИПC / ИПD * ИПE 1 ^ 2 / +
* cos KИПE * ИПB + ПB ИПE 1 +
ПE БП 54 ИПB 2 * ИПC / С/П ИПD
1 + ПD БП 46 С/П</syntaxhighlight>
=={{header|Nim}}==
{{trans|Go}}
Line 1,701 ⟶ 1,993:
 
my @pi-n = ( ^n »+» ½ ) »×» (π/n);
my @f = ( @pi_npi-n».cos »×» bma »+» bpa )».&$func;
my @sums = (^n).map: { [+] @f »×« ( @pi-n »×» $_ )».cos };
 
Line 1,720 ⟶ 2,012:
+6.5962992e-10
-1.0021994e-11</pre>
 
=={{header|REXX}}==
{{trans|C}}
Line 1,880 ⟶ 2,173:
<syntaxhighlight lang="ruby">func chebft (callback, a, b, n) {
 
var bma = (0.5 * b-a);
var bpa = (0.5 * b+a);
 
var pi_n = ((0..(^n-1)  »+» 0.5)  »*» (NumberNum.pi / n));
var f = (pi_n  »cos»() » »*» bma  »+» bpa «call«  callback);
var sums = (0..(^n-1) «run«  {|i| f  »*«  ((pi_n  »*» i)  »cos»()») «+» });
 
sums  »*» (2/n);
}
 
for v in (chebft(func(v){v.cos}, 0, 1, 10).each) { |v|
say ("%+.10e" % v);
}</syntaxhighlight>
 
Line 1,907 ⟶ 2,200:
-1.0022591709e-11
</pre>
 
=={{header|Swift}}==
 
Line 2,183 ⟶ 2,477:
0.950 0.58168308946388 0.58168308946379 -8.992806E-014
1.000 0.54030230586814 0.54030230586859 4.468648E-013</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var mapRange = Fn.new { |x, min, max, minTo, maxTo| (x - min)/(max - min)*(maxTo - minTo) + minTo }
Line 2,273 ⟶ 2,568:
1.000 0.54030231 0.54030231 4.47e-13
</pre>
 
=={{header|XPL0}}==
{{trans|C}}
<syntaxhighlight lang "XPL0">include xpllib; \for Print and Pi
 
func real Map(X, MinX, MaxX, MinTo, MaxTo);
\Map X from range Min,Max to MinTo,MaxTo
real X, MinX, MaxX, MinTo, MaxTo;
return (X-MinX) / (MaxX-MinX) * (MaxTo-MinTo) + MinTo;
 
proc ChebCoef(N, Min, Max, Coef);
int N; real Min, Max, Coef;
int I, J;
real F;
[for I:= 0 to N-1 do Coef(I):= 0.0;
for I:= 0 to N-1 do
[F:= Cos(Map(Cos(Pi*(float(I)+0.5)/float(N)), -1.0, 1.0, Min, Max)) *
2.0/float(N);
for J:= 0 to N-1 do
Coef(J):= Coef(J) + F*Cos(Pi*float(J) * (float(I)+0.5) / float(N));
];
];
 
func real ChebApprox(X, N, Min, Max, Coef);
real X; int N; real Min, Max, Coef;
real A, B, C, Res;
int I;
[A:= 1.0;
B:= Map(X, Min, Max, -1.0, 1.0);
Res:= Coef(0)/2.0 + Coef(1)*B;
X:= 2.0*B;
for I:= 2 to N-1 do
[C:= X*B - A;
Res:= Res + Coef(I)*C;
A:= B;
B:= C;
];
return Res;
];
 
def N=10, MinV=0.0, MaxV=1.0;
real C(N);
int I;
real X, F, Approx;
[ChebCoef(N, MinV, MaxV, C);
Print("Coefficients:\n");
for I:= 0 to N-1 do
Print(" %2.15f\n", C(I));
Print("\nApproximation:\n X Cos(X) Approx Diff\n");
for I:= 0 to 20 do
[X:= Map(float(I), 0.0, 20.0, MinV, MaxV);
F:= Cos(X);
Approx:= ChebApprox(X, N, MinV, MaxV, C);
Print("%2.2f %2.14f %2.14f %0.1f\n", X, F, Approx, Approx-F);
];
]</syntaxhighlight>
{{out}}
<pre>
Coefficients:
1.647169475390310
-0.232299371615172
-0.053715114622048
0.002458235266982
0.000282119057434
-0.000007722229156
-0.000000589855646
0.000000011521428
0.000000000659630
-0.000000000010022
 
Approximation:
X Cos(X) Approx Diff
0.00 1.00000000000000 1.00000000000047 4.7E-013
0.05 0.99875026039497 0.99875026039487 -9.4E-014
0.10 0.99500416527803 0.99500416527849 4.6E-013
0.15 0.98877107793604 0.98877107793599 -4.7E-014
0.20 0.98006657784124 0.98006657784078 -4.6E-013
0.25 0.96891242171064 0.96891242171041 -2.3E-013
0.30 0.95533648912561 0.95533648912587 2.6E-013
0.35 0.93937271284738 0.93937271284784 4.6E-013
0.40 0.92106099400289 0.92106099400308 2.0E-013
0.45 0.90044710235268 0.90044710235243 -2.5E-013
0.50 0.87758256189037 0.87758256188991 -4.6E-013
0.55 0.85252452205951 0.85252452205926 -2.5E-013
0.60 0.82533561490968 0.82533561490987 2.0E-013
0.65 0.79608379854906 0.79608379854951 4.5E-013
0.70 0.76484218728449 0.76484218728474 2.5E-013
0.75 0.73168886887382 0.73168886887359 -2.3E-013
0.80 0.69670670934717 0.69670670934672 -4.5E-013
0.85 0.65998314588498 0.65998314588494 -4.4E-014
0.90 0.62160996827066 0.62160996827111 4.5E-013
0.95 0.58168308946388 0.58168308946379 -9.0E-014
1.00 0.54030230586814 0.54030230586859 4.5E-013
</pre>
 
=={{header|zkl}}==
{{trans|C}}{{trans|Perl}}