maple による - part 1 - cybernet › maple › documents › pdf › example › ...plot...

32
Maple による ため ・プロット - Part 1 サイバネットシステム株 ワークシートシリーズ モデル Maple 』を した、 ・プロット について をしています。Maple 意されている プロット また して、 ったり、また ・プロットに たデータ いて しいアルゴリズム アプリケーション にお けます。 お、こ ワークシート Maple について学 しているこ にしていま す。Maple について から されている「Maple ギナーズガイド してください。 Maple する ツールを していますが、 する しています。また、Maple ツール してだけ く、GUI いた対 アプリ ケーション して、さらに マルチスレッド・ いたアルゴリズム して けます。 ワークシート 、そ よう Maple をお けるようにするために、以 ように4つ から っています。 1: するグラフ・プロット 2:データに するグラフ・プロット 3:ドキュメント アプリケーション する 4:アルゴリズム ため Table 1: ワークシート

Upload: others

Post on 09-Jun-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

Maple による科学技術計算のための可視化・プロット関連機能 - Part 1

サイバネットシステム株式会社

このワークシートシリーズでは、数式処理・数式モデル設計環境『Maple』を利用した、科学技術計算を行う際に便利な可視化・プロット機能について包括的な紹介をしています。Maple に用意されている豊富なプロット機能または関連機能を利用して、目的別の可視化を行ったり、また可視化・プロットに関連したデータ処理を用いて新しいアルゴリズムやアプリケーションの開発にお役立て頂けます。なお、このワークシートはすでに Maple の基本的な操作方法について学習していることを前提にしています。Maple の基本的な操作方法については当社から提供されている「Maple ビギナーズガイド」等を参照してください。

目的と構成Maple は数式処理に関する豊富な計算ツールを提供していますが、同時に数値計算に関する機能も豊富に提供しています。また、Maple は計算ツールとしてだけではなく、GUI 部品を用いた対話的なアプリケーション開発環境として、さらに最先端のマルチスレッド・並列計算等も用いたアルゴリズム開発環境としてもお使い頂けます。このワークシートは、そのような科学技術計算全般で Maple をお使い頂けるようにするために、以下のように4つの構成から成っています。

利用例1:数式に関するグラフ・プロット作成 利用例2:データに関するグラフ・プロット作成

利用例3:ドキュメントやアプリケーション開発に関する関連機能

利用例4:アルゴリズム開発のための関連機能

Table 1: 本ワークシートの構成

Page 2: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

Part 1 では、利用例1~2までを紹介し、Part 2 では利用例3~4について紹介します。

関連リソースMaple で利用可能な可視化・プロット機能の全体は、ヘルプ・コンテンツとして提供されている『プロットガイド』が便利です。

Table 2: 『プロットガイド』メニュー

『プロットガイド』は [ヘルプ] メニューから [マニュアル、リソース、その他] を選択し、『プロットガイド』のメニュー項目から選択可能です。

プロットガイドでは、目的別のプロットメニューへのリンクが掲載されています。等高線や密度線、ベクトル場の描画など目的別のプロットのヘルプへのリンクが用意されています。

Page 3: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

> >

> >

利用例1:数式に対する可視化・プロットと関連機能

基本的なプロット機能ここでは、プロットの対象が何かしらの数式で表現されている場合について紹介します。もっとも頻繁に使われるのは plot コマンドです。 plot コマンドは1変数の式をプロットします。

plot(sin(x), x = 0 .. 10);

x2 4 6 8 10

K1

K0.5

0

0.5

1

プロット時の軸のスタイルや線の色などはオプションで指定するか、もしくは GUI 上でツールバーまたはグラフを右クリックして表示されるメニューから設定可能です。

plot コマンドのオプションによる指定:plot(sin(x), x = 0 .. 10, axes = boxed);

x0 2 4 6 8 10

K1

K0.5

0

0.5

1

Page 4: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

> >

> >

> >

グリッド線を指定:plot(sin(x), x = 0 .. 10, axes = boxed, gridlines = true);

x0 2 4 6 8 10

K1

K0.5

0

0.5

1

線色(color)と太さ(thickness)を指定:plot(sin(x), x = 0 .. 10, axes = boxed, gridlines = true, color = green, thickness = 3);

x0 2 4 6 8 10

K1

K0.5

0

0.5

1

plot コマンドで指定可能なオプションは、plot,option のヘルプから参照することが可能です。

プロットのオプションを GUI 上で変更する場合は、描画されたプロットを一度マウスで選択し、その後マウス右ボタンをクリックして表示されるメニューから選択します。

plot コマンドと同様に頻繁によく使われるのは plot3d コマンドです。plot3d コマンドは、2変数の式に対する描画を行います。

plot3d(sin(x^2-x*y+y^2), x = -2 .. 2, y = -2 .. 2);

Page 5: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

> >

plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照明位置)などを変更することが可能です。

