Mathematica学习笔记

Mathematica学习笔记

mathematica 使用总结:

1 基础知识:

(1)从1开始编号输入
$Line = 1;
(2)函数的另一种表达形式
{{1, 2, 3, 4}, {5, 6, 7, 8}} // MatrixForm
(3)跳出死循环
如不小心进入死循环,可以采用快捷键Ctrl+C强行中断。
(3)给出随机数
d = RandomInteger10, 5

2 动态图绘制

(1) 清除变量
X=5;
Clear[x]
(2)清除函数
f[x_] := x^2
Clear[f]
(3) 绘制参数图:
x[t_] := Cos[t] - Cos[80 t] Sin[t];
y[t_] := 2 Sin[t] - Sin[80 t];
r[t_] := {x[t], y[t]};
ParametricPlot[r[t], {t, 0, 2 Pi}, Axes -> False]
(4)绘制动态图
(w=0.0248;
bc=85;
)
h2 = Table[
Plot[Cos[2 Piwt]Cos[2 Pix/bc], {x, -400, 400},
PlotRange -> {-1.5, 1.5}, PlotLabel -> 驻波演示,
FillingStyle -> Green, Filling -> Axis], {t, 0, 100}];
ListAnimate[%]
Export[“h:\mathematica\x9.gif”, h2]

(5)绘制动态图并输出
w = 0.0248;
w1 = 0.042;
b = 2.1;
b1 = 0.0603;
h4 = Table[
Plot[Cos[wt - bx]Cos[w1t - b1x], {x, -50, 50},
PlotRange -> {-1.5, 1.5}, PlotLabel -> 包络波 - 群速度,
FillingStyle -> Green, Filling -> Axis], {t, 0, 400}];
ListAnimate[%];
Export[“h:\mathematica\x15.gif”, h4];
(6)绘制摆线
Animate[ParametricPlot[{t-Sin[t],1-Cos[t]},{t,0,t0},PlotRange→{{0,10Pi},{0,2}}],{t0,0,10Pi}]
(7)绘制参数方程的gif:
dat = Table[
ParametricPlot[{ 2 Sin[t], Cos[t]}, {t, 0, t0},
PlotRange -> {{-2.5, 2.5}, {-1.5, 1.5}},
AxesLabel -> {x[t], v[t]}], {t0, -2
Pi, 2Pi, .2}];
Export[“h:\mathematica\oval.gif”, dat]
(8)绘制参数方程的三维gif:
aa = Table[
ParametricPlot3D[{u
Cos[r], uSin[r], u^2}, {r, 0, 2Pi}, {u, 0,
1}, ViewPoint -> 3 {Cos[t] Sin[t], Cos[t]^2, Sin[t]},
SphericalRegion -> True, Axes -> True, PlotStyle -> Axis,
AxesLabel -> {x[t], y[t], z[t]}], {t, 0, 4 Pi, 0.1}];
Export[“h:\mathematica\curve2.gif”, aa]
(9)函数自定义
g[x_] := Sin[x];
Plot[g[x], {x, -10, 10}]
(10)参数绘图
x[t_] := Sin[t];
y[t_] := Cos[t];
(r[t_]:={x[t],y[t]};)
ParametricPlot[{x[t], y[t]}, {t, 0, Pi}]
(11)隐函数绘图
Clear[f, g]
f[x_, y_] := Exp[y] + x*y + x^2 - Exp[1];
ContourPlot[f[x, y] == 0, {x, -10, 10}, {y, -10, 10}]
(12)For循环结构
For[start,test,incr,body]
For[i = 0, i < 4, i++, Print[i]]
For[i = 3, i <= 10, i++, t = i^2; Print[t]]
sum = 0;
For[i = 1, i <= 10, i++, sum = sum + i^2; Print[“sum=”, sum]];
(13)Do循环结构
Do[expr,n]
Do[expr,{ i, max}]
Do [expr,{i,imin,imax}]
Do[Print[n^2], {n, 4}]
sum = 0;
Do[sum += i^2; Print[sum], {i, 1, 10}]

