howto/mathematica

一行メモ

  • プロットした図を選択した状態で印刷すると、図だけを印刷できる -- 2018-12-22 (土) 15:23:25
  • 会計用表記の出力:AccountingForm -- 2019-04-15 (月) 15:49:04
  • スライダーで値を変化させてグラフを見る: -- 2019-05-01 (水) 15:15:15
    Manipulate[
     Plot[a*Sin[n*x], {x, 0, 2*Pi}],
     {{n, 1},   1, 20, Appearance -> "Labeled"},
     {{a, 1}, -10, 10, Appearance -> "Labeled"} ]
(* ちょっと複雑な例 *)
inSin[A_, w_, k_, t_, x_] := A*Sin[w*t + k*x]
outSin[A_, w_, k_, t_, x_] := A*Sin[w*t - k*x + Pi]

cwInSin[\[Rho]0_, A_, w_, k_, t_, x_] := \[Rho]0/(1 + k*A*Cos[w*t + k*x])
cwOutSin[\[Rho]0_, A_, w_, k_, t_, x_] := \[Rho]0/(1 - k*A*Cos[w*t - k*x + Pi])
cwInSinPlusOutSin[\[Rho]0_, A_, w_, k_, t_, x_] := \[Rho]0/(1 + k*A*Cos[w*t + k*x] - k*A*Cos[w*t - k*x + Pi])

