見出し画像

Maple: Tips ちょっとプログラム「func:=proc(in1,in2)」の使い方


Maple使っていて、数式処理を繰り返したいと思うことありますよね。
そういう場合に使えるのが今回紹介する「proc」によるユーザー関数です。
そんなに難しくないのですぐに誰でも使えるようになると思います。簡単なユーザー関数のフォーマットは以下のプログラム例に示します。
超簡単にまとめると、「関数を定義」して、「処理」して、「returrn」するだけです。「return」は数値だけでなく、「グラフ描け」とかも命令することができます。

# 関数の定義
関数名 := proc(引数1, 引数2, ・・・) 

# 関数内の変数定義
local in1, in2, ・・・ 

# 関数内の処理
関数内の処理を記載
・
・
・
out = ・・・・

# 関数の結果出力
return out; 

#関数の定義終了
end proc;

例)伝達関数にSin波形を入力した出力を表示するユーザー関数

f1, f2の2つの周波数を引数(入力)として、与えられた伝達関数「sys」の応答を重ね描きする関数の例を以下に示します。
作った関数を「explore」でf1、f2をスライダーで動かせるように呼び出すこともできますし、具体的な数字を入力してグラフを表示させることもできます。

with(DynamicSystems);

sys := TransferFunction(1/(s^2 + 0.2*s + 1));

# 関数の定義
func := proc(f1, f2) 

# 関数内の変数定義
local in1, in2, p1, p2; 

# 以下4行は関数内の処理
in1 := Sine(1, f1, 0, 0); 
in2 := Sine(1, f2, 0, 0); 
p1 := ResponsePlot(sys, in1, duration = 50, color = red); 
p2 := ResponsePlot(sys, in2, duration = 50, color = blue); 

# 関数の結果出力(この例では図を表示すること)
return plots:-display(p1, p2, view = [0 .. 100, -1 .. 1]); 

#関数の定義終了
end proc;

# 関数の利用1
Explore(func(f1, f2));

# 関数の利用2
func(5, 10)

【English】

When using Maple, you may sometimes want to repeat mathematical operations. In such cases, the "proc" user function introduced in this article can be used. It is not that difficult, so anyone will be able to use it soon. The format of a simple user function is shown in the following example program. To summarize very simply, all you have to do is "define a function," "process," and "returrn." The "return" can be a command to " plot a graph" as well as a numerical value.

# 関数の定義
関数名 := proc(引数1, 引数2, ・・・) 

# 関数内の変数定義
local in1, in2, ・・・ 

# 関数内の処理
関数内の処理を記載
・
・
・
out = ・・・・

# 関数の結果出力
return out; 

#関数の定義終了
end proc;

Example: User function to display the output of a Sin waveform input to a transfer function

The following is an example of a function that takes two frequencies, f1 and f2, as arguments (inputs) and overplots the response of a given transfer function "sys". You can call the function you created with "explore" to move f1 and f2 with a slider, or you can input specific numbers to display a graph.

with(DynamicSystems);

sys := TransferFunction(1/(s^2 + 0.2*s + 1));

# 関数の定義
func := proc(f1, f2) 

# 関数内の変数定義
local in1, in2, p1, p2; 

# 以下4行は関数内の処理
in1 := Sine(1, f1, 0, 0); 
in2 := Sine(1, f2, 0, 0); 
p1 := ResponsePlot(sys, in1, duration = 50, color = red); 
p2 := ResponsePlot(sys, in2, duration = 50, color = blue); 

# 関数の結果出力(この例では図を表示すること)
return plots:-display(p1, p2, view = [0 .. 100, -1 .. 1]); 

#関数の定義終了
end proc;

# 関数の利用1
Explore(func(f1, f2));

# 関数の利用2
func(5, 10)

【Sample】

Created by Maple 2023.2

この記事が気に入ったらサポートをしてみませんか?