3 快捷键

(1) 分式
Fn+PgUp/PgDn可以快速翻页
  Ctrl+/(斜杠分式)
 
 (3) 上、下标
  Ctrl+^(上标)
  Ctrl±(下标)

(5) 导数、积分
  Ctrl+Alt+’(单撇(导数符号))
  Ctrl+Shift+”(双撇(二阶导数符号))
  Ctrl+I(定积分记号)
  Ctrl+Shift+I, ! (不定积分记号)

(6)上横线、矢量箭头 

 Ctrl+Shift+连字符(上横线) 
 Ctrl+Alt+连字符(矢量箭头)

(7)注释快捷键
Alt+/
(8)重复上次的输入
Ctrl+L快捷键

(9) 清屏指令
Ctrl+A,而后Delete

(10)ctrl+shift+N–切换标准形式
ctrl+shift+T–切换传统形式

(如果你要问如何记下这些快捷键,其实只要注意把那些字母和英文对应就很好记忆了。
比如,R代表Root,F代表Fraction,I代表Integate,H代表Higher等等)

(11)希腊字母
   esc+a+esc—alpha
(12)F1快捷键调出帮助界面
ctrl+F12全屏快捷键

(13)插入菜单快捷键

结束子表达式 Ctrl+[SpaceIndicator]
上标 Ctrl+^
下标 Ctrl+_
上 Ctrl+7
下 Ctrl+$
相反位置 Ctrl+5
分数 Ctrl+/
根式 Ctrl+2
开始内嵌单元 Ctrl+(
结束内嵌单元 Ctrl+)
左移 Alt+Left
右移 Alt+Right
下移 Alt+Down
上移 Alt+Up
方括号 [] [NegativeMediumSpace][NegativeMediumSpace]Alt+]
大括号 {} [NegativeMediumSpace][NegativeMediumSpace]Alt+}
圆括号 () [NegativeMediumSpace][NegativeMediumSpace]Alt+)

4 绘图Plot参数

(1)基本形式:
Plot[Sin[x], {x, 0, 6 Pi}]
(2)添加图例:
Plot[{Sin[x], Sin[2 x], Sin[3 x]}, {x, 0, 2 Pi},
PlotLegends -> “Expressions”]
或者:Plot[{Sin[x], Sin[2 x], Sin[3 x]}, {x, 0, 2 Pi},
PlotLegends -> Automatic]
(3)添加填充颜色
Plot[2 Sin[x] + x, {x, 0, 15}, Filling -> Bottom]
Plot[Sin[x], {x, 0, 15}, Filling -> Axis]
Plot[2 Sin[x] + x, {x, 0, 15}, Filling -> Top]
或者填充两条线之间的区域:Plot[{Sin[x] + x/2, Sin[x] + x}, {x, 0, 10}, Filling -> {1 -> {2}}]
(4)纵横比坐标轴调整
Plot[Sqrt[1 - x^2], {x, 0, 1}, AspectRatio -> 2]
(5)是否显示坐标轴
Plot[Sinc[x], {x, 0, 10}, Axes -> False]
或者:Plot[Sinc[x], {x, 0, 10}, Axes -> {False, True}]
(6)坐标轴标签
Plot[Sinc[u], {u, 0, 10}, AxesLabel -> Automatic]
或者:Plot[Sinc[x], {x, 0, 10}, AxesLabel -> {x, Sinc[x]}]
(7)调整坐标轴原点位置
Plot[1/(x - 1) + 2, {x, -2, 4}, AxesOrigin -> Automatic]
或者:Plot[1/(x - 1) + 2, {x, -2, 4}, AxesOrigin -> {1, 2}]
(8)坐标轴的样式调整
Plot[Sinc[x], {x, 0, 10},
AxesStyle -> {Directive[Thick, Dashed, Red], Blue}]
(9)展示曲线的截断区域
Plot[Sin[x]/x^2, {x, -10, 10}, ClippingStyle -> Automatic]
(10)调整曲线的颜色
Plot[Sinc[x], {x, 0, 10}, ColorFunction -> “DarkRainbow”]
(11)绘制散点图
p = Table[x^2, {x, 1, 10}]
ListPlot[p]
(12)坐标轴
Graphics[Circle[], Axes -> True]
Plot[Sin[x], {x, 0, 10}, Axes -> {True, False}]
(13)坐标轴样式与刻度大小
Plot[Sinc[x], {x, 0, 10}, AxesStyle -> Directive[Orange, 12]]