Manipulate[
 Plot[{
    inSin[A, w, k, t, x], outSin[A, w, k, t, x], 
    inSin[A, w, k, t, x] + outSin[A, w, k, t, x], x,
    (cwInSin[\[Rho]0, A, w, k, t, x] - \[Rho]0), (cwOutSin[\[Rho]0, A, w, k, t, x] - \[Rho]0), (cwInSinPlusOutSin[\[Rho]0, A, w, k, t, x] - \[Rho]0), (\[Rho]0)
  },
  {x, -3, 12}, PlotRange -> {-0.74*range, 0.74*range}, 
  AspectRatio -> ratio, ImageSize -> 600,
  PlotStyle -> {
    {Red, Thickness[0.0003]}, {Green, Thickness[0.0003]}, {Blue, Thickness[0.003]}, {Black, Thickness[0.0003]},
    {Magenta, Dashed, Thickness[0.002]}, {RGBColor[0, 0.7, 0], Dashed, Thickness[0.002]}, {Cyan, Thickness[0.004]}, {Black, Dashed, Thickness[0.0003]}
  },
  PlotLegends -> {
    "inSin 入射波", "outSin 反射波", "inSin+outSin 合成波", "x 参考",
    "cw InSin 入射波の密度変化", "cw OutSin 反射波の密度変化", "cw InSin+OutSin 合成波の密度変化", "\[Rho]0 波が無いときの媒質密度"
}],
{{ratio, 0.5, "ranito(縦横比を変更)"}, 1/10, 1, Appearance -> "Labeled"},
{{\[Rho]0, 3, "媒質の密度"}, 0, 5, Appearance -> "Labeled"},
{{range, 5, "range(縦軸幅を変更)"}, 1, 20, Appearance -> "Labeled"},
{{A, 0.45, "振幅(入射波,反射波)"}, 0, 1/(2*k), Appearance -> "Labeled"},
{{w, 1, "角振動数"}, 0, 10, Appearance -> "Labeled"},
{{k, 1, "波数"}, 0, 2, Appearance -> "Labeled"},
{{t, 0.544, "時刻"}, 0, 2*Pi/w, Appearance -> "Labeled"},
ControlPlacement -> {Top}
]
  • 例 -- 2019-06-05 (水) 20:08:26
    V[x_, y_, z_] := -1/Sqrt[x^2 + y^2 + z^2]  (* クーロン引力 *)
    
    m = {V[x, y, z],
     D[V[x, y, z], x],
     D[D[V[x, y, z], x], y],
     D[D[D[V[x, y, z], x], y], z]}  (* 3階微分までの結果を、行列mに代入 *)
    
    mm = Simplify[m /. {y -> 0, z -> 0}, Assumptions -> {x > 0}]  (* 正のx軸上の結果を、行列mmに代入 *)
    {mm[[2]], mm[[1]]}  (* 確認のため∂xVとVを行列要素要素として表示 *)
    
    mm[[2]]/mm[[1]]  (* ∂xV / V の比 *)
  • DateString[] で Fri 7 Jun 2019 12:15:56 などが出力される。 Date[+9] では {2019, 6, 7, 12, 9, 34.1648988} などのリストが出力される。 -- 2019-06-07 (金) 12:16:58
  • MonomialList[式] 式の各項をリストにしたものを与える -- 2019-06-12 (水) 13:48:15
  • GridLines -> {Table[i, {i, -10, 40}], Table[i, {i, -3, 3}]} Plotでグリッド線を自分で与える -- 2019-06-12 (水) 14:42:26
  • https://reference.wolfram.com/language/ref/Plot.html?q=Plot マニュアルのリンク -- 2019-06-12 (水) 14:42:40
  • ESC hb ESC でディラック定数入力できる(hbの他にもいろいろあり) -- 2019-06-12 (水) 15:07:59
  • Magnify[ hoge, 1.5] とすると1.5倍に出来るが、印刷時は期待通りにならない。 -- 2019-06-12 (水) 15:59:07
  • メニュー→パレット、でいろいろな記号を入力できる -- 2019-06-13 (木) 11:12:32
  • コンテキスト -- 2019-06-14 (金) 19:32:46
    $Context  (*現在のコンテキスト(名前空間のようなもの)を表示する*)
    Global`   (*デフォルトで使われているコンテキストらしい*)
    変数等の正式名称は、 コンテキスト名`簡略名 である。現在のコンテキスト内の変数等であれば簡略名だけで使用可。
  • ( ..body.. )& は、純関数(名無し関数)であり、 ( ..body.. )&[x] のように使える。引数は#1,#2,...で利用可 -- 2019-06-14 (金) 19:36:35
  • 前置記法 f@x は、f[x]の意味 -- 2019-06-14 (金) 19:38:45
  • 後置記法 x//f は、f[x]の意味 -- 2019-06-14 (金) 19:39:07
  • 入力の自動フォーマットなど -- 2019-06-14 (金) 19:39:53
    • AutoIndent = False がいい
    • ShowAutoStyle = False がいい
  • 音を鳴らす -- 2019-08-01 (木) 17:49:19
    Manipulate[
     Play[Sin[2*Pi*(440 + s)*t], {t, 0, 1}],
     {{s, 0}, -100, 100, Appearance -> "Labeled"}]
  • Plotで軸目盛を書かない: Ticks -> None -- 2019-08-05 (月) 14:57:32
  • Plotで軸を書かない: Axes -> False -- 2019-08-05 (月) 14:57:49
  • DSolve -- 2019-10-02 (水) 14:28:37
    DSolve[{y''[x] + \[Lambda]* y[x] == 0, y[0] == 0, y[\[Pi]] == 0}, y[x], x] (*二階微分方程式を解いた例*)
  • Plotのオプションで、AspectRatio -> 1/4, ImageSize -> 640 により横長拡大可。 -- 2020-02-06 (木) 12:34:13
  • LogLogPlot 両方の軸とも対数のプロットをする -- 2020-03-27 (金) 13:10:42
  • Assuming[assump, Collect[aall /. {R0 -> xk - m*dx}, {dx}, Simplify]] 式aallについて、R0を置換し、dxの次数で括ったあとに、assumpの仮定の下で各項の係数を簡略化する -- 2020-08-06 (木) 11:12:15
  • BernoulliB[2*n] 2nのベルヌーイ数 -- 2020-08-26 (水) 16:51:36
  • n! nの階乗 -- 2020-08-26 (水) 16:52:19
  • n!! nの二重階乗 -- 2020-08-26 (水) 16:52:49
  • Show[a,b] a=何かのプロット, b=何かのプロット(Plot,ParametricPlotなど)のとき、両方のプロットを重ねてプロットする -- 2020-08-27 (木) 14:26:16
  • ParametricPlot, ParametricPlot3Dの例 -- 2020-08-27 (木) 17:27:00
    ParametricPlot[{
      {Re[ArcTan[4*Exp[I*\[Theta]]]], Im[ArcTan[4*Exp[I*\[Theta]]]]},
      {Re[ArcTan[2*Exp[I*\[Theta]]]], Im[ArcTan[2*Exp[I*\[Theta]]]]},
      {Re[ArcTan[1*Exp[I*\[Theta]]]], Im[ArcTan[1*Exp[I*\[Theta]]]]},
      {Re[ArcTan[1/2*Exp[I*\[Theta]]]], Im[ArcTan[1/2*Exp[I*\[Theta]]]]},
      {Re[ArcTan[1/4*Exp[I*\[Theta]]]], Im[ArcTan[1/4*Exp[I*\[Theta]]]]},
      {Re[Exp[I*\[Theta]]], Im[Exp[I*\[Theta]]]}},
     {\[Theta], 0, Pi*2}, PlotLegends -> "Expressions", 
     PlotRange -> {{-2, 2}, {-2, 2}}]
    
    ParametricPlot3D[{Re[ArcTan[r*Exp[I*\[Theta]]]], 
      Im[ArcTan[r*Exp[I*\[Theta]]]], 
      Abs[ArcTan[r*Exp[I*\[Theta]]]]}, {\[Theta], 0, Pi*2}, {r, 0, 4}, 
     AxesLabel -> {"Re", "Im", "Abs"}, 
     PlotRange -> {{-2, 2}, {-2, 2}, {0, 2}}, Mesh -> Automatic]
    
    Manipulate[
     ParametricPlot[{{Re[ArcTan[r*Exp[I*\[Theta]]]], 
        Im[ArcTan[r*Exp[I*\[Theta]]]]}, {Re[r*Exp[I*\[Theta]]], 
        Im[r*Exp[I*\[Theta]]]}}, {\[Theta], Pi/2, Pi*3/2}, 
      PlotLegends -> {"ArcTan[r*Exp[I*\[Theta]]]", "r*Exp[I*\[Theta]]"},
      PlotRange -> {{-2, 2}, {-2, 2}}], {{r, 0.5}, 0, 10, 
      Appearance -> "Labeled"}]
  • ver.12からだと、ComplexPlot, ComplexPlot3Dなどが使えるらしい -- 2020-08-27 (木) 17:41:09

