Anonymous user
Thiele's interpolation formula: Difference between revisions
→{{header|Tcl}}: Corrected version
(Reference of reciprocal difference.) |
(→{{header|Tcl}}: Corrected version) |
||
Line 246:
=={{header|Tcl}}==
{{works with|Tcl|8.5}}<br>{{trans|D}}▼
<lang tcl>#
▲{{works with|Tcl|8.5}}
### Create a thiele-interpretation function with the given name that interpolates
### off the given table.
#
foreach {a b} [lrange $args 0 end-1] {▼
}▼
}▼
▲proc thiele {X Y x} {
# Sanity check
if {[llength $X] != [llength $
error "unequal length lists supplied: [llength $X] != [llength $
}
Line 267 ⟶ 260:
### Compute the table of reciprocal differences
#
set
set p [lrepeat $N [lrepeat $N 0.0]]
for {set
lset p $i 0 [lindex $F $i]
▲ }
}]▼
for {set i 2} {$i <= $n} {incr i} {▼
e [lrange $pp 1 end-1] {▼
}]
}▼
for {set i 0} {$i<$N-$j} {incr i} {
lset p $i $j [expr {
[lindex $p $i+1 $j-2] +
([lindex $X $i] - [lindex $X $i+$j]) /
([lindex $p $i $j-1] - [lindex $p $i+1 $j-1])
▲ }]
▲ }
}
#
###
#
interp alias {} $name {} apply {{X rho f1 x} {
▲ set a [expr {($x - $b) / ([lindex $c 0] - [lindex $d 0] + $a)}]
▲ }
set a [expr {
}
expr {$f1 + ($x - [lindex $X 1]) / ([lindex $rho 1] + $a)}
}} $X [lindex $p 1] [lindex $F 1]
}</lang>
Demonstration code:
Line 306 ⟶ 303:
}
}
initThieleTest
Line 316 ⟶ 313:
Output:
<pre>
pi estimate using sin interpolation: 3.
pi estimate using cos interpolation:
pi estimate using tan interpolation:
</pre>
|