(14)绘图标题PlotLable
Plot[BesselJ[1, x], {x, 0, 10}, PlotLabel -> BesselJ[1, x]]

(15)坐标轴刻度Ticks
Plot[Sin[x], {x, 0, 10}, Ticks -> None]
Plot[Sin[x], {x, 0, 10}, Ticks -> Automatic]
Plot[Sin[x], {x, 0, 10}, Ticks -> {{0, Pi, 2 Pi, 3 Pi}, {-1, 1}}]
Plot[Sin[x], {x, 0, 10},
Ticks -> {Table[x, {x, 0, 10}], Table[x/4, {x, -4, 4}] // N}]
(16)绘图样式PlotStyle
Clear[“Global`*”]
Plot[{Sin@x, Cos@x, Tan@x}, {x, 0, 2 [Pi]},
PlotStyle -> {Orange, Dashed, Thick}]

5 参数拟合函数

【Fit函数拟合】
(1)拟合数据并绘制曲线
data = Table[x^2, {x, 1, 20}];
p1 = ListPlot[data];
p2 = Fit[data, {1, x^2}, x]😭此处选择拟合的曲线类型)
f[x_] := p2;
p3 = Plot[p2, {x, 1, 20}];
f[x]
Show[p1, p3]
(2)导入数据成矩阵
r = Import[“h:\data.txt”, “Table”];
r1 = Transpose[r];
ListPlot[r1]
r // Length
Max[r1]
Min[r1]
(3)导入数据成向量
d1 = ReadList[“h:\data.txt”];
len = Length@d1;
p1 = ListPlot@d1
p2 = Fit[d1, {1, x, x^2, x^3}, x];
f[x_] := p2
p3 = Plot[p2, {x, 1, len}];
Show[p1, p3]
(4)
(数据拟合&数据预测)
Clear[“Global`*”]
x = {0.30, 0.40, 0.55, 0.65, 0.80, 1.05};
y = {0.30163, 0.41075, 0.57815, 0.69675, 0.87335, 1.18885};
f = Predict[x -> y, Method -> “LinearRegression”]
n = x // Length;
d = Table[0, {n}, {2}];
For[i = 1, i <= n, i++, {
d[[i, 1]] = Part[x, i];
d[[i, 2]] = Part[y, i];}]
d // MatrixForm

p1 = Plot[f@x, {x, 0, 1.5}, PlotLegends -> “Prediction”];
p2 = ListPlot[d];

Show[p1, p2]
line = Fit[d, {1, t, t^2}, t]
g[t_] = line;
p3 = Plot[g@t, {t, 0, 1.5}];
Show[p2, p3]
h = {0.36, 0.42, 0.75, 0.98};
g@h

6 矩阵(与列表的索引不同)

蓝色字母表示还未赋值,黑色字母表示已赋值。
(1)列表索引
V2 = {{1, 2, 3, 4}, {5, 6, 7, 8}}
For[i = 1, i <= 2, i++,
{For[j = 1, j <= 4, j++, Print[V2[[i, j]]]]}]

(2)【对于一维列列表】提取列表的一行元素构成一个列表
x = {{a, b, c}, {d, e, f}, {g, h, i}}
x[2]或者Part[x,2]
For[i = 1, i <= 3, i++, Print[x[[i]]]]

(3)提取列表的一列元素
x[All, 2]
For[i = 1, i <= 3, i++, Print[x[[All, i]]]]

注意:若列表是一维的,则x[[2]]为索引第二个元素
若列表是二维的,则x[[2]]为索引第二行元素

(4)根据列表元素,绘制饼状图
PieChart[{1, 2, 3, 4}, ChartLegends -> Automatic]

7 基本的列表操作

(1)求行列式
Clear[x]
x = {{a, b}, {c, d}};
x1 = MatrixForm[x]
Det[x]😭求行列式只能针对列表,而非矩阵)
(2)求转置【不能转置一维列表】
Clear[x, x1, x2, x3]
x = {{a, b}, {c, d}};
x1 = MatrixForm[x]
x2 = Transpose[x]😭转置只能针对列表,而非矩阵)