ver.11では次のように代替できそうである。

(* 複素関数のプロット 2021/12/13 *)
Options[ParametricPlot3DComplex] = {
   "inc" -> 0, (* Arg[]の結果にincを足す。z^(1/n)などの単純な場合のみ、リーマン面の葉を描くのに有効だろう。0は主分枝を描く。 *)
   "pRange" -> {Automatic, Automatic, {0, 5}}, (* プロットする範囲{実軸,虚軸,Abs軸} *)
   "LogAbs" -> "LogAbs", (* プロットの縦軸を対数にするかなど, Abs, LogAbs, LogLogAbs *)
   "domainScale" -> 1(* 定義域をdomainScale倍に広げる *)
   };
ParametricPlot3DComplex[funcz_, OptionsPattern[]] := 
 Module[{myAbs, myAxesLabel},
  Switch[ OptionValue["LogAbs"],
   "LogLogAbs",{(* Log Log scale *) myAbs = Log[Log[Abs[funcz /. {z -> x + I*y}]]]; myAxesLabel = {"Re", "Im", "LogLogAbs"};},
   "LogAbs",   {(* Log scale *)     myAbs = Log[Abs[funcz /. {z -> x + I*y}]];      myAxesLabel = {"Re", "Im", "LogAbs"};   },
   _,          {(* Normal scale *)  myAbs = Abs[funcz /. {z -> x + I*y}];           myAxesLabel = {"Re", "Im", "Abs"};      }
  ];
  ParametricPlot3D[{x, y, myAbs},
   {x, -3*OptionValue["domainScale"], 3*OptionValue["domainScale"]},
   {y, -3*OptionValue["domainScale"], 3*OptionValue["domainScale"]},
   PlotRange -> OptionValue["pRange"], AxesLabel -> myAxesLabel,
   ViewPoint -> {0, 0, Infinity} (*Above*),
   ColorFunction -> Function[{px, py, pz}, Hue[(Arg[funcz /. {z -> px + I*py}] + OptionValue["inc"])/(2*Pi)]],
   ColorFunctionScaling -> False,
   MeshFunctions -> {Function[{px, py, pz}, (Arg[funcz /. {z -> px + I*py}] + OptionValue["inc"])/(2*Pi)],
                     Function[{px, py, pz}, pz]},
   PlotStyle -> Opacity[1.0], Mesh -> 10, PlotPoints -> 30, MaxRecursion -> 5, 
   Lighting -> {{"Ambient", RGBColor[.8, .8, .8]}}]]

(* 複素関数のプロットのカラーマップ *)
ParametricPlot3DComplexColormap[] := Module[{anglelist},
  anglelist = {1, 1 + I, I, -1 + I, -1, -1 - I, -I, 1 - I, 1 - I/1000};
  MatrixForm[{
    Table[Hue[N[Arg[e]/(2*Pi)]], {e, anglelist}],(*Hue[負値]は、負値に整数が加えられ0~1に範囲になるようだ*)
    Table[N[Arg[e]/(2*Pi)], {e, anglelist}],
    Table[Arg[e], {e, anglelist}]}]]

実行例

ParametricPlot3DComplexColormap[]
nowrap
ParametricPlot3DComplex[Log[z], "inc"->0, "pRange"->{Automatic, Automatic, {0, 5}},
                                "LogAbs"->"Abs", "domainScale"->1]
や
ParametricPlot3DComplex[Log[z]]

nowrap nowrap

ParametricPlot3DComplex[Exp[z], "inc"->0, "pRange"->{Automatic, Automatic, {0, 7}},
                                "LogAbs"->"Abs", "domainScale"->2.09]

nowrap nowrap

(注)黒い波線は数値計算上の問題だろう

ParametricPlot3DComplex[z, "inc"->0, "pRange"->{Automatic, Automatic, {0, 5}},
                           "LogAbs"->"Abs", "domainScale"->1]

nowrap nowrap

