Mupad学习记录2

来源:互联网 发布:金融和互联网行业知乎 编辑:程序博客网 时间:2024/04/30 04:35


Mupad学习记录2

这一节着重学习关于绘图及其动画控制

绘图
1)一般绘图
plot(sin(x)*cos(3*x))   绘制二维图
plot(sin(x)*sin(y), #3D)   绘制三维图(#3D替代二维动画)
plot(sin(x), cos(x), tan(x), cot(x)   多个图形在同一张纸里
plot(sin(k*x) $ k = 1..3)   使用$符号使得一系列图画在一张纸上(二维)
plot(-sqrt(r^2 - x^2 - y^2) $ r = 1..5, #3D)   使用$符号使得一系列图画在一张纸上(三维)
plot(sin(x^3)*exp(x), x = 3..5)   使用x=#..#确定作图范围(二维)
plot(sin(x)*sin(y), x = 0..3, y = 1..3, #3D)   使用x=#..#,y=#..#确定作图范围(三维)
plot(sin(k*x) $ k = 1..5, x = 0..2*PI)   在关于$的系列图中设定范围
plot({sin(k*x), k*t^2} $ k = 1..5, x = 0..2*PI, t = -1..1)   多幅系列图的画法及其范围
plot(piecewise([x < - 2, - 1], [-1 < x and x < 0, x^2], [0 < x and x < 1, -x^2], [x > 1, 1]))   分段函数的画法

通过界面直接修改图形属性,而非使用编码修改,如果点击目标对象无反应,选择菜单View > Object Browser
在图形界面,可以自由旋转,缩放,以及自动旋转等,相关命令在图形窗口界面,自行尝试,概不赘述

动画
1)关于参数的动画
plot(exp(x)*sin(a^2*x), x = 1..2, a = 2..6)   二维直角坐标动画
plot(sin(a*x^2 + a*y^2), x = -2..2, y = -2..2, a = 0.1..2, #3D)   三维直角坐标动画

例子1
plot(
 plot::Function2d(a*sin(x), x = 0..a*PI, a = 0.5..1),
 plot::Rectangle(0..a*PI, 0..a, a = 0.5..1,
                 LineColor = RGB::Black),
 plot::Line2d([0, 0], [PI*a, a], a = 0.5 ..1,
                   LineColor = RGB::Black)
//其中的plot::Rectangle和plot::Line2d分别是矩形作图和直线作图

例子2
plot(plot::Arc2d(1 + a, [0, 0], AngleRange = 0..a*PI, a = 0..1)):
//其中的plot::Arc2d是指plot::Arc2d(半径,中心,弧线角度范围,动画参数a)

例子3
plot(plot::Function2d(4*a*x, x = 0..1, a = 0..1),
     plot::Function2d(b*x^2, x = 0..1, b = 1..4)):
//这是将两幅参数动画画到同一幅图片中,参数的名称不同,注意参数名称是全局变量,必须不同

例子4
f := (x, y) -> 4 - (x - a)^2 - (y - a)^2:
mycolor := proc(x, y, z, a)
  local t;
  begin
     t := sqrt((x - a)^2 + (y - a)^2):
     if t < 0.1 then
          return(RGB::Red)
     elif t < 0.4 then
          return(RGB::Orange)
     elif t < 0.7 then
          return(RGB::Green)
     else return(RGB::Blue)
     end_if;
end:
plot(plot::Function3d(f, x = -1..1, y = -1..1, a = -1..1,
                      FillColorFunction = mycolor)):
//这个例子展示了如何用全局变量t定义调色函数myclolor,其中proc是定义程序的命令(procedure的缩写),local等也是相应配套命令,详情可以用?local命令查看帮助文档

例子5
plot(plot::Point2d([a, sin(a)], a = 0..2*PI,
                   Frames = 100, TimeRange = 0..5)):
//5秒100帧
plot(plot::Point2d([a, sin(a)], a = 0..2*PI,
                   Frames = 200, TimeRange = 0..5)):
//5秒200帧,增加帧可以使动画更光滑,默认是50帧,根据人眼特性,n<25(t1-t0)是合适的帧数

例子6
p1 := plot::Point2d(-1, sin(a), a = 0..PI, Color = RGB::Red,
                    PointSize = 5*unit::mm,
                    TimeBegin = 0, TimeEnd = 5):
p2 := plot::Point2d(0, sin(a), a = 0..PI, Color = RGB::Green,
                    PointSize = 5*unit::mm,
                    TimeBegin = 6, TimeEnd = 12):
p3 := plot::Point2d(1, sin(a), a = 0..PI, Color = RGB::Blue,
                    PointSize = 5*unit::mm,
                    TimeBegin = 9, TimeEnd = 15):
plot(p1, p2, p3, PointSize = 3.0*unit::mm,
     YAxisVisible = FALSE):
//多个对象的动画设定,动画的先后顺序通过timebegin和timeend来控制

例子7
p2::VisibleAfterEnd := FALSE:
p3::VisibleBeforeBegin := FALSE:
plot(p1, p2, p3, PointSize = 3.0*unit::mm,
     YAxisVisible = FALSE):
//通过Visible命令控制动画前后的可见性
//常见命令有VisibleAfter = t0,VisibleBefore = t1,VisibleFromTo = t0..t1等,详见帮助文档

例子8
p1 := plot::Point2d(-1, sin(a), a = 0..PI, Color = RGB::Red,
                    PointSize = 5*unit::mm,
                    TimeBegin = 0, TimeEnd = 5):
p2 := plot::Point2d(0, sin(a), a = 0..PI, Color = RGB::Green,
                    PointSize = 5*unit::mm,
                    TimeBegin = 6, TimeEnd = 12):
p3 := plot::Point2d(1, sin(a), a = 0..PI, Color = RGB::Blue,
                    PointSize = 5*unit::mm,
                    TimeBegin = 9, TimeEnd = 15):
p2::VisibleAfterEnd := FALSE:
p3::VisibleBeforeBegin := FALSE:
p4 := plot::Point2d(0.5, 0.5, Color = RGB::Black,
                    PointSize = 5*unit::mm,
                    VisibleFromTo = 7..13):
plot(p1, p2, p3, p4, PointSize = 3.0*unit::mm,
     YAxisVisible = FALSE):
//多个对象,静止和运动交互出现的控制

例子9
for i from 0 to 101 do
    t[i] := i/10;
end_for:
for i from 0 to 100 do
  x := i/100*PI;
  myframe[i] := plot::Group2d(
        plot::Point2d([x, sin(x)], Color = RGB::Red),
        plot::Point2d([x, cos(x)], Color = RGB::Blue),
        VisibleFromTo = t[i]..t[i + 1]);
end_for:
plot(myframe[i] $ i = 0..100, PointSize = 5.0*unit::mm):
//这是所谓的"Frame by frame animations",通过编程实现动画

例子10
delete i:
plot(plot::Point2d([i/100*PI, sin(i/100*PI)], i = 0..100,
                   Color = RGB::Red),
     plot::Point2d([i/100*PI, cos(i/100*PI)], i = 0..100,
                   Color = RGB::Blue),
     Frames = 101, TimeRange = 0..10,
     PointSize = 5.0*unit::mm):
//例9同样可以用一般动画命令来实现

例子11
f := x -> -sqrt(1 - x^2):
plot(// The static rim:
     plot::Function2d(f(x), x = -1..1, Color = RGB::Black),
     // The incoming rays:
     plot::Line2d([x, 2], [x, f(x)], VisibleAfter = 5*x
                 ) $ x in [-1 + i/20 $ i = 1..39],
     // The reflected rays leaving to the right:
     plot::Line2d([x, f(x)],
                  [1, f(x) + (1-x)*(f'(x) - 1/f'(x))/2],
                  Color = RGB::Orange, VisibleAfter = 5*x
                 ) $ x in [-1 + i/20 $ i =  1..19],
     // The reflected rays leaving to the left:
     plot::Line2d([x, f(x)],
                  [-1, f(x) - (x+1)*(f'(x) - 1/f'(x))/2],
                  Color = RGB::Orange, VisibleAfter = 5*x
                 ) $ x in [-1 + i/20 $ i = 21..39],
     ViewingBox = [-1..1, -1..1]):
//运用visibleafter命令,将已经画好的图像逐步呈现,以达到动画效果,这是一种常见的简单动画制作方法

应用实例展示

例子12
DIGITS := 2:
// the function:
f := x -> cos(x^2):
// the anti-derivative:
F := x -> numeric::int(f(y), y = 0..x):
// the graph of f(x):
g := plot::Function2d(f(x), x = 0..6, Color = RGB::Blue):
// the graph of F(x):
G := plot::Function2d(F(x), x = 0..6, Color = RGB::Black):
// a point moving along the graph of F(x):
p := plot::Point2d([a, F(a)], a = 0..6, Color = RGB::Black):
// hatched region between the origin and the moving point p:
h := plot::Hatch(g, 0, 0 ..a, a = 0..6, Color = RGB::Red):
// the right border line of the hatched region:
l := plot::Line2d([a, 0], [a, f(a)], a = 0..6,
                  Color = RGB::Red):
// a dashed vertical line from f to F:
L1 := plot::Line2d([a, f(a)], [a, F(a)], a = 0..6,
                  Color = RGB::Black, LineStyle = Dashed):
// a dashed horizontal line from the y axis to F:
L2 := plot::Line2d([-0.1, F(a)], [a, F(a)], a = 0..6,
                  Color = RGB::Black, LineStyle = Dashed):
// the current value of F at the moving point p:
t := plot::Text2d(a -> F(a), [-0.2, F(a)], a = 0..6,
                  HorizontalAlignment = Right):
plot(g, G, p, h, l, L1, L2, t,
     YTicksNumber = None, YTicksAt = [-1, 1]):
delete DIGITS:
//积分动画

例子13
c := a -> 1/2 *(1 - 1/sin(PI/2*a)):
mycolor := (u, v, x, y, z) -> [(u - 0.8)/0.4, 0, (1.2 - u)/0.4]:
rectangle2annulus := plot::Surface(
   [c(a) + (u - c(a))*cos(PI*v), (u - c(a))*sin(PI*v), 0],
   u = 0.8..1.2, v = -a..a, a = 1/10^10..1,
   FillColorFunction = mycolor, Mesh = [3, 40], Frames = 40):
plot(rectangle2annulus, Axes = None,
     CameraDirection = [-11, -3, 3]):
//圆盘动画
annulus2moebius := plot::Surface(
   [((u - 1)*cos(a*v*PI/2) + 1)*cos(PI*v),
    ((u - 1)*cos(a*v*PI/2) + 1)*sin(PI*v),
     (u - 1)*sin(a*v*PI/2)],
   u = 0.8..1.2, v = -1..1, a = 0..1,
   FillColorFunction = mycolor, Mesh = [3, 40], Frames = 20):
plot(annulus2moebius, Axes = None,
     CameraDirection = [-11, -3, 3]):
//折叠动画
rectangle2annulus::VisibleFromTo := 0..5:
annulus2moebius::VisibleFromTo := 5..7:
plot(rectangle2annulus, annulus2moebius, Axes = None,
     CameraDirection = [-11, -3, 3]):
//连接前两个动画
//莫比乌斯带

例子14
ms := 1: m1 := 0.04: m2 := 0.0001:
//设定三体质量
Y := numeric::odesolve2(numeric::ode2vectorfield(
 {xs''(t) =
   -m1*(xs(t)-x1(t))/sqrt((xs(t)-x1(t))^2 + (ys(t)-y1(t))^2)^3
   -m2*(xs(t)-x2(t))/sqrt((xs(t)-x2(t))^2 + (ys(t)-y2(t))^2)^3,
  ys''(t) =
   -m1*(ys(t)-y1(t))/sqrt((xs(t)-x1(t))^2 + (ys(t)-y1(t))^2)^3
   -m2*(ys(t)-y2(t))/sqrt((xs(t)-x2(t))^2 + (ys(t)-y2(t))^2)^3,
  x1''(t) =
   -ms*(x1(t)-xs(t))/sqrt((x1(t)-xs(t))^2 + (y1(t)-ys(t))^2)^3
   -m2*(x1(t)-x2(t))/sqrt((x1(t)-x2(t))^2 + (y1(t)-y2(t))^2)^3,
  y1''(t) =
   -ms*(y1(t)-ys(t))/sqrt((x1(t)-xs(t))^2 + (y1(t)-ys(t))^2)^3
   -m2*(y1(t)-y2(t))/sqrt((x1(t)-x2(t))^2 + (y1(t)-y2(t))^2)^3,
  x2''(t) =
   -ms*(x2(t)-xs(t))/sqrt((x2(t)-xs(t))^2 + (y2(t)-ys(t))^2)^3
   -m1*(x2(t)-x1(t))/sqrt((x2(t)-x1(t))^2 + (y2(t)-y1(t))^2)^3,
  y2''(t) =
   -ms*(y2(t)-ys(t))/sqrt((x2(t)-xs(t))^2 + (y2(t)-ys(t))^2)^3
   -m1*(y2(t)-y1(t))/sqrt((x2(t)-x1(t))^2 + (y2(t)-y1(t))^2)^3,
  xs(0)  = -m1   ,   x1(0)  = ms,     x2(0)  =  0,
  ys(0)  = 0.7*m2,   y1(0)  = 0,      y2(0)  = -0.7*ms,
  xs'(0) = -1.01*m2, x1'(0) = 0,      x2'(0) =  1.01*ms,
  ys'(0) = -0.9*m1,  y1'(0) = 0.9*ms, y2'(0) =  0},
 [xs(t), xs'(t), ys(t), ys'(t),
  x1(t), x1'(t), y1(t), y1'(t),
  x2(t), x2'(t), y2(t), y2'(t)]
)):
//解ODE方程并输入相关公式
dt := 0.05: imax := 516:
plot(// The sun:
     plot::Point2d(Y(t)[1], Y(t)[3], Color = RGB::Orange,
                   VisibleFromTo = t..t + 0.99*dt,
                   PointSize = 4*unit::mm
                  ) $  t in [i*dt $ i = 0..imax],
     // The giant planet:
     plot::Point2d(Y(t)[5], Y(t)[7], Color = RGB::Red,
                   VisibleFromTo = t..t + 0.99*dt,
                   PointSize = 3*unit::mm
                  ) $  t in [i*dt $ i = 0..imax],
     // The orbit of the giant planet:
     plot::Line2d([Y(t - dt)[5], Y(t - dt)[7]],
                  [Y(t)[5], Y(t)[7]], Color = RGB::Red,
                   VisibleAfter = t
                 ) $  t in [i*dt $ i = 1..imax],
     // The small planet:
     plot::Point2d(Y(t)[9], Y(t)[11], Color = RGB::Blue,
                   VisibleFromTo = t..t + 0.99*dt,
                   PointSize = 2*unit::mm
                  ) $  t in [i*dt $ i = 0..imax],
     // The orbit of the small planet:
     plot::Line2d([Y(t - dt)[9], Y(t - dt)[11]],
                  [Y(t)[9], Y(t)[11]], Color = RGB::Blue,
                  VisibleAfter = t
                 ) $  t in [i*dt $ i = 1..imax]
):
//运用"frame by frame"制定三体运动动画





0 0
原创粉丝点击