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, -2Pi, 2Pi, .2}];
Export[“h:\mathematica\oval.gif”, dat]
(8)绘制参数方程的三维gif:
aa = Table[
ParametricPlot3D[{uCos[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]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)