[c.f.] https://mathrelish.com/mathematics/singularity-in-complex-analysis

  • https://ja.wolframalpha.com/ -- 2020-08-27 (木) 17:49:49
  • 引数a,bから10a+bを計算する純関数(いわゆる名無し関数) -- 2020-08-28 (金) 11:59:22
       Function[{x, y}, 10*x + y][a, b]
       または
       (10*#1 + #2)&[a, b]
  • Showで2つのプロットをひとつに(最初のプロットに)まとめることが出来るようだ Show[Plot[Sin[1 x], {x, 0, 2 Pi}], Plot[Cos[1 x], {x, 0, 2 Pi}]] -- 2021-12-10 (金) 17:51:04
  • Manipulateの中にFor文を使うと評価が永遠と続き暴走する(ForがNullを返すためらしい)。For文を使った関数を別に定義して、その関数を呼べば暴走しない。 -- 2021-12-10 (金) 17:52:20
  • Manipulateの中にグローバル変数を使ったListPlotを書いても、評価が暴走する(コントロールはなんとか効くようだ)。 -- 2021-12-10 (金) 18:30:42
  • Total[ ( {4, 3} - {1, 2} )^2 ] #行列{...}の差のあと、各要素を2乗し、要素の和をとる計算 (4-1)^2 + (3-2)^2 -- 2022-10-06 (木) 12:25:07
  • f[a_] := a + {100, 200}; #関数定義の引数は行列でもよい -- 2022-10-06 (木) 12:30:38
    #サイズの異なる演算はブロードキャストされる(pythonと同じ)
    {1, 2} + 3*f[{1, 2}]  #{1, 2} + 3*( {1, 2}+{100, 200} )の計算
    4 + 3*f[4]            #{4, 4} + 3*( {4, 4}+{100, 200} )の計算
  • 上から、下からの極限 -- 2022-10-11 (火) 16:35:45
    Limit[Abs[x - Rx]/(x - Rx), x -> Rx]
    Limit[Abs[x - Rx]/(x - Rx), x -> Rx, Direction -> "FromAbove"] #上から
    Limit[Abs[x - Rx]/(x - Rx), x -> Rx, Direction -> "FromBelow"] #下から
  • Plotで、Abs'[x]は描かれないが、RealAbs'[x]は描かれる。 -- 2022-10-11 (火) 17:50:28
  • DegreeはPi/180である。 Tan[45 Degree] は1になる -- 2022-10-23 (日) 15:27:37
  • 数式の置き換え( /. {rule} ) -- 2022-10-26 (水) 18:03:11
    ある数式aがあるとして、Sqrt[X^2+y^2]をrに置き換えたいとき、
    a /. {Sqrt[x^2 + y^2] -> r, 1/Sqrt[x^2 + y^2] -> 1/r}
    とする(a内に 1/Sqrt[X^2+y^2] があっても、ひとつめの規則で1/rに置き換えられないため)。
  • ManipulateとPlotの典型例 -- 2023-06-05 (月) 16:31:55
    psi[x0_, px0_, w_, x_, t_] := 1/((I*t/w + 2*w)^2*Pi/2)^(1/4)
       * Exp[(t/(2*w^2) + I)/(t^2/w^2 + 4*w^2)*I*(x - x0 - px0*t)^2 + I*px0*(x - x0 - px0/2*t)]
    rho[x0_, px0_, w_, x_, t_] := 1/Sqrt[Pi/2*t^2/w^2 + 2*Pi*w^2] * Exp[-2/(t^2/w^2 + 4*w^2)*(x - x0 - px0*t)^2]
    Manipulate[
      Plot[
        { rho[2, px0, w, x, t],
          Re[psi[2, px0, w, x, t]],
          Im[psi[2, px0, w, x, t]] },
        {x, 0 - 1, 15 + 1},
        ImageSize -> 700, GridLines -> Automatic, PlotLegends -> "Expressions",
        PlotRange -> {-0.8, 0.8}
      ],
      { {px0, 2.1602464}, 0, 5,   Appearance -> "Labeled"},
      { {w, 0.5},         0, 1.5, Appearance -> "Labeled"},
      { {t, 0},           0, 5,   Appearance -> "Labeled"}
    ]
  • Plotでタイトルを付けるには、PlotLabel->"hogehoge" -- 2023-06-13 (火) 11:57:23
  • GraphicsGridを使って、Plotをそろえる、かつ余分な空白を入れない、には? -- 2023-06-13 (火) 13:03:18
  • 波の重ね合わせ -- 2023-06-22 (木) 12:51:58
    wd[k_] := k^2/2
    (*wd[k_]:=k*)
    wav1[k_, w_, x_, t_, e_] := Im[Exp[I*(k*x - w*t + e)]]
    wav2[k_, w_, x_, t_, e_] := Im[Exp[I*(k*x - w*t + e)]]
    Manipulate[
     Plot[{
       wav1[k1, wd[k1], x, t, 0],
       wav2[k2, wd[k2], x, t, e2],
       wav1[k1, wd[k1], x, t, 0] + wav2[k2, wd[k2], x, t, e2]
       },
      {x, -50, 50}, PlotRange -> {-5, 5}, ImageSize -> 600,
      PlotStyle -> {{Red, Thickness[0.0003]}, {Blue, Thickness[0.0003]}, {Green, Thickness[0.003]}},
      PlotLegends -> {"wav1", "wav2", "wav1+wav2"}],
     {{k1, 1.0}, -5, 5, Appearance -> "Labeled"},
     {{k2, 0.8}, -5, 5, Appearance -> "Labeled"},
     {{e2, 0.0}, -Pi, Pi, Appearance -> "Labeled"},
     {{t, 0}, 0, 100, Appearance -> "Labeled"}
     ]
  • FindRoot[f, {x, x0}] #x=x0から始めてfの数値根を求める。f==0と指定してもよい。x0がひとつだけの場合はニュートン法が使われるらしい(その他、詳細はマニュアル参照)。 -- 2023-10-19 (木) 14:19:54
  • リストxにある座標をx軸上にプロットする -- 2023-11-25 (土) 21:08:08
    MAX = 5;
    MAXIM = 5;
    x = {};  (*空リスト*)
    a = 1;
    x = Append[x, a];
    For[i = 0, i < MAXIM, i++,
      (*Print[Sort[x]];*)
      y = x;
      For[j = 1, j <= Length[x], j++,
         a = x[[j]];
         y = Append[y, (1/2)*a];
         y = Append[y, (21/10)*a];
         y = Append[y, (3/10)*a];
      ];
      x = DeleteDuplicates[y];
    ](*プロットする座標作成*)
    (*
    For[l=0,l<MAX,l++,
      For[m=0,m<MAX,m++,
        For[n=0,n<MAX,n++,
          x=Append[x,(1/2)^l*(21/10)^m*(3/10)^n*a]; (*プロットする座標作成*)
        ];
      ];
    ];
    *)
    (*N[x]*)
    N[{Min[x], Max[x]}]
    ListPlot[
      Transpose[{x, ConstantArray[0, Length[x]]}], (* (x,0)座標のリスト作成*)
      GridLines -> {x, {}}, GridLinesStyle -> Directive[Blue],
      PlotStyle -> PointSize[Medium],
      AspectRatio -> 1/10, AxesOrigin -> {-0.5, 0},
      AxesStyle -> Directive[Black, 12],
      Ticks -> {Automatic, None},
      PlotRange -> {{-1, Min[{20, Ceiling[Max[x]]}]}, Automatic},
      ImageSize -> Full
    ]
    selectedList = Select[x, 9 <= # <= 11 &];
    selectedList
    nowrap
  • コメントの (* *) は入れ子が可能のようだ。 -- 2023-11-25 (土) 22:10:43
  • 連立方程式を解く x=LinearSolve[ A, b ] (* Aは係数行列、bは右辺のベクトル、xは解ベクトル *) -- 2023-12-01 (金) 12:31:58
  • 固有値、固有ベクトル、行列式、行列のランク、単位行列
    • 固有値、固有ベクトルを求める Eigenvalues[A] , Eigenvectors[A] (* Aは行列 *)
    • 一般化固有値方程式の場合は、重なり行列をSとして Eigenvalues[{A,S}] , Eigenvectors[ {A,S} ]
    • 行列式を求める Det[A]
    • 行列のランクを求める MatrixRank[A]
    • 4x4の単位行列を作る IdentityMatrix[4]
      例)
      A = { {2, 0, 1, 0},
            {0, 2, 0, 1},
            {1, 0, 2, 0},
            {0, 1, 0, 2} };
      Eigenvalues[A]          (* A c = c w *)
      Eigenvectors[A]         (* c *)
      w=1                     (* w, Aのひとつの固有値 *)
      Det[ A - w*IdentityMatrix[4] ]         (* 永年行列式にw=1を代入した値 *)
      MatrixRank[ A - w*IdentityMatrix[4] ]  (* 〃の行列のランク(階数) *)
  • Solveで桁を増やす -- 2023-12-16 (土) 12:50:56
    a=Solve[x^2+x-1.0==10*^-4]
      (* {{x->-1.61848},{x->0.61848}} と表示 *)
      (* N[a,16]では桁は増えない、SetPrecision[a,16]として精度を変更しないといけないらしい *)
    SetPrecision[Solve[x^2+x-1.0==10*^-4],16]
      (* {{x->-1.618481112938435},{x->0.618481112938434885}} と16桁の精度を持つように作成 *)
    x/.SetPrecision[Solve[x^2+x-1.0==10*^-4],16][[1]]
      (* -1.618481112938435 と、ひとつ目の値だけ取り出す *)
  • Reduceで不等式を解く -- 2023-12-16 (土) 13:01:23
    SetPrecision[Reduce[x^2+x-1.0<0*^-4] ,16]
  • プラスマイナスは \[PlusMinus] で入力できる。 -- 2024-04-23 (火) 16:14:23

マニュアル・リンクなど

いろいろなこと

適度な印刷方法

「一般的な場合」
ファイル→印刷設定→印刷オプションで、
  セルのブラケットを印刷と、領域マークを印刷のチェックをする、
  マージン設定で上下左右の余白を5mmにする(左右の余白が大きすぎるため)
→A3でpdfとして印刷
→AcrobatでA4に縮小印刷
「長い数式を印刷する場合」
ファイル→印刷設定→印刷用環境
  →Condensedを選ぶ(表示画面と似た感じで、やや小さめのカラーで印刷できる)
  (Printoutだと白黒で行間がもう少し狭めになる)
ファイル→印刷設定→印刷オプション
  →マージンを 20mmに小さくする。
A2サイズ横で、pdf出力してから、pdfをA4サイズに縮小して印刷する。

定数

虚数単位    I
ネイピア数  E
円周率      Pi
無限大      Infinity

関数

Exp, Log
Sqrt
Abs, Arg
Sign
Sin, Cos, Tan

ギリシャ文字の入力

\[Alpha] で α になる
\[Theta] で θ

下付き、上付き、二次元式

下付き文字をつけたい文字を選択か文字の直後で、Ctrl+マイナス(-) を押す→プレースフォルダ
があらわれる
上付き文字をつけたい文字(べき乗になる)を選択し、Ctrl+^ を押す →プレースフォルダ
分子の入力後、Ctrl+/ で分数の分母を二次元式で入力できる、らしい
Ctrl-Enter で列ベクトルの次の行の入力ができる

表示形式

InputForm["hogehoge"]    # 数式hogehogeを、一次元の形式(通常の入力に使われる形式)で表示
OutputForm["hogehoge"]   # 通常の画面に出る形式で表示

StandardForm["hogehoge"] # see manual(たぶん通常はOutputFormと同じ)
FullForm["hogehoge"]     # see manual(たぶん使うことはない)
Style[ hogehoge, Smaller ]  やや小さいフォントで出力する

近似値、四捨五入など

N[ hogehoge, [桁数] ]//InputForm
   # hogehogeを数値として表示する。
   # //InputFormを付けると一次元の入力形式で得られるが、
   # 指定した桁数以上(25~50桁など)が出力される。
   # 10のべき乗部分は、`桁数*^10 となる。
Round[...]    #数値で、四捨五入して出力
Floor[...]    #数値で、切り捨てして出力
Ceiling[...]  #数値で、切上げして出力

極限

Limit[expr, x->a]  #exprのx->aの極限を計算する

テーラー展開

Series[ expr, {x,x0,n} ]             #x=x0でn次までexprをテイラー展開する
Normal[ Series[ expr, {x,x0,n} ] ]   #余剰項を除く形を得る

微分、積分

D[]           微分。高階微分はmanualをみよ
Integrate[ ]  積分。重積分はmanualをみよ

仮定付き積分の例) Integrate[s*Exp[-s^2/2], {s, 0, x}, Assumptions-> {x > 0}]

