按键
运行: Shift + Enter
矩阵换行: Ctrl + Enter
换行: Alt + Enter
大小写
区分大小写.
首字母大写, 如 Sin[x]
, I
, E
.
括号
函数用中括号, 中括号前可以有空格.
指定优先级一律使用小括号.
函数
F[f]
等价于 f // F
F[x, t]
等价于 x // F[#, t] &
符号
末位加分号: 不显示输出结果.
逗号前也可以加分号.
注释
1(* 注释 *)
2(*
3注释
4*)
Alt + /
帮助文档
xxxxxxxxxx
21?Simplify
2Options[Simplify]
清空输入
法一: Ctrl + A 然后 Delete.
法二: 运行下述代码
xxxxxxxxxx
41cls := (SelectionMove[InputNotebook[], All, Notebook];
2FrontEndExecute[
3FrontEndToken["Clear"]]);
4Save[FindFile["init.m"], cls];
删除 cls 函数
xxxxxxxxxx
11SystemOpen[FindFile["init.m"]]
清楚全局变量
xxxxxxxxxx
11Clear["Global`*"]
转为近似值
xxxxxxxxxx
11N[1/3]
逻辑运算
If 作为一个函数, 第二个参数为正确时的返回值, 第三个参数为错误时的返回值.
非零数字不代表 True, 0 不代表 False.
xxxxxxxxxx
11If [t > 1 && t > 2, 1, 2]
Esc 键: 数学 2D 格式.
百分号
xxxxxxxxxx
31% (* 引用上一次结果 *)
2%% (* 引用上上一次结果 *)
3%5 (* 引用第 5 次结果 *)
格式排版: Alt + 1-7
1-4 次方程
x1Solve[a x^2 + b x + c == 0, x]
2
3Factor[a x^2 + b x + c]
4Expand[(a + 1)(a - 2)]
方程组
xxxxxxxxxx
41Solve[{
2 a t^2 + m t + c == 0,
3 m^2 - 1 == 0
4}, {t, m}]
求导
xxxxxxxxxx
11D[Log[t] * E^(t^2 + Sin[t^t]), t]
积分 (注意是 Integrate
, 不是 Integral
)
xxxxxxxxxx
11Integrate[1 / (a t^2 + b t + c), t]
泰勒展开
在 0 处展开前十项
xxxxxxxxxx
21Series[1 / (1 - x), {x, 0, 10}]
2Series[1 / (1 - x), {x, 0, 10}] // InputForm
求极限
xxxxxxxxxx
11Limit[Sin[x] / x, x -> 0]
定义函数
xxxxxxxxxx
11fun[t_] := t^2 - t
解微分方程
xxxxxxxxxx
11DSolve[D[f[x], x] + x^2 - x + f[x] == 0, f[x], x]
xxxxxxxxxx
31DSolve[{
2 D[f[x], x] + x^2 - x + f[x] == 0
3}, f[x], x]
列表与矩阵
创建
xxxxxxxxxx
91A = Table[
2 3 i + j - 3,
3 {i, 1, 3},
4 {j, 1, 3}
5]
6(* A = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} *)
7M = A // MatrixForm
8
9B = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}}
例子
列表每行的元素的个数可以不同, 也可以是多维数组.
xxxxxxxxxx
61test = Table[
2 i + j + k,
3 {i, 1, 4},
4 {j, 1, i},
5 {k, 1, 3}
6]
使用函数来更方便地定义:
xxxxxxxxxx
41Table[
2 If[i == j + 1 || i == j - 1, 1, 0],
3 {i, 1, 10}, {j, 1, 10}
4] // MatrixForm
指标
xxxxxxxxxx
81A[[1]] (* {1, 2, 3} *)
2A[[1]][[2]] (* 2 *)
3M[[1]] (* 即 A *)
4M[[1]][[2]] (* {4, 5, 6} *)
5
6A[[1, 2]] (* 2 *)
7A[[;;, 3]] (* {3, 6, 9} *)
8(* 在 matlab 里是 A[;, 3] *)
长度
xxxxxxxxxx
51A // Length (* 矩阵有 3 行 *)
2A[[1]] // Length (* 一行有 3 列 *)
3A[[1]][[1]] // Length (* 0, 单个元素非列表 *)
4M // Length (* 有 1 个元素, 即 A *)
5M[[1]][[1]] // Length (* 一行有 3 列 *)
运算
xxxxxxxxxx
51A.B (* 矩阵乘法 *)
2Eigenvalues[A] (* 特征值 *)
3Det[A] (* 行列式 *)
4Transpose[A] (* 转置 *)
5Inverse[A] (* 逆矩阵 *)
替换
xxxxxxxxxx
71test = Table[c[i + j], {i, 1, 3}, {j, 1, 3}]
2test /. c -> f
3test /. c[p_] -> p
4test /. {c[2] -> 2, c[3] -> 3}
5test //. c[x_] -> If[x < 3, c[x + 1] + b[x], b[x]]
6
7ReplaceAll[test, c[p_] -> p]
xxxxxxxxxx
31y[1, c, 3, c, 51, 3] /. y[n__] -> {n}
2y[1, c, 3, c, 51, 3] /. y[n1__, c, n2__] -> y[n1, n2]
3y[1, c, 3, c, 51, 3] /. y[n1___, c, n2___] -> y[n1, n2]
输入输出
xxxxxxxxxx
31SetDirectory[NotebookDirectory[]]; (* 设置目录为当前笔记本的目录 *)
2Export["../t.m", t] (* 将 t 的值输出到上一级目录的 t.m 文件中 *)
3x = Import["../t.m", t] (* 导入数据, 并赋给 x *)
xxxxxxxxxx
111Do[
2 t[i] = i;
3 t[i] = t[i // Mod[#, 3] &] - t[i];
4, {i, 0, 3}]
5Table[t[i], {i, 0, 3}]
6
7SetDirectory[NotebookDirectory[]];
8Do[
9 Export["t" <> ToString[i] <> ".m", t[i]];
10 Print[{i, t[i]}]
11, {i, 0, 3}]
图像
xxxxxxxxxx
31plot sin x (* 自由格式输入 *)
2
3Plot[Sin[x], {x, 0, 10}]
分式操作
xxxxxxxxxx
71Denominator[F[x] / G[x]] (* 提取分式的分母 *)
2Numerator[F[x] / G[x]] (* 提取分式的分子 *)
3ExpandDenominator[F[x] / G[x]] (* 展开分母 *)
4ExpandNumerator[F[x] / G[x]] (* 展开分子 *)
5Expand[F[x] / G[x]] (* 展开整个分式 *)
6Apart[F[x] / G[x]] (* 化为部分分式 *)
7
流程
If
xxxxxxxxxx
81If[condition, t, f]
2If[condition, t, f, u]
3
4If[a < b, 1, 0] (* a < b 则为 1, 否则为 0 *)
5If[a < b, 1, 0, Indetermintate] (* 不确定条件则为 Inderterminate *)
6If[TrueQ[a < b], 1, 0] (* 强制返回布尔值 *)
7
8abs[x_] := If[x < 0, -x, x]
Swich
xxxxxxxxxx
41Switch[expr,
2 form1, value1,
3 form2, value2, ...
4]
Which
xxxxxxxxxx
41Which[
2 test1, value1,
3 test2, value2, ...
4]
Piecewise
xxxxxxxxxx
41Piecewise[{
2 {val1, cond1},
3 {val2, cond2}, ...
4}, val]
Condition
xxxxxxxxxx
11lhs := rhs /; test
While
xxxxxxxxxx
161While[test, body]
2(* 可用 Print[] 输出, 注意用逗号分隔 test 和 body, 用分号分隔语句 *)
3
4n = 1;
5While[
6 n < 4,
7 Print[n];
8 n++
9]
10(*
11 n++, ++n, n += 1 等都是合法的,
12*)
13While[
14 n++ < 4,
15 Print[n]
16]
Do
xxxxxxxxxx
41Do[expr, n]
2Do[expr, {i, imin, imax, di}]
3Do[expr, {i, {i1, i2, ...}}]
4Do[expr, {i, imin, imax}, {j, jmin, jmax}]
For
xxxxxxxxxx
11For[start, test, incr, body]
Nestwhile
xxxxxxxxxx
51NestWhile[f, expr, test]
2(*
3 对 expr 反复应用 f, 直到 test 不成立,
4 即 f[f[f...f[expr]]]
5*)
TakeWhile
xxxxxxxxxx
21TakeWhile[list,crit]
2(* 列出 list 中满足 crit[ei] 的元素 ei *)
LengthWhile
xxxxxxxxxx
21LengthWhile[list, crit]
2(* 找出 list 中使 crit[ei] 为 True 的连续元素 ei 的数量 *)
Nest
xxxxxxxxxx
21Nest[f, expr, n]
2(* f[f[f...f[expr]]] *)
Select
xxxxxxxxxx
21Select[list, crit] (* crit[ei] 为真的所有元素 ei *)
2Select[list, crit, n] (* 前 n 个元素 *)
判断
xxxxxxxxxx
81NumberQ[expr]
2IntegerQ[expr]
3EvenQ[expr]
4OddQ[expr]
5Number[expr]
6BooleanQ[expr] (* 为 True 或 False 时返回 True *)
7TrueQ[expr] (* 为 True 时返回 True, 其它返回 False *)
8SameQ[x, y] (* 等价于 x === y *)
声明范围
xxxxxxxxxx
41Element[x, dom]
2x \[Element] Reals (* 也可用于测试 *)
3
4Element[x1|x2|..., dom]