Chebyshev coefficients: Difference between revisions
Content added Content deleted
Line 811: | Line 811: | ||
1.000 0.54030231 0.54030231 4.5e-13 |
1.000 0.54030231 0.54030231 4.5e-13 |
||
</pre> |
</pre> |
||
=={{header|Lua}}== |
|||
{{trans|Java}} |
|||
<lang lua>function map(x, min_x, max_x, min_to, max_to) |
|||
return (x - min_x) / (max_x - min_x) * (max_to - min_to) + min_to |
|||
end |
|||
function chebyshevCoef(func, minn, maxx, coef) |
|||
local N = table.getn(coef) |
|||
for j=1,N do |
|||
local i = j - 1 |
|||
local m = map(math.cos(math.pi * (i + 0.5) / N), -1, 1, minn, maxx) |
|||
local f = func(m) * 2 / N |
|||
for k=1,N do |
|||
local p = k -1 |
|||
coef[k] = coef[k] + f * math.cos(math.pi * p * (i + 0.5) / N) |
|||
end |
|||
end |
|||
end |
|||
function main() |
|||
local N = 10 |
|||
local c = {} |
|||
local minn = 0.0 |
|||
local maxx = 1.0 |
|||
for i=1,N do |
|||
table.insert(c, 0) |
|||
end |
|||
chebyshevCoef(function (x) return math.cos(x) end, minn, maxx, c) |
|||
print("Coefficients:") |
|||
for i,d in pairs(c) do |
|||
print(d) |
|||
end |
|||
end |
|||
main() |
|||
</lang> |
|||
{{out}} |
|||
<pre>Coefficients: |
|||
1.6471694753903 |
|||
-0.23229937161517 |
|||
-0.053715114622048 |
|||
0.0024582352669818 |
|||
0.00028211905743405 |
|||
-7.7222291563483e-006 |
|||
-5.898556456746e-007 |
|||
1.1521427756289e-008 |
|||
6.5963018380799e-010 |
|||
-1.0021913854352e-011</pre> |
|||
=={{header|Microsoft Small Basic}}== |
=={{header|Microsoft Small Basic}}== |