方程式を解く

Solve[ 式, 解く変数 ]

x /. Solve[ 式, x ]  (*xの解のみのリスト出力する
                      (Solveは変換規則のリストとして解を出力する。そのため、余分な所を消すのに置換表示している)*)
例)
Part[z /. Solve[1 - 3*z^2 + 2*z^3 == x, z], 1 ;; 3]  (*方程式をzについて解き、解のみを1~3個出力する*)

シンボルの説明やオプションを表示する

?hoge          # hogeの説明を表示
?*hoge         # *hogeの関数名一覧を表示(ワイルドカードも利用可)
Options[hoge]  # hoge関数で利用できるオプションの一覧表示

結果を出力しない

hoge;          #式hogeの末尾に、セミコロンを付ける

置換表示、変換規則

/.    置換表示
->    変数規則

      例) x^2 /. x -> 2

仮定

仮定の作り方

assump = Assumptions -> {x0 ∈ Reals && px0 ∈ Reals &&
                         t >= 0 && w > 0 && x ∈ Reals && k ∈ Reals }
などする。そして、
  Integrate[ func[x], {x,-Infinity,Infinity}, assump ]
  Simplify[ func[x], assump ]
で利用する。

大域的な仮定の設定

Simplify, Refine Integrate等で使われる。
$Assumptions この値がTrueのとき大域的な仮定は無い。

