Chebyshev coefficients: Difference between revisions

(Add Swift)
Line 1,398:
9 : -1,0022016549982E-11
</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<lang vbnet>Module Module1
 
Structure ChebyshevApprox
Public ReadOnly coeffs As List(Of Double)
Public ReadOnly domain As Tuple(Of Double, Double)
 
Public Sub New(func As Func(Of Double, Double), n As Integer, domain As Tuple(Of Double, Double))
coeffs = ChebCoef(func, n, domain)
Me.domain = domain
End Sub
 
Public Function Eval(x As Double) As Double
Return ChebEval(coeffs, domain, x)
End Function
End Structure
 
Function AffineRemap(from As Tuple(Of Double, Double), x As Double, t0 As Tuple(Of Double, Double)) As Double
Return t0.Item1 + (x - from.Item1) * (t0.Item2 - t0.Item1) / (from.Item2 - from.Item1)
End Function
 
Function ChebCoef(fVals As List(Of Double)) As List(Of Double)
Dim n = fVals.Count
Dim theta = Math.PI / n
Dim retval As New List(Of Double)
For i = 1 To n
retval.Add(0.0)
Next
For i = 1 To n
Dim ii = i - 1
Dim f = fVals(ii) * 2.0 / n
Dim phi = (ii + 0.5) * theta
Dim c1 = Math.Cos(phi)
Dim s1 = Math.Sin(phi)
Dim c = 1.0
Dim s = 0.0
For j = 1 To n
Dim jj = j - 1
retval(jj) += f * c
' update c -> cos(j*phi) for next value of j
Dim cNext = c * c1 - s * s1
s = c * s1 + s * c1
c = cNext
Next
Next
Return retval
End Function
 
Function ChebCoef(func As Func(Of Double, Double), n As Integer, domain As Tuple(Of Double, Double)) As List(Of Double)
Dim Remap As Func(Of Double, Double)
Remap = Function(x As Double)
Return AffineRemap(Tuple.Create(-1.0, 1.0), x, domain)
End Function
Dim theta = Math.PI / n
Dim fVals As New List(Of Double)
For i = 1 To n
fVals.Add(0.0)
Next
For i = 1 To n
Dim ii = i - 1
fVals(ii) = func(Remap(Math.Cos((ii + 0.5) * theta)))
Next
Return ChebCoef(fVals)
End Function
 
Function ChebEval(coef As List(Of Double), x As Double) As Double
Dim a = 1.0
Dim b = x
Dim c As Double
Dim retval = 0.5 * coef(0) + b * coef(1)
Dim it = coef.GetEnumerator
it.MoveNext()
it.MoveNext()
While it.MoveNext
Dim pc = it.Current
c = 2.0 * b * x - a
retval += pc * c
a = b
b = c
End While
Return retval
End Function
 
Function ChebEval(coef As List(Of Double), domain As Tuple(Of Double, Double), x As Double) As Double
Return ChebEval(coef, AffineRemap(domain, x, Tuple.Create(-1.0, 1.0)))
End Function
 
Sub Main()
Dim N = 10
Dim fApprox As New ChebyshevApprox(AddressOf Math.Cos, N, Tuple.Create(0.0, 1.0))
Console.WriteLine("Coefficients: ")
For Each c In fApprox.coeffs
Console.WriteLine(vbTab + "{0: 0.00000000000000;-0.00000000000000;zero}", c)
Next
 
Console.WriteLine(vbNewLine + "Approximation:" + vbNewLine + " x func(x) approx diff")
Dim nX = 20.0
Dim min = 0.0
Dim max = 1.0
For i = 1 To nX
Dim x = AffineRemap(Tuple.Create(0.0, nX), i, Tuple.Create(min, max))
Dim f = Math.Cos(x)
Dim approx = fApprox.Eval(x)
Console.WriteLine("{0:0.000} {1:0.00000000000000} {2:0.00000000000000} {3:E}", x, f, approx, approx - f)
Next
End Sub
 
End Module</lang>
{{out}}
<pre>Coefficients:
1.64716947539031
-0.23229937161517
-0.05371511462205
0.00245823526698
0.00028211905743
-0.00000772222916
-0.00000058985565
0.00000001152143
0.00000000065963
-0.00000000001002
 
Approximation:
x func(x) approx diff
0.050 0.99875026039497 0.99875026039487 -9.370282E-014
0.100 0.99500416527803 0.99500416527849 4.622969E-013
0.150 0.98877107793604 0.98877107793600 -4.662937E-014
0.200 0.98006657784124 0.98006657784078 -4.604095E-013
0.250 0.96891242171065 0.96891242171041 -2.322587E-013
0.300 0.95533648912561 0.95533648912587 2.609024E-013
0.350 0.93937271284738 0.93937271284784 4.606315E-013
0.400 0.92106099400289 0.92106099400308 1.980638E-013
0.450 0.90044710235268 0.90044710235243 -2.473577E-013
0.500 0.87758256189037 0.87758256188991 -4.586331E-013
0.550 0.85252452205951 0.85252452205926 -2.461364E-013
0.600 0.82533561490968 0.82533561490988 1.961764E-013
0.650 0.79608379854906 0.79608379854951 4.536371E-013
0.700 0.76484218728449 0.76484218728474 2.553513E-013
0.750 0.73168886887382 0.73168886887359 -2.267075E-013
0.800 0.69670670934717 0.69670670934672 -4.467537E-013
0.850 0.65998314588498 0.65998314588494 -4.485301E-014
0.900 0.62160996827066 0.62160996827111 4.444223E-013
0.950 0.58168308946388 0.58168308946379 -8.992806E-014
1.000 0.54030230586814 0.54030230586859 4.468648E-013</pre>
 
=={{header|zkl}}==
1,452

edits