plot3d( sin(x^2-x*y+y^2), x = -2 .. 2, y = -2 .. 2, axes = boxed, #軸のスタイル指定 scaling = constrained, #軸の縦横比設定の指定 style = patchnogrid, #曲面のスタイルの指定 lightmodel = light3, #照明の指定 glossiness = 0.5 #光沢度の指定);

plots パッケージを用いた可視化・プロット

Page 6: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

> >

> >

> >

(3.2.1)(3.2.1)

plots パッケージには、50種以上の拡張プロットコマンドが標準で用意されています。

restart;with(plots);

animate, animate3d, animatecurve, arrow, changecoords, complexplot, complexplot3d, conformal,conformal3d, contourplot, contourplot3d, coordplot, coordplot3d, densityplot, display,dualaxisplot, fieldplot, fieldplot3d, gradplot, gradplot3d, implicitplot, implicitplot3d, inequal,interactive, interactiveparams, intersectplot, listcontplot, listcontplot3d, listdensityplot, listplot,listplot3d, loglogplot, logplot, matrixplot, multiple, odeplot, pareto, plotcompare, pointplot,pointplot3d, polarplot, polygonplot, polygonplot3d, polyhedra_supported, polyhedraplot,rootlocus, semilogplot, setcolors, setoptions, setoptions3d, spacecurve, sparsematrixplot, surfdata,textplot, textplot3d, tubeplot

いくつかの目的別プロットコマンドを例題と共に見てみます。

チューブ形状のプロット:tubeplot( [cos(t),sin(t),0], t = 0..2*Pi, radius=0.25, #チューブ形状の半径 scaling=constrained);

tubeplot( {[cos(t),sin(t),0],[0,sin(t)-1,cos(t)]}, t = 0..2*Pi, radius=0.25, scaling=constrained);

Page 7: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

> >

> > 極座標で表現された数式のプロット:

polarplot([cos(2*t),sin(8*t),t=0..Pi]);

0 0.2 0.4 0.6 0.80

π4

π2

3 π4

π

5 π4

3 π2

7 π4

複素関数の等角プロット:conformal((2*z-1)/(2-z),z=-2-2*I..2+2*I,-2-2*I..2+2*I,gridlines=true);

Page 8: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

> >

> >

K2 K1 0 1 2

K2

K1

1

2

座標系のプロット:coordplot(bipolar);

coordplot(maxwell);

Page 9: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

(3.2.2)(3.2.2)

> >

> >

> >

根軌跡図のプロット:rootlocus( (s^4+s-2)/(s^2-3*s+4), s,-3..3, style=point, gridlines=true);

K8 K6 K4 K2 0 2 4 6K8

K6

K4

K2

0

2

4

6

8

微分方程式の近似解のプロット:# 微分方程式の近似解を計算:dsol := dsolve([diff(y(t),t)=sin(t*y(t)),y(0)=1],y(t),numeric);

dsol := proc x_rkf45 ... end proc

odeplot( dsol, #得られた微分方程式の近似解を指定

Page 10: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

> >

> >

[t,y(t)], #プロットする変数・関数を指定 t=0..10, #プロットの範囲を指定 gridlines=true);

t0 2 4 6 8 10

y

0.4

0.6

0.8

1.0

1.2

1.4

1.6

1.8

ベクトル場のプロット:fieldplot( [y,-sin(x)-y/20], x=-10..10, y=-7..7, color=x, #色関数を指定 arrows=SLIM, #矢印のタイプを指定 grid=[15,15] #ベクトルを描くプロット評価点の個数を指定);

xK10 K5 0 5 10

y

K6

K4

K2

2

4

6

アニメーションの描画:animate( plot3d, # アニメーションの元となるプロット用コマンドを指定 [sin(a)*(x^2-y^2),x=-2..2,y=-2..2,axes=frame], # 一番目の引数で与えた

Page 11: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

> >

(3.2.3)(3.2.3)

> >

プロット用コマンドへの引数のリスト a=0..2*Pi, # パラメータ範囲 frames=20 # フレーム数);

a = 0.

# 描画対象の数式を定義f := (x,y,p) -> 0.5*sqrt(x^2+y^2)*sin(p*arctan(y/x)+2*p*sqrt(x^2+y^2));

f := x, y, p /0.5 x2Cy2 sin p arctan yx

C2 p x2Cy2

# 各種オプションを与えてアニメーションを描画animate( plot3d, [f(x,y,p), x=-1..1, y=-1..1, # 描画式と範囲を指定 axes=frame, grid=[100,100], # 軸スタイルと描画点数を指定 style=patchnogrid, shading=zhue, # 曲面スタイルと光沢度を指定 transparency=0.2, lightmodel=light4, # 透明度と照明モデルを指定 scaling=constrained], # 軸の縦横比形式を指定 p=5..20, # パラメータ p の範囲を指定 frames=40 # アニメーションのフレーム数を指定);

Page 12: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

> >

(3.3.2)(3.3.2)

> > (3.3.1)(3.3.1)

> >

p = 5.

plottools パッケージを用いた可視化・プロット

plottools パッケージには、すでに描画したプロットデータに対する操作コマンドなどのユーティリティとして利用できる機能も用意されています。

with(plottools);arc, arrow, circle, cone, cuboid, curve, cutin, cutout, cylinder, disk, dodecahedron, ellipse, ellipticArc,

getdata, hemisphere, hexahedron, homothety, hyperbola, icosahedron, line, octahedron,parallelepiped, pieslice, point, polygon, project, rectangle, reflect, rotate, scale, semitorus, sphere,stellate, tetrahedron, torus, transform, translate

plottools パッケージの transform コマンドは、一度描画したプロットデータに対して変形・移動を適用することが出来るコマンドです。以下の例で説明します。

まず、3次元の等高線のプロット g1 を用意します。g1 := plot3d(0.75*sin(x*y), x=-3..3,y=-3..3, scaling=constrained, style=contour, contours=10, axes=boxed, thickness=3);

g1 := PLOT3D ...g1;

Page 13: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

> >

> >

> >

(3.3.3)(3.3.3)> >

同じ関数に対して、 contourplot コマンドによるプロット g2 を作成します。g2 := contourplot(0.75*sin(x*y),x=-3..3,y=-3..3,filled=true);

g2 := PLOT ...g2;

ここで、 transform コマンドを用いてプロットオブジェクト g1, g2 を同時に描画します。まず、tranform コマンドを用いたプロシージャ F を次のように定義します。

F := transform((x,y) -> [x,y,-3]):

定義した F を g2 に適用し、g1 と共に描画してみます。display([g1,F(g2)]);

Page 14: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

> >

> >

(3.4.1)(3.4.1)

このように tranform コマンドを用いることで、2次元内のプロットオブジェクトを3次元空間で用いることも可能になります。同様に、 project コマンドや reflect コマンドを用いて、既存のプロットオブジェクトに対する操作処理も手軽に実現できます。

geometry パッケージ, geom3d パッケージを用いた可視化・プロットrestart;

geometry パッケージは平面幾何の、geom3d パッケージは空間幾何の計算・プロット機能を提供しています。なお、geometry パッケージの詳しい例題は geometry_examples のヘルプトピックスに記載されています。

geometry, geom3d パッケージでは、それぞれ幾何学的なオブジェクト(線分、無向線分、有向線分、三角形、矩形、放物線、双曲線等)に対して幾何学上の種々の計算やオブジェクトの描画機能を提供しています。

三角形の定義と描画、辺の長さの計算:with(geometry);

Apollonius, AreCollinear, AreConcurrent, AreConcyclic, AreConjugate, AreHarmonic,AreOrthogonal, AreParallel, ArePerpendicular, AreSimilar, AreTangent, CircleOfSimilitude,CrossProduct, CrossRatio, DefinedAs, Equation, EulerCircle, EulerLine, ExteriorAngle,ExternalBisector, FindAngle, GergonnePoint, GlideReflection, HorizontalCoord,HorizontalName, InteriorAngle, IsEquilateral, IsOnCircle, IsOnLine, IsRightTriangle, MajorAxis,MakeSquare, MinorAxis, NagelPoint, OnSegment, ParallelLine, PedalTriangle, PerpenBisector,PerpendicularLine, Polar, Pole, RadicalAxis, RadicalCenter, RegularPolygon,RegularStarPolygon, SensedMagnitude, SimsonLine, SpiralRotation, StretchReflection,StretchRotation, TangentLine, VerticalCoord, VerticalName, altitude, apothem, area, asymptotes,

Page 15: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

> >

> >

(3.4.7)(3.4.7)

(3.4.3)(3.4.3)

> >

(3.4.2)(3.4.2)

(3.4.5)(3.4.5)

> >

> >

> >

(3.4.4)(3.4.4)

> >

(3.4.6)(3.4.6)

bisector, center, centroid, circle, circumcircle, conic, convexhull, coordinates, detail, diagonal,diameter, dilatation, directrix, distance, draw, dsegment, ellipse, excircle, expansion, foci, focus,form, homology, homothety, hyperbola, incircle, inradius, intersection, inversion, line, medial,median, method, midpoint, orthocenter, parabola, perimeter, point, powerpc, projection, radius,randpoint, reciprocation, reflection, rotation, segment, sides, similitude, slope, square, stretch,tangentpc, translation, triangle, vertex, vertices# 三角形オブジェクト P を頂点座標から定義triangle(P, [point(A,0,0), point(B,3,0), point(C,0,4)]);

P# オブジェクト P についての詳細属性情報detail(P);

name of the object P

form of the object triangle2d

method to define the triangle points

the three vertices 0, 0 , 3, 0 , 0, 4

# P を描画draw(P);

0 1 2 30

1

2

3

4

# P の各辺の長さを計算sides(P);

9 , 25 , 16

双曲線 H の描画と焦点の計算:hyperbola(H,-4*x^2+9*y^2=32,[x,y]);

H# 双曲線 H の中心座標coordinates(center(H));

0, 0# 双曲線 H の漸近線map(Equation, asymptotes(H));

yC 23

x = 0, yK 23

x = 0

Page 16: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

> >

(3.4.8)(3.4.8)

(3.4.9)(3.4.9)> >

> > # 双曲線 H を描画draw(H);

K20 K10 0 10 20

K10

K5

0

5

10

# 双曲線 H の焦点座標map(coordinates, foci(H));

0, K23

26 , 0, 23

26

geom3d パッケージを用いた空間図形に対する処理も geometry パッケージと同様にして行えます。

原点を中心とした icosahedron (正20面体)の作成と描画:with(geom3d);

Archimedean, AreCollinear, AreConcurrent, AreConjugate, AreCoplanar, AreDistinct, AreParallel,ArePerpendicular, AreSkewLines, DefinedAs, DirectionRatios, Equation, FindAngle, FixedPoint,GlideReflect, GlideReflection, GreatDodecahedron, GreatIcosahedron,GreatRhombicuboctahedron, GreatRhombiicosidodecahedron, GreatStellatedDodecahedron,HarmonicConjugate, HexakisIcosahedron, HexakisOctahedron, InRadius, IsArchimedean,IsEquilateral, IsFacetted, IsOnObject, IsQuasi, IsRegular, IsRightTriangle, IsStellated, IsTangent,MidRadius, NormalVector, OnSegment, ParallelVector, PentagonalHexacontahedron,PentagonalIcositetrahedron, PentakisDodecahedron, QuasiRegularPolyhedron, RadicalCenter,RadicalLine, RadicalPlane, RegularPolyhedron, RhombicDodecahedron,RhombicTriacontahedron, RotatoryReflect, RotatoryReflection, ScrewDisplace,ScrewDisplacement, SmallRhombicuboctahedron, SmallRhombiicosidodecahedron,SmallStellatedDodecahedron, SnubCube, SnubDodecahedron, StereographicProjection,StretchRotate, TangentPlane, TetrakisHexahedron, TrapezoidalHexecontahedron,TrapezoidalIcositetrahedron, TriakisIcosahedron, TriakisOctahedron, TriakisTetrahedron,TruncatedCuboctahedron, TruncatedDodecahedron, TruncatedHexahedron,TruncatedIcosahedron, TruncatedIcosidodecahedron, TruncatedOctahedron,TruncatedTetrahedron, altitude, area, center, centroid, coordinates, cube, cuboctahedron, detail,dilate, distance, dodecahedron, draw, dsegment, duality, faces, facet, form, gtetrahedron,hexahedron, homology, homothety, icosahedron, icosidodecahedron, incident, intersection,

Page 17: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

> >

(3.4.10)(3.4.10)> >

> >

(3.4.11)(3.4.11)

> >

inverse, inversion, line, midpoint, octahedron, parallel, parallelepiped, plane, point, polar, pole,powerps, projection, radius, randpoint, reflect, reflection, rotate, rotation, schlafli, segment, sides,sphere, stellate, tetrahedron, tname, transform, translate, translation, transprod, triangle, vertices,volume, xcoord, xname, ycoord, yname, zcoord, znameicosahedron(p1,point(o,0,0,0),1);

p1draw(p1);

# 正20面体の各面を星状に分割(stellate)したオブジェクトを p2 として定義stellate(p2,p1,4);

p2# p2 を描画draw(p2);

Page 18: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

• • • •

• • • •

> >

その他の可視化・プロット関連機能

上記以外の可視化・プロット機能についてはそれぞれのヘルプページを参照してください。

組合せ論に関連した可視化・プロット機能:GraphTheory パッケージ内の DrawGraph コマンド統計解析に関連した可視化・プロット機能:Statistics パッケージ 描画機能一覧画像処理に関連した可視化・プロット機能:ImageTools パッケージ内の View コマンド動的システム(制御対象モデル)に関連した可視化・プロット機能:DynamicSystems パッケージ

利用例2:数値データに対する可視化・プロットと関連機能

restart;

アシスタント機能を用いたデータの読込み

Maple には、外部データ(画像、音声、Excel、CSV等)を読み込むためのダイアログ形式のアシスタント機能が用意されています。[ツール]メニューの[アシスタント]から[データインポート]を選択するか、もしくはコマンドで以下を実行するとダイアログが表示されます。

Page 19: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

(4.2.3)(4.2.3)

(4.2.5)(4.2.5)

(4.2.6)(4.2.6)

> >

(4.2.2)(4.2.2)

> >

Table 4: [データインポート]メニューTable 4: [データインポート]メニュー

(4.2.4)(4.2.4)

(4 2 7)(4 2 7)

> >

> >

> >

> >

> >

> >

(4.2.1)(4.2.1)

コマンドから呼び出す場合:ImportData();

読み込まれたデータは、Maple 上の配列データまたは行列データとして取り扱われます。

各種読込みコマンドによるデータの読込み

区切りテキストデータの場合:# サンプルデータ用フォルダを変数に割当:dataDir := kernelopts(datadir);

dataDir := "C:\Program Files\Maple 15\data"# CSV データを読み込む場合dataCSV := ImportMatrix(cat(dataDir,"/help/ImportData/excel.csv"),source=csv);

dataCSV :=

28 x 6 Matrix

Data Type: anything

Storage: rectangular

Order: Fortran_order

# dataCSV の1行目を表示:dataCSV[1,1..-1];

"Mar 06 01:16 " "3/7/2005" 26.86091241 1.414301472 107 1.505894402 107 0.0648

# 半角 1byte スペースで区切られたファイルを読み込む場合dataDEL := ImportMatrix(cat(dataDir,"/help/ImportData/example.del"),delimiter=" ",datatype=string);

dataDEL :=

93 x 7 Matrix

Data Type: string

Storage: rectangular

Order: Fortran_order

dataDEL[1,1..-1];

"Country" "GDP" "GDPpc" "LifeExp." "Persons/MD" "InfantMort." "Literacy%"

dataDEL[2,1..-1];

"Algeria" "42.00" "1570" "69" "1062" "52" "0.52"

画像データの場合:# ImageTools パッケージを読込みwith(ImageTools);

Page 20: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

> >

(4.2.8)(4.2.8)

> >

> >

(4.2.10)(4.2.10)

(4.2.9)(4.2.9)

(4.2.7)(4.2.7)

> >

> >

Checkerboard, Clip, ColorAImage, ColorImage, ColorTransform, CombineLayers, Complement,

Convolution, Create, Entropy, FitIntensity, Flip, FormatFromName, Formats, Γ, GetLayer,

GetSubImage, GrayImage, HSVtoRGB, Height, Histogram, Image, Intensity, Layers, Mask,

PadImage, PlotHistogram, Preview, Quality, RGBtoGray, RGBtoHSV, RGBtoYUV, Read, Rotate,

Scale, ScaleIntensity, SetLayer, SetSubImage, Stack, Threshold, ToGrayscale, ToRGB, ToRGBA,

Transpose, View, WhatTypeImage, Width, Write, YUVtoRGB

imgFile := Read(cat(dataDir,"/images/phone.jpg"));

imgFile :=

1..256 x 1..256 Array

Data Type: float8

Storage: rectangular

Order: C_order

# 読み込んだ画像データを確認View(imgFile);

unwith(ImageTools);

音声データの場合:# AudioTools パッケージを読込みwith(AudioTools);

Audio, Clip, Convolution, Create, Duration, Extract, FormatFromName, Formats, Modulate,Normalize, Preview, Read, Resample, Scale, ToMono, ToStereo, WritewaveFile := Read(cat(dataDir,"/help/AudioTools/stereo.wav"));

Page 21: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

> >

> >

(4.2.13)(4.2.13)

(4.2.12)(4.2.12)

(4.2.10)(4.2.10)

> >

(4.2.14)(4.2.14)

> >

(4.2.11)(4.2.11)

(4.2.15)(4.2.15)

> >

> >

> >

waveFile :=

1..19962 x 1..2 Array

Data Type: float8

Storage: rectangular

Order: C_order

# 先頭の3行分データを確認waveFile[1..3];

K0.0156250000000000 0.212598425196850

0. K0.265625000000000

0.0393700787401575 0.456692913385827

# 0.2sec ~ 0.3sec 間の波形を確認Preview(waveFile,0.2..0.3);

0.22 0.24 0.26 0.28 0.30

unwith(AudioTools);

Excel ファイルの場合:# ExcelTools パッケージを読込みwith(ExcelTools);

Export, Import, WorkbookDataxlsFile := Import(cat(dataDir,"/Excel/ExperimentalData.xls"));

xlsFile :=

1..12 x 1..7 Array

Data Type: anything

Storage: rectangular

Order: Fortran_order

# 読み込んだファイルの1行目・2行目を確認xlsFile[1,1..-1];

"Time", "Concentration A", "Concentration CO2", "Concentration AT", "Concentration B", "Concentration C", "Concentration A.B"xlsFile[2,1..-1];

0. 0.54399 0.00121 0.0048 0.00722 0.00079 0.34423

Page 22: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

(4.3.3)(4.3.3)

> >

> >

> >

(4.3.2)(4.3.2)

> >

(4.3.4)(4.3.4)

> >

> >

(4.3.1)(4.3.1)

ファイル入出力関数を用いたデータの読込み

Maple には、C言語などと同じように低レベルのファイル入出力I/Oのための関数群も用意されています。関連するパッケージと共にそれらの関数群を見てみます。

低レベルのファイルI/O:restart;# ファイル名を変数に割当てfname := "C:\\temp\\sampleData.txt";

fname := "C:\temp\sampleData.txt"# for コマンドを使って行単位でファイルにデータを書込みfor xi from 1 to 10 do fprintf(fname, "x=%f, y=%f\n", xi, evalf(sin(Pi/xi)) );end do;

23232323232323232324

# 書き込んだファイルを閉じるfclose(fname);# 書き込んだファイルから fscanf 関数で数値データを読込み# 読み込んだデータは valueData 変数に長さ分の配列型を用意して格納valueData := Array(1..10):for k from 1 to 10 do valueData[k] := fscanf(fname, "x=%f, y=%f\n");end do;

valueData1 := 1.000000, 0.

valueData2 := 2.000000, 1.000000

valueData3 := 3.000000, 0.866025

valueData4 := 4.000000, 0.707107

valueData5 := 5.000000, 0.587785

valueData6 := 6.000000, 0.500000

valueData7 := 7.000000, 0.433884

valueData8 := 8.000000, 0.382683

valueData9 := 9.000000, 0.342020

valueData10 := 10.000000, 0.309017

# 読み込んだデータを確認valueData;1.000000, 0. , 2.000000, 1.000000 , 3.000000, 0.866025 , 4.000000, 0.707107 , 5.000000,

Page 23: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

> >

> >

> >

> >

> >

> >

> >

> >

(4.3.7)(4.3.7)

> >

> >

> >

(4.3.8)(4.3.8)

> >

> >

(4.3.5)(4.3.5)

> >

(4.3.6)(4.3.6)

(4.3.10)(4.3.10)

(4.3.9)(4.3.9)

> >

0.587785 , 6.000000, 0.500000 , 7.000000, 0.433884 , 8.000000, 0.382683 , 9.000000,0.342020 , 10.000000, 0.309017# ファイルを閉じるfclose(fname);

なお、行単位での入出力については readline, writeline コマンドも用意されています。

?readline

テキストファイルによる Maple 上で定義したプログラムや変数内容の保存・読込:# 自作の関数を定義myfunc := proc(x,y) local t;

t := sqrt(x^2+y^2); return(t);end proc;

myfunc := proc x, y local t; t := sqrt x^2Cy^2 ; return t end proc

# 定義した myfunc を "myfunc.mpl" の名前で C:\\temp フォルダへ保存save myfunc, "C:\\temp\\myfunc.mpl";# Maple カーネルを再起動restart;# 保存した .mpl ファイルから myfunc 関数の定義を読込みread "C:\\temp\\myfunc.mpl";

myfunc := proc x, y local t; t := sqrt x^2Cy^2 ; return t end proc

myfunc(3,4);5

# 変数 ret に式の展開結果を割当てret := expand((x+y-z)^50):# ret の結果を外部ファイルに保存save ret, "C:\\temp\\expand.txt";# Maple カーネルを再起動し ret に何も割り当てられていないことを確認restart;ret;

ret# 保存したファイルから先の計算結果を読込みread "C:\\temp\\expand.txt":# 再び変数 ret に結果が割り当てられるので中身を確認op(1..10,ret);

y50, z50, 95945650286253768000 x23 y7 z20, K31981883428751256000 x23 y6 z21,

8722331844204888000 x23 y5 z22, K1896159096566280000 x23 y4 z23,

316026516094380000 x23 y3 z24, K37923181931325600 x23 y2 z25, 2917167840871200 x23 y z26,

K2484994827408800 x22 y27 z

FileTools パッケージによるファイルI/O:なお、FileTools パッケージは上述の低レベルI/Oよりも多くの機能・オプションが提供されています。例えば、特定ディレクトリ以下のファイル名の取得(ListDirectory コマンド)、ディレクトリの作成(MakeDirectory)、ファイルサイズの取得(Size コマンド)などです。

restart;# パッケージを読込みwith(FileTools);

AbsolutePath, AtEndOfFile, Binary, Copy, Exists, Filename, Flush, IsDirectory, IsExecutable,IsLockable, IsOpen, IsReadable, IsWritable, ListDirectory, Lock, MakeDirectory,

Page 24: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

(4.3.14)(4.3.14)

(4.3.17)(4.3.17)

> >

(4.3.12)(4.3.12)

> >

(4.3.15)(4.3.15)

(4.3.13)(4.3.13)

(4.3.11)(4.3.11)> >

> >

> >

> >

> >

> >

> >

> >

(4.3.16)(4.3.16)

> >

ModificationTime, ParentDirectory, Position, Remove, Rename, Size, Status,TemporaryFilename, Text, Unlockfname := "C:\\temp\\sampleData.txt";

fname := "C:\temp\sampleData.txt"# FileTools パッケージには、テキストファイルへの操作と# バイナリファイルへの操作に応じて2つのサブパッケージ# が用意されています。# ここではテキストファイルの操作を行なってみます。# Readline コマンドは行単位でファイルを読み込みます。# Text サブパッケージの CountLines コマンドで行数を# 取得し、各行に対して読込を行います。# 読み込んだ結果は parse コマンドにより Maple 上での# 表現式に解釈され、eval コマンドで値を取り出しています。

loadData :=[seq(eval([x,y], [parse(Text:-ReadLine(fname))]), k=1..Text:-CountLines(fname) )];Text:-Close(fname);

loadData := 1.000000, 0. , 2.000000, 1.000000 , 3.000000, 0.866025 , 4.000000, 0.707107 ,5.000000, 0.587785 , 6.000000, 0.500000 , 7.000000, 0.433884 , 8.000000, 0.382683 ,9.000000, 0.342020 , 10.000000, 0.309017

restart;with(FileTools):# バイナリ形式による保存# 次に示す変数 data に行列データを割当てdata := Matrix( [seq([x,1/(1-exp(x))],x=1.0..10.0,0.5)], datatype = float[8]);

data :=

19 x 2 Matrix

Data Type: float8

Storage: rectangular

Order: Fortran_order

# 保存するファイル名を変数 fname で用意fname := "C:\\Temp\\binData.dat";

fname := "C:\Temp\binData.dat"# すでに同名のファイルがある場合は削除Exists(fname);

false

`if`((4.3.15), Remove(fname),NULL);# バイナリ形式で保存Binary:-Write(fname, data);Binary:-Close(fname);

304# 変数 data の内容を消去unassign('data');# 次の変数に保存したバイナリ形式のファイルからデータを読込みmyData := Binary:-Read( fname, # ファイル名 float[8], # データの形式 byteorder=native # 保存した際に用いたバイトオーダー);Binary:-Close(fname);

myData := 1., 1.50000000000000000, 2., 2.50000000000000000, 3., 3.50000000000000000, 4.,

Page 25: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

(4.4.1)(4.4.1)

> > > >

> >

> >

(4.3.18)(4.3.18)

> >

> >

(4.3.19)(4.3.19)

> >

4.50000000000000000, 5., 5.50000000000000000, 6., 6.50000000000000000, 7.,7.50000000000000000, 8., 8.50000000000000000, 9., 9.50000000000000000, 10.,K0.581976706999999926, K0.287216916799999999, K0.156517642699999987,K0.0894254898399999970, K0.0523956964999999983, K0.0311376592599999988,K0.0186573603599999999, K0.0112337927000000001, K0.00678365490599999992,K0.00410354167499999995, K0.00248491165700000021, K0.00150570292600000002,K0.000912714253599999957, K0.000553390441900000057, K0.000335575200800000012,K0.000203509776799999995, K0.000123425035899999992, K0.0000748574331000000011,K0.0000454019910199999983# 読み込んだデータ配列の長さを確認nops(myData);

38# ファイルに保存した際に行列構造はフラットになっているため# 配列長さの最初の半分がインデックス値、残りがデータ値とな# ります。myData[1..nops(myData)/2];

1., 1.50000000000000000, 2., 2.50000000000000000, 3., 3.50000000000000000, 4.,4.50000000000000000, 5., 5.50000000000000000, 6., 6.50000000000000000, 7.,7.50000000000000000, 8., 8.50000000000000000, 9., 9.50000000000000000, 10.unwith(FileTools);

ファイル入出力に関連して、XML 等の構造化データを扱う場合は、 XMLTools パッケージやMathML パッケージが便利です。

数値データを対象にした可視化・プロット機能

各種の読込みコマンドやファイル入出力関数で取り込んだデータを Maple 上で描画するためのコマンドが用意されています。

plot コマンド及び plots パッケージを用いた描画:restart;# サンプルデータを用意# データは [xi, yi] (i=1..n)の形で格納されているとします。dataPoints := [seq([x, evalf(sin(3.2*Pi*x))+rand()/10.0^12], x=0.0..1.0,0.05)];

dataPoints := 0., 0.3957188605 , 0.05, 0.6748934905 , 0.10, 0.8667520959 , 0.15,1.798214213 , 0.20, 1.332379109 , 0.25, 1.430407937 , 0.30, 0.5376195188 , 0.35,0.6282926615 , 0.40, K0.3841049356 , 0.45, K0.2876800616 , 0.50, K0.1780435362 ,0.55, 0.0460691875 , 0.60, K0.1421828324 , 0.65, 0.6451026107 , 0.70, 1.629460457 ,0.75, 1.161992946 , 0.80, 1.732359323 , 0.85, 1.225257639 , 0.90, 1.104727173 , 0.95,

0.2045113572 , 1.00, 0.0279468181# plot コマンドに style=point オプションを与えて描画plot(dataPoints, style=point);

Page 26: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

> >

> >

(4.4.2)(4.4.2)

> >

0.2 0.4 0.6 0.8 1K0.2

0

0.2

0.4

0.6

0.8

1.0

1.2

1.4

1.6

# plot コマンドに style=line オプションを与えて描画plot(dataPoints, style=line);

0.2 0.4 0.6 0.8 1K0.2

0

0.2

0.4

0.6

0.8

1.0

1.2

1.4

1.6

# plots パッケージ内のコマンドで描画するため、パッケージを読込みwith(plots);

animate, animate3d, animatecurve, arrow, changecoords, complexplot, complexplot3d, conformal,conformal3d, contourplot, contourplot3d, coordplot, coordplot3d, densityplot, display,dualaxisplot, fieldplot, fieldplot3d, gradplot, gradplot3d, implicitplot, implicitplot3d, inequal,interactive, interactiveparams, intersectplot, listcontplot, listcontplot3d, listdensityplot, listplot,listplot3d, loglogplot, logplot, matrixplot, multiple, odeplot, pareto, plotcompare, pointplot,pointplot3d, polarplot, polygonplot, polygonplot3d, polyhedra_supported, polyhedraplot,rootlocus, semilogplot, setcolors, setoptions, setoptions3d, spacecurve, sparsematrixplot, surfdata,textplot, textplot3d, tubeplot# pointplot コマンドで描画

Page 27: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

> >

> >

> >

pointplot(dataPoints);

0.2 0.4 0.6 0.8 1K0.2

0

0.2

0.4

0.6

0.8

1.0

1.2

1.4

1.6

# listplot コマンドで描画listplot(dataPoints);

0.2 0.4 0.6 0.8 1K0.2

0

0.2

0.4

0.6

0.8

1.0

1.2

1.4

1.6

Statistics パッケージのコマンドを用いた描画:# Statistics パッケージの PointPlot コマンドを用いて描画with(Statistics):# PointPlot コマンドを用いる場合、x 座標の値は xcoords オプションに指定PointPlot(dataPoints[1..-1,2], xcoords=dataPoints[1..-1,1]);

Page 28: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

> >

> >

(4.4.3)(4.4.3)

0.2 0.4 0.6 0.8 1K0.2

0

0.2

0.4

0.6

0.8

1.0

1.2

1.4

1.6

# PointPlot コマンドは、同一座標に対する複数データの描画が便利に行えます。xData := [seq(i,i=0.1..1,0.1)];Data1 := [2.3,2.5,3.1,4.2,5.8,6.4,7.0,8.9,9.3,4.3];Data2 := [7.2,3.2,7.3,1.1,9.7,8.9,2.2,2.8,1.2,7.1];Data3 := [2.1,3.2,7.1,8.3,9.0,9.2,7.3,9.3,5.2,1.3];

xData := 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0Data1 := 2.3, 2.5, 3.1, 4.2, 5.8, 6.4, 7.0, 8.9, 9.3, 4.3Data2 := 7.2, 3.2, 7.3, 1.1, 9.7, 8.9, 2.2, 2.8, 1.2, 7.1Data3 := 2.1, 3.2, 7.1, 8.3, 9.0, 9.2, 7.3, 9.3, 5.2, 1.3

# PointPlot コマンドで複数データを描画する際、各データの色は自動的に設定されます。PointPlot( [Data1,Data2,Data3], xcoords=xData, gridlines=true, symbolsize=12);

0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0

2

3

4

5

6

7

8

9

Page 29: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

> >

> >

# LineChart コマンドを用いた描画LineChart( [Data1,Data2,Data3], xcoords=xData, gridlines=true, color=red..yellow, thickness=3);

0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0

2

3

4

5

6

7

8

9

# BarChart コマンドを用いた描画BarChart( [Data1,Data2,Data3], datasetlabels=xData,(a)*(b) color=black..grey);

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1.0

0 2 4 6 8 10

Statistics パッケージには、上記以外にも ScatterPlot コマンドや FrequencyPlot コマンド、SurfacePlot コマンドなどが用意されています。また、標準のプロットコマンド類(plot 等)や plots パッケージ、Statistics パッケージのいずれのコマンドでも、プロットコマンドのためのオプションを指定することが可能です。

Page 30: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

> >

(4.5.1)(4.5.1)

> >

> >

(4.5.2)(4.5.2)

> >

データの平滑化とモデルフィッティング

数値データを読み込んだ際、場合によってはデータの平滑化や特定の数式モデルへのフィッティングを行うことがあります。Maple には平滑化、フィッティングそれぞれのためのコマンドが標準で用意されています。ここでは、主に Statistics パッケージを用いた平滑化とフィッティングについて紹介します。

# Statistics パッケージを読込みrestart;with(Statistics);

AbsoluteDeviation, AgglomeratedPlot, AreaChart, AutoCorrelation, BarChart, Bootstrap, BoxPlot,BubblePlot, CDF, CGF, CentralMoment, CharacteristicFunction, ChiSquareGoodnessOfFitTest,ChiSquareIndependenceTest, ChiSquareSuitableModelTest, ColumnGraph, Correlation,CorrelationMatrix, Count, CountMissing, Covariance, CovarianceMatrix, CrossCorrelation,Cumulant, CumulantGeneratingFunction, CumulativeDistributionFunction, CumulativeProduct,CumulativeSum, CumulativeSumChart, DataSummary, Decile, DensityPlot, Discretize,Distribution, ErrorPlot, EvaluateToFloat, Excise, ExpectedValue, ExponentialFit,ExponentialSmoothing, FailureRate, FisherInformation, Fit, FivePointSummary, FrequencyPlot,FrequencyTable, GeometricMean, HarmonicMean, HazardRate, Histogram, Information,InteractiveDataAnalysis, InterquartileRange, InverseSurvivalFunction, Join, KernelDensity,KernelDensityPlot, KernelDensitySample, Kurtosis, Likelihood, LikelihoodRatioStatistic,LineChart, LinearFilter, LinearFit, LogLikelihood, LogarithmicFit, MGF, MLE, MakeProcedure,MaximumLikelihoodEstimate, Mean, MeanDeviation, Median, MedianDeviation, MillsRatio,Mode, Moment, MomentGeneratingFunction, MovingAverage, MovingMedian, MovingStatistic,NonlinearFit, NormalPlot, OneSampleChiSquareTest, OneSampleTTest, OneSampleZTest,OneWayANOVA, OrderByRank, OrderStatistic, PDF, Percentile, PieChart, PointPlot,PolynomialFit, PowerFit, Probability, ProbabilityDensityFunction, ProbabilityFunction,ProbabilityPlot, ProfileLikelihood, ProfileLogLikelihood, QuadraticMean, Quantile, QuantilePlot,Quartile, RandomVariable, Range, Rank, Remove, RemoveInRange, RemoveNonNumeric, Sample,ScatterPlot, Score, Select, SelectInRange, SelectNonNumeric, ShapiroWilkWTest, Shuffle,Skewness, Sort, StandardDeviation, StandardError, StandardizedMoment, SunflowerPlot,Support, SurfacePlot, SurvivalFunction, SymmetryPlot, Tally, TallyInto, Trim, TrimmedMean,TwoSampleFTest, TwoSamplePairedTTest, TwoSampleTTest, TwoSampleZTest, Variance,Variation, WeightedMovingAverage, Winsorize, WinsorizedMean# 白色雑音を乗せた正弦波データを用意# ここでは平均0, 分散0.25の標準分布による200個のサンプル値を用意U := Sample(Normal(0,0.25),200):xData := <seq(1..200)>:yData := <seq(sin(2*Pi*i/200)+U[i], i=1..200)>:

# 10要素間の移動平均により平滑化yDataNew := MovingAverage(yData, 10);

yDataNew :=

1 .. 191 Vectorcolumn

Data Type: float8

Storage: rectangular

Order: Fortran_order

# 元データと平滑化データ yDataNew をグラフ描画

Page 31: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

(4.5.3)(4.5.3)

> >

> >

> >

(4.5.4)(4.5.4)

g1 := LineChart([yData,yDataNew], color=red..green, symbol=point):g1;

20 40 60 80 100 120 140 160 180 200

K1.5

K1

K0.5

0

0.5

1

# フィッティングに使うモデル式を以下の変数 model に割当てmodel := a*sin(b*Pi*t/200)+c*cos(d*Pi*t/200)+e;

model := a sin 1200

b π t Cc cos 1200

d π t Ce

# NonlinearFit コマンドによりフィッティングfitModel := NonlinearFit(model, xData[1..-10], yDataNew, t);

fitModel := 0.940103586220568 sin 0.0102632372448847 π t

C0.109422264368262 cos 0.00806021247967144 π t C0.0269659201816682

# フィッティングしたモデルのグラフを作成し、元データ、平滑化データと共に描画g2 := plot(fitModel, t=xData[1]..xData[-10], color=blue, thickness=3):plots[display](g1,g2);

Page 32: Maple による - Part 1 - CYBERNET › maple › documents › pdf › example › ...plot コマンド同様に、オプション引数または GUI によってグラフの種類や軸、ライティング(照

t20 40 60 80 100 120 140 160 180

K1.5

K1

K0.5

0

0.5

1

Copyright © Cybernet Systems Co., Ltd., 2011, All rights reserved.