設定例)
$Assumptions = {x \[Element] Reals && y \[Element] Reals && 
  z \[Element] Reals && Rx \[Element] Reals && Ry \[Element] Reals && 
  Rz \[Element] Reals && A > 0 && a > 0 && b > 0 && r >= 0}

$Assumptions = True
で設定を無くす

初期化やクリア

ClearAll["Global`*"]  #何もかもをクリアする

ClearAll[x]           #変数xの何もかもをクリアする

詳しくはmanualをみよ。
  Clear[ hoge ]     値、定義をクリア
  Clear["Global`*"] 現行のセッションで作成された値、定義をすべてクリア
  ClearAll[ hoge ]  記号の値、定義、属性、メッセージ、デフォルト設定をクリア
  Remove[ hoge ]    シンボルの名前が認識されないように、完全に除去
<< Utilities`CleanSlate` #この2行で、Clear["Global`*"]をし、In/Out番号も初期化し、
CleanSlate[]              #開放メモリ量のレポートも出すらしい

?Utilities`CleanSlate`*  #詳細はこちら

行列

行列を作る

m={
   {1,2,3},
   {4,5,6}
  }
Table[ x^i*y^j, {i, 1, 3}, {j, 1, 2}]
m=DiagonalMatrix[{1,2,3,4}]    (* 対角行列 *)

行列要素を取り出す

m[[1,All]]  (* mの1行目だけ取り出す。mは2次元配列より大くても同様に指定可 *)
Part[m,1,All]  (* 〃 *)
m[[1;;1, 1;;2]]  (* 1-1行目x1-2列目からなる部分行列 *)

