0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

gnuplot備忘録#1 「for文で複数の関数を呼び出す方法」

Posted at

状況

  • 複数の関数を定義して、プロットしたい。

方法

  • 関数のパラメータを増やし、もう一つのパラメータで関数を選択する。

例:

f(x,a) = \
       (a == 1) ? sin(x) : \
       (a == 2) ? cos(x) : \
       (a == 3) ? sinh(x) : \
       (a == 4) ? cosh(x) : \
       0/0

set yrange [-5:5]
plot for [a=1:4] f(x,a)

失敗例

今回この記事を書いたのは、グラフの名前をグラフごとに変えてfor文で呼び出そうとすると、エラーが出てしまったためである。

set multiplot layout 2,2 
set xrange [-pi:pi] 
set yrange [-1:1] 
f1(x)=sin(x)
f2(x)=cos(x)
f3(x)=sinh(x)
f4(x)=cosh(x)

plot for [i=1:4] sprintf("f%d(x)",i)
unset multiplot

このコードを実行すると、以下のようなエラーが出る:

"data.plt" line 11: warning: Cannot find or open file "f1(x)"
 "data.plt" line 11: warning: Cannot find or open file "f2(x)"
 "data.plt" line 11: warning: Cannot find or open file "f3(x)"
 "data.plt" line 11: warning: Cannot find or open file "f4(x)"
 "data.plt" line 11: warning: Cannot find or open file "f5(x)"
 "data.plt" line 11: warning: Cannot find or open file "f6(x)"
 "data.plt" line 11: No data in plot

これは関数を呼び出す際、String型で呼び出すことができないためである。(sprintfはStringを返す)したがって、関数の名前をナンバリングして呼び出すことはかなり難しい。(コマンド全体をforで囲むとか方法はあると思うが、ちょっと思いつかない)

したがって、パラメータを増やして呼び出すことが一番簡単であると思う。

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?