欢迎光临散文网 会员登陆 & 注册

mathematica软件的常用操作1——注释、运行代码、解方程、解不等式、画图

2023-06-25 22:14 作者:木修于淋  | 我要投稿

本篇文章基于12.0版本进行介绍。mathematica是一款很好用的数学计算软件,它有很强大的符号计算能力,我常常用它来进行复杂的符号表达式运算,其中用到的主要操作如下。

注释/取消注释:

选中要注释的内容,然后alt+/。注释完之后如下所示:(*fjlaskgj*)。

运行代码:

输入代码后点击shift+enter即可运行刚写的代码,重新计算整个文档可以点上面的"计算"选项,里面有"结束内核",点击就可以了,然后再点“计算”里面的"计算笔记本",即可重新计算整个文档。

Solve解方程:

解一元方程:

Solve[a x^2 + b x + c == 0, x] // Simplify。//Simplify是化简用的,运行后的结果如下:

Solve[a x^2 + b x + c == 0, x] // Simplify

Solve[Sin[x] == Cos[x], x],计算结果如下:

Solve[Sin[x] == Cos[x], x]

Solve[x^2 + 3 x + 2 == 0, x],计算结果为:{{x -> -2}, {x -> -1}}。

解方程组:

Solve[a x + b y == c && a1 x + b1 y == d, {x, y}] // FullSimplify//FullSimplify是完全化简用的,和Simplify的效果差不多,使用的时候可以都试试,运行后的结果如下:

Solve[a x + b y == c && a1 x + b1 y == d, {x, y}] // FullSimplify

此时x、y并没有值,可以通过下法拿到上面的值并赋值给某个变量,也可以用%表示上一步得到的结果,但是用它容易混乱,可以直接复制使用上面的结果。

拿到上面结果的值

Reduce解不等式:

Reduce[2 x + y >= 1 && 4 x - y <= 2, {x, y}] // FullSimplify,运行结果如下:

(2 x <= 1 && 2 x + y >= 1) || (2 x > 1 && 2 + y >= 4 x)

Reduce[0 < 1/2 (1 - Sqrt[1 - p0]) < 1, p0] // Simplify,运行结果为0 < p0 <= 1。

Plot和Plot3D画图:

下面生成的图片可以另存为多种格式,也可以在画图时添加一些命令改变图片的样式,以便在论文中使用。

画曲线:

Plot[{1 - 2 x, 4 x - 2}, {x, -5, 5}, PlotLegends -> "Expressions",  AxesLabel -> {"x1", "y1"}, LabelStyle -> Directive[15, FontFamily -> "Times New Roman", FontColor -> Blue], ImageSize -> {200}]。其中{1 - 2 x, 4 x - 2}表示要画的两个函数,{x, -5, 5}表示自变量的范围,PlotLegends -> "Expressions"表示图例用所画曲线的表达式表示, AxesLabel -> {"x1", "y1"}表示x轴和y轴对应的名称分别命名为“x1”、“y1”,LabelStyle -> Directive[15, FontFamily -> "Times New Roman", FontColor -> Blue]表示对图中的字体样式进行设置,字体大小为15,字体格式为"Times New Roman",字体颜色为蓝色,ImageSize -> {200}表示设置图片大小为200。如果想更深入了解某个关键词的用法可以去帮助文档里面搜索,会出来很多示例。最终的运行结果如下:

Plot[{1 - 2 x, 4 x - 2}, {x, -5, 5}, PlotLegends -> "Expressions",  AxesLabel -> {"x1", "y1"}, LabelStyle -> Directive[15, FontFamily -> "Times New Roman", FontColor -> Blue], ImageSize -> {200}]

画三维曲面:

Plot3D[x^2 + 2 y^2, {x, -5, 5}, {y, -5, 5}],其中x^2 + 2 y^2表示三维曲面的函数,{x, -5, 5}, {y, -5, 5}表示两个变量的范围,运行结果如下:

Plot3D[x^2 + 2 y^2, {x, -5, 5}, {y, -5, 5}]

加入坐标图例,并更改字体格式和大小:

Plot3D[x^2 + 2 y^2, {x, -5, 5}, {y, -5, 5}, AxesLabel -> {x, y, z},LabelStyle->Directive[20, FontFamily -> "Times New Roman", FontColor -> Blue]],其中AxesLabel -> {x, y, z}表示为三个坐标轴命名,最终运行结果如下:

Plot3D[x^2 + 2 y^2, {x, -5, 5}, {y, -5, 5}, AxesLabel -> {x, y, z},  LabelStyle ->Directive[20, FontFamily -> "Times New Roman", FontColor -> Blue]]

为图形加入边框:

Plot3D[x^2 + 2 y^2, {x, -5, 5}, {y, -5, 5}, AxesLabel -> {x, y, z},  LabelStyle ->Directive[20, FontFamily -> "Times New Roman", FontColor -> Black], BoundaryStyle -> Directive[Blue, Thick]],其中BoundaryStyle -> Directive[Blue, Thick]表示为图片添加边框,颜色为蓝色,粗的,效果如下:

Plot3D[x^2 + 2 y^2, {x, -5, 5}, {y, -5, 5}, AxesLabel -> {x, y, z},  LabelStyle ->Directive[20, FontFamily -> "Times New Roman", FontColor -> Black], BoundaryStyle -> Directive[Blue, Thick]]

改变图形样式:

Plot3D[x^2 + 2 y^2, {x, -5, 5}, {y, -5, 5}, AxesLabel -> {x, y, z}, LabelStyle ->Directive[20, FontFamily -> "Times New Roman", FontColor -> Black], BoundaryStyle -> Directive[Blue, Thick], PlotStyle -> ColorData["Rainbow"]],其中PlotStyle -> ColorData["Rainbow"]改变图片的背景颜色,有很多颜色可以选,我这里只是随便选了一种,效果如下:

Plot3D[x^2 + 2 y^2, {x, -5, 5}, {y, -5, 5}, AxesLabel -> {x, y, z}, LabelStyle ->Directive[20, FontFamily -> "Times New Roman", FontColor -> Black], BoundaryStyle -> Directive[Blue, Thick], PlotStyle -> ColorData["Rainbow"]]

还可以去除图形颜色:

Plot3D[x^2 + 2 y^2, {x, -5, 5}, {y, -5, 5}, AxesLabel -> {x, y, z}, LabelStyle ->Directive[20, FontFamily -> "Times New Roman", FontColor -> Black], BoundaryStyle -> Directive[Blue, Thick], PlotStyle -> None],其中 PlotStyle -> None为去除背景颜色,只剩下网格了,效果如下:

Plot3D[x^2 + 2 y^2, {x, -5, 5}, {y, -5, 5}, AxesLabel -> {x, y, z}, LabelStyle ->Directive[20, FontFamily -> "Times New Roman", FontColor -> Black], BoundaryStyle -> Directive[Blue, Thick], PlotStyle -> None]

也可以同时绘制多幅图:

Plot3D[{x^2 + 2 y^2, 60}, {x, -5, 5}, {y, -5, 5},AxesLabel -> {x, y, z},LabelStyle -> Directive[20, FontFamily -> "Times New Roman", FontColor -> Black],  BoundaryStyle -> Directive[Blue, Thick], PlotStyle -> None],其中{x^2 + 2 y^2, 60}表示要绘制的两个平面:

Plot3D[{x^2 + 2 y^2, 60}, {x, -5, 5}, {y, -5, 5},AxesLabel -> {x, y, z},LabelStyle -> Directive[20, FontFamily -> "Times New Roman", FontColor -> Black],  BoundaryStyle -> Directive[Blue, Thick], PlotStyle -> None]

还可以用含有x的变量来约束范围:

Plot3D[60, {x, -5, 5}, {y, x, 5}, AxesLabel -> {x, y, z}, LabelStyle -> Directive[20, FontFamily -> "Times New Roman", FontColor -> Black],  BoundaryStyle -> Directive[Blue, Thick], PlotStyle -> None],其中{y, x, 5}表示用变量x来约束y的范围,输出效果如下:

Plot3D[60, {x, -5, 5}, {y, x, 5}, AxesLabel -> {x, y, z}, LabelStyle -> Directive[20, FontFamily -> "Times New Roman", FontColor -> Black],  BoundaryStyle -> Directive[Blue, Thick], PlotStyle -> None]

将多幅不同样式的图放在一起:

Show[Plot3D[x^2 + 2 y^2, {x, -5, 5}, {y, -5, 5},   AxesLabel -> {x, y, z},   LabelStyle ->    Directive[20, FontFamily -> "Times New Roman", FontColor -> Black], BoundaryStyle -> Directive[Blue, Thick], PlotStyle -> None], Plot3D[60, {x, -5, 5}, {y, -5, 5}, AxesLabel -> {x, y, z}, LabelStyle -> Directive[20, FontFamily -> "Times New Roman", FontColor -> Black],  BoundaryStyle -> Directive[Red, Thickness[0.01]], Mesh -> None, PlotStyle -> None]],里面包含了两个Plot3D函数,利用Show语句将两个图合并到一张图上展示,其中BoundaryStyle -> Directive[Red, Thickness[0.01]]设置边框为红色,粗细为0.01,Mesh -> None表示去除内部网格, PlotStyle -> None表示去除图片颜色。输出结果如下:

Show[Plot3D[x^2 + 2 y^2, {x, -5, 5}, {y, -5, 5},   AxesLabel -> {x, y, z},   LabelStyle ->    Directive[20, FontFamily -> "Times New Roman", FontColor -> Black], BoundaryStyle -> Directive[Blue, Thick], PlotStyle -> None], Plot3D[60, {x, -5, 5}, {y, -5, 5}, AxesLabel -> {x, y, z}, LabelStyle -> Directive[20, FontFamily -> "Times New Roman", FontColor -> Black],  BoundaryStyle -> Directive[Red, Thickness[0.01]], Mesh -> None, PlotStyle -> None]]

上面也可以通过赋值来实现:

p1 = Plot3D[x^2 + 2 y^2, {x, -5, 5}, {y, -5, 5}, AxesLabel -> {x, y, z}, LabelStyle ->   Directive[20, FontFamily -> "Times New Roman", FontColor -> Black],BoundaryStyle -> Directive[Blue, Thick], PlotStyle -> None];

p2 = Plot3D[60, {x, -5, 5}, {y, -5, 5}, AxesLabel->{x, y, z},LabelStyle-> Directive[20, FontFamily -> "Times New Roman", FontColor -> Black], BoundaryStyle -> Directive[Red, Thickness[0.01]], Mesh ->None, PlotStyle -> None];

Show[p1, p2],输出结果如下:

Show[p1, p2]

未完待续。。。。。。

大家感觉有帮助的话不妨动动小手点个赞吧。

mathematica软件的常用操作1——注释、运行代码、解方程、解不等式、画图的评论 (共 条)

分享到微博请遵守国家法律