Chebyshev coefficients: Difference between revisions

m (→‎{{header|Sidef}}: Fix link: Perl 6 --> Raku)
Line 1,198:
18 Chebyshev coefficient is: -3.976201538410589537318561880598E-27
19 Chebyshev coefficient is: 2.859065292763079576513213370136E-29
 
=={{header|lang}}==
{{trans|lang}}
<lang lang>def mapp(x, min_x, max_x, min_to, max_to)
return (x - min_x) / (max_x - min_x) * (max_to - min_to) + min_to
end
 
def chebyshevCoef(func, min, max, coef)
n = coef.length
 
for i in 0 .. n-1 do
m = mapp(Math.cos(Math::PI * (i + 0.5) / n), -1, 1, min, max)
f = func.call(m) * 2 / n
 
for j in 0 .. n-1 do
coef[j] = coef[j] + f * Math.cos(Math::PI * j * (i + 0.5) / n)
end
end
end
 
N = 10
def main
c = Array.new(N, 0)
min = 0
max = 1
chebyshevCoef(lambda { |x| Math.cos(x) }, min, max, c)
 
puts "Coefficients:"
puts c
end
 
main()</lang>
{{out}}
<pre>Coefficients:
1.6471694753903139
-0.23229937161517178
-0.0537151146220477
0.002458235266981773
0.00028211905743405485
-7.722229156348348e-06
-5.898556456745974e-07
1.1521427756289171e-08
6.59630183807991e-10
-1.0021913854352249e-11</pre>
 
=={{header|Scala}}==
1,452

edits