Language/Ruby
[Ruby] 루비로 cos 그래프 구현하기
PersesTitan(페르)
2023. 4. 12. 09:49
관련된 글
[Ruby] 루비로 sin 그래프 구현하기
관련된 글 [Ruby] 루비에서 그래프 구현하기 (gnuplot 설치) [Ruby] 루비에서 그래프 구현하기 (gnuplot 설치) 먼저 homebrew가 설치가 되어 있어야합니다. 그리고 아래 커맨드를 터미널에 입력해서 gnuplot를
persestitan.tistory.com
풀이
sin 그래프를 이미 구현하셨다면 해당 코드에서 코드에서 sin을 cos으로 변경만하면 됩니다.
코드
require 'gnuplot'
Gnuplot.open do |g|
Gnuplot::Plot.new(g) do |plot|
x = (-5..5).step(0.1).collect { |v| v.to_f.round 1 }
y = x.collect { |v| Math.cos v }
plot.data << Gnuplot::DataSet.new([x, y]) do |d|
d.with = "lines"
d.linewidth = 2
end
end
end