Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ruby script to solve simultaneous equations with Gauss elimination method.
#! /usr/local/bin/ruby
#*********************************************
# 連立方程式の解法 ( ガウスの消去法 )
#*********************************************
#
class GaussElimination
def initialize
# 係数
@a = [
#[ 2, -3, 1, 5],
#[ 1, 1, -1, 2],
#[ 3, 5, -7, 0]
[ 1, -2, 3, -4, 5],
[-2, 5, 8, -3, 9],
[ 5, 4, 7, 1, -1],
[ 9, 7, 3, 5, 4]
]
# 次元の数
@n = @a.size
end
# 計算・結果出力
def exec
# 元の連立方程式をコンソール出力
display_equations
# 前進消去
(@n - 1).times do |k|
(k + 1).upto(@n - 1) do |i|
d = @a[i][k] / @a[k][k].to_f
(k + 1).upto(@n) do |j|
@a[i][j] -= @a[k][j] * d
end
end
end
# 後退代入
(@n - 1).downto(0) do |i|
d = @a[i][@n]
(i + 1).upto(@n - 1) do |j|
d -= @a[i][j] * @a[j][@n]
end
@a[i][@n] = d / @a[i][i].to_f
end
# 結果出力
display_answers
rescue => e
raise
end
private
# 元の連立方程式をコンソール出力
def display_equations
@n.times do |i|
@n.times { |j| printf("%+dx%d ", @a[i][j], j + 1) }
puts "= %+d" % @a[i][@n]
end
rescue => e
raise
end
# 結果出力
def display_answers
0.upto(@n - 1) { |k| puts "x%d = %f" % [k + 1, @a[k][@n]] }
rescue => e
raise
end
end
if __FILE__ == $0
begin
obj = GaussElimination.new
obj.exec
rescue => e
$stderr.puts "[#{e.class}] #{e.message}"
e.backtrace.each{ |tr| $stderr.puts "\t#{tr}" }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.