行列表記やテーブルで表示

m // MatrixForm  (* 行列表記で出力 *)
m // TableForm   (* テーブルで出力,要素を並べる *)
または
MatrixForm[m]
TableForm[m]

なお、//MatrixForm をつけた形で行列を、変数に代入すると、
その後の行列積などが計算できない。

行列の積、転置、逆行列

A.B             (* 積 *)
Transpose[A]    (* 転置 *)
Inverse[A]      (* 逆行列 *)
MatrixPower[A,n]  (* 行列Aの n乗 *)

A*B  (* 行列A,Bの各要素をお互いに掛ける *)
A^n  (* 行列Aの各要素を n乗する *)

ベクトル

ベクトルは、1行または1列の行列として扱う。

ノルム

Norm[v]    ベクトルのノルム、行列のノルム(詳細は調べよ)

直積(テンソル積)の例

AB = {
   {a11  b11, a11 b12, a12 b11, a12 b12},
   {a11  b21, a11 b22, a12 b21, a12 b22},
   {a21  b11, a21 b12, a22 b11, a22 b12},
   {a21  b21, a21 b22, a22 b21, a22 b22}
   };
CC = Transpose[{{C0' C0'', C0' C1'', C1' C0'', C1' C1''}}];
A = {
   {a11, a12},
   {a21, a22}
   };
B = {
   {b11, b12},
   {b21, b22}
   };
C'  = Transpose[{{C0', C1'}}];
C'' = Transpose[{{C0'', C1''}}];
AB // MatrixForm
CC // MatrixForm
A  // MatrixForm
B  // MatrixForm
C'  // MatrixForm
C'' // MatrixForm