x3 = MatrixForm[%]
(3)求秩
MatrixRank[{{1, 2}, {1, 2}}]
(4)矩阵的迹(主对角线所有元素之和)
Tr[{{a, c}, {b, d}}]
(5)列表基本操作
x1 = {{1, 2}, {3, 4}};
MatrixForm[%]
x2 = {{1, 2}, {3, 4}};
MatrixForm[%]
x1 - x2;(列表对应元素相减)
MatrixForm[%]
x1.x2;(列表相乘)
MatrixForm[%]
x1*x2;(列表对应元素相乘)
MatrixForm[%]
x3 = Inverse[x1]😭求逆矩阵)
MatrixForm[%]
x1.x3;
MatrixForm[%]

(6)列表长度
Length[g]
维数:V2 // Dimensions

8 清除命令

(1)清除当前软件中所有的定义
Clear My Definitions

(2)清除函数定义
Zeros[x_, y_] := Table[0, {x}, {y}];
Clear[zeros]

(3)清除变量的赋值定义
x=2;
y=3;
Clear[x,y]

(4)清除所有的变量、定义、属性、信息
ClearAll[sym1,sym2,Fun1,Fun2]
(5)针对全部变量
一次性查询所有变量:
Names[“Global*"] 一次性删除对所有变量的赋值: Clear["Global"]
一次性删除所有变量:
Remove["Global`
”]
注:后两个可以删除变量和函数定义

9 循环结构

(1)For循环
一层循环:For[i = 1, i <= 10, i++, Print[i, “^2=”, i^2]]
二层循环:For[i = 1, i <= 3, i++,
{For[j = 1, j <= 2, j++, Print[i*j]]}]

(2)Do循环
一层:Do[Print[i], {i, 1, 3}]
二层:Do[i*j // Print, {i, 1, 5}, {j, 1, 4}]

(3)while循环
一层:n = 1;
While[n < 4, Print[n]; n++]

10 格式控制函数

(1)Row将列表元素放在一行
Row[{aaa, b, cccc}]
Row[{aaa, b, cccc}, “----”]
(2)Grid将列表元素排列成表格
Grid[{{a, b, c}, {x, y, z}}]
Grid[{{a, b, c}, {x, y^2, z^3}}, Frame -> All]
(3)Column将列表元素放在一列
Column[{1, 12, 123, 1234}]
(4)Table[x^2,{x,1,10}]

11 Mathematica解方程

(1)普通的方程
f[x_]:=x^2 定义函数 f
?f 显示 f 的定义
Clear[f] 清除所有 f 的定义
x^2 + y /. {x -> 1, y -> 2}
Solve[x^2 + a x + 1 == 0, x]
(2)求解关于 x 和 y 的联立方程组:
Solve[a x + y == 7 && b x - y == 1, {x, y}]
(3)在实数上求方程
Solve[(x^2 + 2) (x^2 - 2) == 0, x, Reals]
(4)在正整数上求解方程:
Solve[x^2 + 2 y^3 == 3681 && x > 0 && y > 0, {x, y}, Integers]
(5)将方程解转换为列表
sol = Solve[x^2 + y^2 == 2 && x - y == 1, {x, y}]
x/.sol
(6)Solve 使用 {} 表示空解或者没有解:
(7)Solve 使用 {{}} 表示满足方程的通解或者所有点:
(8)特定域上求解
Solve[(x^4 - 1) (x^4 - 4) == 0, x, Complexes]
(9)求解具有涉及符号参数的系数的方程:
sol = Solve[x^2 + y^2 == 1 && x + y == a, {x, y}]

(10)寻找靠近一个点的数值解
FindRoot[Sin[x] + Exp[x], {x, 0}]

12 数值结算

(1)辛普森积分
(清除变量和函数)Remove["Global`"]
f[x_] := Exp[-x]😭函数定义)a = 0;
b = 1;(积分上下限)result = (b - a)/6
(f@a + f@b + 4*f[(a + b)/2]);
Print[“数值积分的结果是:\n”, N[result, 10]]
{M4, t} = FindMaximum[{f@x, 0 <= x <= 1}, {x, 0}];
(辛普森积分余项最大值,函数不同,余项表达式也不同)
Print[“余项估计:\n”, N[M4, 10]]

(2)求函数极值
求数值解的话,FindMaximum[函数表达式,变量]
求精确解,MaxValue[函数表达式,变量]
FindMaximum[{函数,约束},{自变量,起始值}]
如FindMaximum[{-x^2+2x, 0<x<2},{x,0.1}]搜索
就是在0到2之间寻找函数最大值,从0.1开始找

(3)

13 解偏微分方程

U=NDSolve[{-D[u[t, x], {t, 2}] ==
D[u[t, x], {x, 2}] + (Pi^2Exp[x]Sin[Pix]),
u[2, x] == Exp[2]Sin[Pix], u[0, x] == Sin[Pi
x], u[t, 0] == 0,
u[t, 1] == 0}, u, {t, 0, 2}, {x, 0, 1}]
Plot3D[Evaluate[u[t, x] /. %], {t, 0, 2}, {x, 0, 1}, PlotRange -> All,
Boxed -> True, AspectRatio -> 0.6]
Information[U]

14 解微分方程组

DSolve[{y’[t] == 4 y[t] - x[t], x’[t] == x[t] + 2 y[t], x[0] == 3,
y[0] == 2}, {x, y}, t]

DSolve[{y’[t] - y[t] - 5 x[t] == 1, x’[t] + 2 y[t] + x[t] == Exp[t],
x[0] == 0, y[0] == 0}, {x, y}, t]

Clear[x, y, t]
DSolve[{y’’[t] == -2 x[t], x’[t] == x[t] - y’[t], x[0] == 5,
y[0] == 0, y’[0] == 10}, {x, y}, t]

DSolve[{y’’[t] - x’[t] == 0, x’’[t] - y’[t] == 0, x[0] == 1,
y[0] == 2, x’[0] == 1, y’[0] == 2}, {x, y}, t]

DSolve[{x’[t] - y’[t] + 6 x[t] == 0, 2*x’[t] + y’[t] - 3 y[t] == 0,
x[0] == 3, y[0] == 2}, {x, y}, t]

DSolve[{y’’[t] + y[t] == 1, y[0] == 3, y’[0] == 0}, y, t]

DSolve[{y’’[t] - 1 == Sin[t], y[0] == 0, y’[0] == 0}, y, t]

DSolve[{y’’[t] + 5 y’[t] + 4 y[t] == Exp[t] + Cos[t], y[0] == 0,
y’[0] == 0}, y, t]

y[t_] := InverseLaplaceTransform[1/(s^2 + 4)^2, s, t]

  • 7
    点赞
  • 77
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值