我試圖保存proc Inverse2
的輸出$result
,它在每一秒鐘後被調度(它在另一個過程中被調用,該過程被重新調度爲1秒,所以Inverse2程序) 我想輸出是{XY現在}並指定變量它最新的兩個實例錯誤「浮點異常(核心轉儲)」的原因是什麼
x1-> x location at current time (for example at 8.0)
y1-> y location at current time
x2-> x location at (current time+1) (for example at 9.0)
y2-> y location at (current time+1)
並用於進一步的計算。 下面是我嘗試過的代碼,但是經過兩次迭代後我得到的錯誤是Floating point exception (core dumped)
。我在哪裏做錯了?
代碼:
set result {}
proc Inverse2 {m} {
set op [open output.tr w]
global result
global ns
set now [$ns now]
lassign [lindex $m 0 2] x1
lassign [lindex $m 0 3] y1
lassign [lindex $m 0 6] d1
lassign [lindex $m 1 2] x2
lassign [lindex $m 1 3] y2
lassign [lindex $m 1 6] d2
lassign [lindex $m 2 2] x3
lassign [lindex $m 2 3] y3
lassign [lindex $m 2 6] d3
set mt {{? ?} {? ?}}
lset mt 0 0 [expr 2*($x1-$x2)]
lset mt 0 1 [expr 2*($y1-$y2)]
lset mt 1 0 [expr 2*($x1-$x3)]
lset mt 1 1 [expr 2*($y1-$y3)]
set const {{?} {?}}
lset const 0 [expr {(pow($x1,2)+pow($y1,2)-pow($d1,2))-(pow($x2,2)+pow($y2,2)-pow($d2,2))}]
lset const 1 [expr {(pow($x1,2)+pow($y1,2)-pow($d1,2))-(pow($x3,2)+pow($y3,2)-pow($d3,2))}]
#puts $result "$const"
# puts $result "$mt"
set x [expr {double([lindex [Inverse3 $mt] 0 0] * [lindex $const 0]
+ [lindex [Inverse3 $mt] 0 1] * [lindex $const 1])}]
set y [expr {double([lindex [Inverse3 $mt] 1 0] * [lindex $const 0]
+ [lindex [Inverse3 $mt] 1 1] * [lindex $const 1])}]
lappend result "$x $y $now"
puts $result
for {set i 0} {$i< [llength $result]} {incr i} { #for latest two instances
for {set j 1} {$i< [llength $result]} {incr j} {
set X1 [lindex $result $i 0]
set Y1 [lindex $result $i 1]
if {[llength $result] >1} { #to ensure length of list is greater than 1
set X2 [lindex $result $j 0]
set Y2 [lindex $result $j 1]
set v [expr hypot($X2-$X1,$Y2-$Y1)/ ($now-($now-1))]
set theta [expr acos(($X2-$X1)/(hypot($X2-$X1,$Y2-$Y1)))]
set Xp [expr ($X2+($v*$now*cos($theta)))]
set Yp [expr ($Y2+($v*$now*sin($theta)))]
puts "$Xp $Yp"
}
break
}
}
}
關於爲loop.I要分配變量輸出即'$ result'了最新的二元組像this.eg兩次迭代'$後result'看起來像這樣{500.1 450 8.0} {378.1 478 9.0}所以我想分配x1> [lindex $ result 0 0]&y1> [lindex $ result 0 1]和x2> [lindex $ result 1 0]&y2> [lindex $ result 1 1]。因此我初始化了「i」爲0和「j」加1,這樣在每次迭代之後它將從列表中取出最新的兩個元組。 'if'裏面第二個for循環,因爲最初list只有一個元組,因此second for循環不能分配變量並且會拋出錯誤。 –