Chebyshev coefficients: Difference between revisions

(→‎{{header|VBScript}}: Shorter version)
Line 764:
+6.5962992e-10
-1.0021994e-11
</pre>
 
=={{header|Phix}}==
{{trans|Go}}
<lang Phix>function Cheb(atom cmin, cmax, integer ncoeff, nnodes)
sequence c = repeat(0,ncoeff),
f = repeat(0,nnodes),
p = repeat(0,nnodes)
atom z = (cmax + cmin) / 2,
r = (cmax - cmin) / 2
for k=1 to nnodes do
p[k] = PI * ((k-1) + 0.5) / nnodes
f[k] = cos(z + cos(p[k]) * r)
end for
atom n2 = 2 / nnodes
for j=1 to nnodes do
atom s := 0
for k=1 to nnodes do
s += f[k] * cos((j-1)*p[k])
end for
c[j] = s * n2
end for
return c
end function
function evaluate(sequence c, atom cmin, cmax, x)
atom x1 = (2*x - cmax - cmin) / (cmax - cmin),
x2 = 2*x1,
t = 0, s = 0
for j=length(c) to 2 by -1 do
{t, s} = {x2 * t - s + c[j], t}
end for
return x1 * t - s + c[1] / 2
end function
atom cmin = 0.0, cmax = 1.0
sequence c = Cheb(cmin, cmax, 10, 10)
printf(1, "Coefficients:\n")
pp(c,{pp_Nest,1,pp_FltFmt,"%18.15f"})
printf(1,"\nx computed approximated computed-approx\n")
constant n = 10
for i=0 to 10 do
atom x = (cmin * (n - i) + cmax * i) / n,
calc = cos(x),
est = evaluate(c, cmin, cmax, x)
printf(1,"%.1f %12.8f %12.8f %10.3e\n", {x, calc, est, calc-est})
end for</lang>
{{out}}
<pre>
Coefficients:
{ 1.647169475390314,
-0.232299371615172,
-0.053715114622048,
0.002458235266981,
0.000282119057434,
-0.000007722229156,
-0.000000589855645,
0.000000011521427,
0.000000000659630,
-0.000000000010022}
 
x computed approximated computed-approx
0.0 1.00000000 1.00000000 -4.686e-13
0.1 0.99500417 0.99500417 -4.620e-13
0.2 0.98006658 0.98006658 4.600e-13
0.3 0.95533649 0.95533649 -2.604e-13
0.4 0.92106099 0.92106099 -1.970e-13
0.5 0.87758256 0.87758256 4.587e-13
0.6 0.82533561 0.82533561 -1.968e-13
0.7 0.76484219 0.76484219 -2.551e-13
0.8 0.69670671 0.69670671 4.470e-13
0.9 0.62160997 0.62160997 -4.450e-13
1.0 0.54030231 0.54030231 -4.477e-13
</pre>
 
7,813

edits