#!/usr/local/bin/ruby require 'matrix' class Matrix def []=(i,j,x) @rows[i][j]=x end end DIVIDE = 30 STEP = 10 CFL = 0.5 #Initialize Scheme Matrix scheme = Matrix.I(DIVIDE) (1...(DIVIDE-1)).each{|x| scheme[x, x-1] = -0.5 * CFL scheme[x, x+1] = 0.5 * CFL } scheme_inv = scheme.inverse #Set initiail condition target = [] (0...DIVIDE).each{|x| target << (x < DIVIDE/3 ? 0 : x < 2*DIVIDE/3 ? 1 : 0) } target = Vector.elements(target) #Calc. (0...STEP).each{|x| target = scheme_inv * target } #Output target.to_a.each{|x| printf("%.6f\n", x)}