(*A \[TensorProduct] B //MatrixForm*) (* \[ TensorProduct ] で入力可 *)
(* TensorProduct[A,B] //MatrixForm *)
KroneckerProduct[A, B] // MatrixForm (*直積*)

lhs = KroneckerProduct[A, B].CC;
lhs // MatrixForm
rhs = Expand[KroneckerProduct[(A.C'), (B.C'')]];
rhs // MatrixForm
lhs - rhs // MatrixForm
(*他*)
Dimensions[A]
Dimensions[A // MatrixForm]

グラフ描画

Plot

(*例1*)
Plot[{
  b*(Exp[k*(b - a)*t] - 1)/(b/a*Exp[k*(b - a)*t] - 1) /. { a -> 1, b -> 0.5, k -> 1},
  b*(Exp[k*(b - a)*t] - 1)/(b/a*Exp[k*(b - a)*t] - 1) /. { a -> 1, b -> 2, k -> 1},
  a*a*k*t/(a*k*t + 1) /. { a -> 1, k -> 1}
  },
  {t, 0, 10} ] 
(*例2*)
omega1 = 0.00288;
E01 = 7.76; (*V/Ang*)
Eenv[t_] := Which[ 
  t < 2 Pi / omega1, omega1*t/2/Pi,
  t < 4 Pi / omega1, 2 - omega1*t/2/Pi,
  True, 0]
Elaser1[t_] := Eenv[t]*(E01*Sin[omega1*t])
Plot[{Elaser1[t], Eenv[t], E01*Sin[omega1*t]}, {t, 0, 2500*100/10}, 
 PlotLegends -> "Expressions",
 PlotRange -> {-10, 10}, 
 GridLines -> Automatic,
 AxesLabel -> {"au(time)", "V/Ang"}, 
 PlotStyle -> {Thickness[0.01], Thickness[0.01], {Dashed, Thickness[0.0001]}}]
(*例3*)
omega2 = 0.00144;
E02 = 2.74 ;(*V/Ang*)
Elaser2[t_] := (Sin[omega2*t/16])^2*(E02*Cos[omega2*t])
Plot[{Elaser2[t], (Sin[omega2*t/16])^2, E02*Cos[omega2*t]}, {t, 0, 2500*100/3},
 PlotLegends -> "Expressions",
 PlotRange -> {-3, 3}, 
 GridLines -> Automatic,
 AxesLabel -> {"au(time)", "V/Ang"}, 
 PlotStyle -> {Thickness[0.01], Thickness[0.01], {Dashed, Thickness[0.0001]}}]

線の太さ指定

PlotStyle -> { Thickness[0.005] }    #0.005がデフォルトに近い

凡例の表示

PlotLegends -> "Expressions"

線を太くして破線にする

PlotStyle -> { Thickness[0.01],
               Thickness[0.01],
               {Dashed, Thickness[0.005]},
               {Dashed, Thickness[0.005]} }  #4本分指定

線の色の指定

PlotStyle -> { {Blue, Thickness[0.01]},
               {Orange, Thickness[0.01]},
               {Blue, Dashed, Thickness[0.005]},
               {Orange, Dashed, Thickness[0.005]}}

縦横のスケールを同じにしてグラフを描きたい

例えば、
Plot[ {0.3*Sin[x],x},
      {x, -3, 12}, PlotRange -> {-0.74, 0.74}, AspectRatio -> 1/10,
      ImageSize -> Full ]
  # 横軸幅 15、縦軸幅 1.5、縦横比が 1.5/15=1/10なので、スケールは同じくなる。
  # ImageSize->Fullを付けるとウィンドウ幅に合わせて拡大されたグラフが描かれる。

Plot3D

(* z^(1/3)=Exp[1/3 Log[Abs[z]] + I/3 Arg[z]+I/3* 2 m Pi
   where -Pi<=Arg[z]<Pi and m=0,1,2 のリーマン面を描く *)
Plot3D[ {
    Arg[( x + I*y)^(1/3)] + 0* 2 Pi /3, (*m=0,mathematicaでの主値の範囲*)
    Arg[( x + I*y)^(1/3)] + 1* 2 Pi /3, (*m=1*)
    Arg[( x + I*y)^(1/3)] + 2* 2 Pi /3  (*m=2*)
  },
 {x, -2, 2}, {y, -2, 2},
 AxesLabel -> Automatic,       (*軸ラベルを表示*)
 PlotLegends -> "Expressions"  (*凡例表示。ひとつだと出ない*),
 LabelStyle -> Directive[Bold, Medium], (*軸ラベルを太く大きく*)
 Mesh -> 3,
 PlotStyle -> {Opacity[0.8]},    (*透過度。色も可*)
 ExclusionsStyle -> {None, Red}, (*値が飛ぶ所を描かない・端を赤色にする*)
 Ticks -> {Automatic, Automatic, {-Pi, -Pi/2, 0, Pi/2, Pi, 3/2 Pi}}, (*軸目盛*)
 ViewPoint -> {-5, -3.4, 3}*1000
]

関数の定義

z[r_, t_] := r Exp[ I t ]
S[n_, x_] := Which[ n == 0, Which[ x < -1, 0,
                                   x < 0, 1 - 3*x^2 - 2*x^3,
                                   x < 1, 1 - 3*x^2 + 2*x^3,
                                   True, 0],
                    n == 1, Which[ x < -1, 0,
                                   x < 0, x + 2*x^2 + x^3,
                                   x < 1, x - 2*x^2 + x^3, 
                                   True, 0]] 
  • 上のS[n,x]の微分をプロットしたい場合は(:=と=のいずれの場合でも)、 Plot[ {S[0,x], D[S[0, t], t] /. {t -> x}}, {x, -2, 2}] とする。
myarg[ a_ ] := Module[ {rcd = a}, (* ローカル変数を定義・初期化 *)
   While[rcd >=  Pi, rcd = rcd - 2 Pi];
   While[rcd < - Pi, rcd = rcd + 2 Pi];
   rcd];

式展開やまとめ

基本

Expand    # 展開
Factor    # 因数分解
Cancel    # 約分
Simplify[ hoge, [assump]]      # 式hogeを簡単にする。assumpで仮定を指定できる。

FullSimplify[ hoge, [assump]]  # 初等関数と特殊関数を含む式hogeを簡単にする。

いろいろな展開・簡約のシンボル

Expand[expr]        式の積とベキ乗の項を展開
ExpandAll[expr]     すべての項にExpandを適用
Factor[expr]        因子の積の形に変換
Together[expr]      通分し単一分数にまとめる
Apart[expr]         簡単な形の分母を持った複数の分数項に展開
Cancel[expr]        分母と分子の共通因子を約分する
Simplify[expr]      式 exprに代数変形を試し,最も短い式の形を探す
{Numerator[expr], Denominator[expr]}    式 exprの分子と分母のリストを作る

Collect[expr,x]     xの同じ次数の項をまとめる
FactorTerms[expr,x] xに依存しない因子を取り出す

TrigExpand[expr]     三角関数の式を項の和の形に展開
TrigFactor[expr]     三角関数の式を項の積の形に分解
TrigReduce[expr]     整数倍の角度を用いて三角関数を簡約
TrigToExp[expr]      三角関数を指数関数に変形
ExpToTrig[expr]      指数関数を三角関数に変形
FunctionExpand[expr] 特殊関数やその他の関数を展開
ComplexExpand[expr]  すべての変数が実数からなるとした上で式を展開
PowerExpand[expr]    例えば(xy)^p を x^p y^p に展開する

For文

(例)
NN = 10;
For[j = 0, j < NN, j++,
  a = 1 - ((j - 1/2*(NN - 1))/(1/2*(NN + 1)))^2;
  Print["j=", j, " a=", N[a, 3]];
];

添付ファイル: fileListPlot_data_on_x_axis.png 31件 [詳細] fileParametricPlot3DComplex_z_2.png 48件 [詳細] fileParametricPlot3DComplex_z.png 42件 [詳細] fileParametricPlot3DComplex_Exp_z_2.png 46件 [詳細] fileParametricPlot3DComplex_Exp_z.png 47件 [詳細] fileParametricPlot3DComplex_Log_z_2.png 41件 [詳細] fileParametricPlot3DComplex_Log_z.png 140件 [詳細] fileParametricPlot3DComplexColormap.png 271件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2024-04-23 (火) 16:14:24 (24d)