跳转到内容

File:Pendulum phase portrait.svg

页面内容不支持其他语言。
這個文件來自維基共享資源
维基百科,自由的百科全书

原始文件(SVG文件,尺寸为479 × 484像素,文件大小:352 KB)


摘要

描述
English: Phase portrait of an undamped simple pendulum.

The latest revision of the image was created in python using the source code provided below.

The first revision of the image was plotted using with GNU Octave using gnuplot backend and saved as a standalone LaTeX file. The PDF generated was then converted to SVG using pdf2svg. The octave source file 'pendulumOde.m' is provided below for reference.
日期
来源 自己的作品
作者 Krishnavedala
SVG开发
InfoField
 
SVG的源代码为有效代码
 
矢量图使用Matplotlib创作。
源代码
InfoField

Python code

Python source code
from numpy import *
from scipy import *
from scipy.integrate import odeint
from matplotlib.pyplot import *
from mpl_toolkits.axes_grid.axislines import SubplotZero
 
def myFun(u,t=0.,mu=.5):
    x = u[0]
    v = u[1]
    dx = v
    dv = -sin(x)
    return (dx,dv)

if __name__ == "__main__":
    fig = figure(figsize=(5.5,7))
    ax = SubplotZero(fig,211)
    x = linspace(-3*pi,3*pi,100)
    ax.plot(x,-cos(x),'b',lw=1.5)
    fig.add_subplot(ax)
    ax.grid(True,which='major')
    ax.minorticks_on()
    ax.axis('tight')
    ax.axis([-3*pi,3*pi, -1,1])
    ax.set_xticks(arange(-3*pi,3.1*pi,pi))
    ax.set_xticklabels(
        [r'$-3\pi$',r'$-2\pi$',
        r'$-\pi$',r'$0$',r'$\pi$',
        r'$2\pi$',r'$3\pi$'])
    ax.set_xlabel(r'$\theta$')
    ax.set_ylabel(r'$V(\theta)$')
    ax = SubplotZero(fig,212)
    fig.add_subplot(ax)
    t = linspace(0,50,200)
    for m in range(0,60,5):
        u = odeint(myFun,[m/10.,0.],t)
        ax.plot(u[:,0],u[:,1],'b',lw=1.5)
        ax.plot(-u[:,0],u[:,1],'b',lw=1.5)
        u = odeint(myFun,[0,m/10.],t)
        ax.plot(ma.masked_outside(u[:,0],-3*pi,3*pi),
            ma.masked_outside(u[:,1],-3,3),'b',lw=1.5)
        ax.plot(ma.masked_outside(-u[:,0],-3*pi,3*pi),
            ma.masked_outside(u[:,1],-3,3),'b',lw=1.5)
        ax.plot(ma.masked_outside(u[:,0],-3*pi,3*pi),
            ma.masked_outside(-u[:,1],-3,3),'b',lw=1.5)
        ax.plot(ma.masked_outside(-u[:,0],-3*pi,3*pi),
            ma.masked_outside(-u[:,1],-3,3),'b',lw=1.5)
    x = linspace(-3*pi,3*pi,20)
    y = linspace(-3,3,15)
    x,y = meshgrid(x,y)
    X,Y = myFun([x,y])
    M = (hypot(X,Y))
    M[M==0]=1.
    X,Y = X/M, Y/M
    ax.quiver(x,y,ma.masked_outside(X,-3*pi+.1,3*pi-.1),Y,M,pivot='mid',color='r')
    ax.minorticks_on()
    ax.axis('scaled')
    ax.axis([-3*pi,3*pi, -3,3])
    ax.set_yticks(arange(-3,3.1,1.5))
    ax.set_xticks(arange(-3*pi,3.1*pi,pi))
    ax.set_xticklabels(
        [r'$-3\pi$',r'$-2\pi$',
        r'$-\pi$',r'$0$',r'$\pi$',
        r'$2\pi$',r'$3\pi$'])
    ax.set_xlabel(r'$\theta$')
    ax.set_ylabel(r'$\frac{\mathrm{d}\theta}{\mathrm{d}t}$')
    ax.grid(True)
    subplots_adjust(wspace=.1,hspace=-.1)
    fig.show()
    fig.savefig("pendulum.svg", bbox_inches="tight",\
        pad_inches=.15, transparent=False)

Data

Matlab source code
function pendulumOde
% main function to numerically solve the pendulum ODE and plot the phase portrait
  figure;
  subplot(211);
  x = -pi:.1:3*pi;
  h = plot(x,-cos(x),'linewidth',2);
  set(gca,'yminortick','on','xtick',[-pi:pi/2:3*pi],'xticklabel',
    {'$-\\pi$';'$-\\frac{\\pi}{2}$';'$0$';'$\\frac{\\pi}{2}$';'$\\pi$';
    '$\\frac{3}{2}\\pi$';'$2\\pi$';'$\\frac{5}{2}\\pi$';'$3\\pi$'});
  xlim([-pi 3*pi])
  xlabel('$\theta$');
  ylabel('$V(\theta)$');
  grid on;
  subplot(212);
  [x,y] = meshgrid(-pi:.4:3*pi,-3:.2:3);
  u = zeros(size(x));
  v = zeros(size(y));
  for i = 1:numel(x)
    yy = ode_eq(0, [x(i),y(i)]);
    u(i) = yy(1);
    v(i) = yy(2);
    vmod = sqrt(u(i).^2 + v(i).^2);
    u(i) = u(i) / vmod;
    v(i) = v(i) / vmod;
  end
  quiver(x,y,u,v,'r');
  xlabel('$\theta$');
  ylabel('$\frac{\mathrm{d}\theta}{\mathrm{d}t}$');
  xlim([-pi 3*pi])
  ylim([-pi pi])
  grid on;
  set(gca,'yminortick','on','xtick',[-pi:pi/2:3*pi],'xticklabel',
    {'$-\\pi$';'$-\\frac{\\pi}{2}$';'$0$';'$\\frac{\\pi}{2}$';'$\\pi$';
    '$\\frac{3}{2}\\pi$';'$2\\pi$';'$\\frac{5}{2}\\pi$';'$3\\pi$'});
  hold all;
  
  dT = .01;
  T = 40;
  for c = 0:.5:5
    [x,y] = rungeKutta([c;0],dT,T,@ode_eq);
    plot(y(1,:),y(2,:),'b','linewidth',2);
    plot(y(1,:),-y(2,:),'b','linewidth',2);
    [x,y] = rungeKutta([0;c],dT,T,@ode_eq);
    plot(y(1,:),y(2,:),'b','linewidth',2);
    plot(-y(1,:),y(2,:),'b','linewidth',2);
    plot(y(1,:),-y(2,:),'b','linewidth',2);
    plot(-y(1,:),-y(2,:),'b','linewidth',2);
    [x,y] = rungeKutta([c;pi*2],dT,T,@ode_eq);
    plot(y(1,:),y(2,:),'b','linewidth',2);
    plot(y(1,:),-y(2,:),'b','linewidth',2);
    [x,y] = rungeKutta([pi*2;c],dT,T,@ode_eq);
    plot(y(1,:),y(2,:),'b','linewidth',2);
    plot(-y(1,:),y(2,:),'b','linewidth',2);
    plot(y(1,:),-y(2,:),'b','linewidth',2);
    plot(-y(1,:),-y(2,:),'b','linewidth',2);
  end
  print -depslatexstandalone "-S512,512" "pendulum.tex";
end

function dy = ode_eq(x,y)
% function that defines an n-dimensional ODE. 
% In this case, the two linear ODEs of pendulum
  dy = [0;0];
  dy(1) = y(2);
  dy(2) = -sin(y(1));
end

function [x, y] = rungeKutta(y0, dT, T, dyFun, x0)
% A generalized Runge-Kutta algorithm to solve 'n' number of linear ODE
% obtained from an 'n'th degree ODE
  n = length(y0);
  if n > 1 && size(y0,2) == n
    y0 = y0';
  end
  if nargin < 5
    x0 = 0;
  end
  N = round(T/dT);
  x = zeros(1,N);
  y = zeros(n,N);
  x(1) = x0;
  y(:,1) = y0;
  for nn = 1:N-1
    k1 = feval(dyFun, x(nn), y(:,nn));
    k2 = feval(dyFun, x(nn)+.5*dT, y(:,nn)+.5*k1*dT);
    k3 = feval(dyFun, x(nn)+.5*dT, y(:,nn)+.5*k2*dT);
    k4 = feval(dyFun, x(nn)+dT, y(:,nn)+k3*dT);
    y(:,nn+1) = y(:,nn) + (dT/6) * (k1 + 2*k2 + 2*k3 + k4);
    x(nn+1) = x(nn) + dT;
  end
end

许可协议

我,本作品著作权人,特此采用以下许可协议发表本作品:
w:zh:知识共享
署名 相同方式共享
本文件采用知识共享署名-相同方式共享 4.0 国际许可协议授权。
您可以自由地:
  • 共享 – 复制、发行并传播本作品
  • 修改 – 改编作品
惟须遵守下列条件:
  • 署名 – 您必须对作品进行署名,提供授权条款的链接,并说明是否对原始内容进行了更改。您可以用任何合理的方式来署名,但不得以任何方式表明许可人认可您或您的使用。
  • 相同方式共享 – 如果您再混合、转换或者基于本作品进行创作,您必须以与原先许可协议相同或相兼容的许可协议分发您贡献的作品。

说明

添加一行文字以描述该文件所表现的内容

此文件中描述的项目

描繪內容

文件历史

点击某个日期/时间查看对应时刻的文件。

日期/时间缩⁠略⁠图大小用户备注
当前2017年11月13日 (一) 15:202017年11月13日 (一) 15:20版本的缩略图479 × 484(352 KB)Krishnavedalarecompiled image using python code given in the description. No SVG errors
2014年11月29日 (六) 20:302014年11月29日 (六) 20:30版本的缩略图483 × 503(306 KB)Krishnavedalafixed svg by a bug of matplotlib while saving to svg and data going beyond graphical display
2014年11月29日 (六) 19:472014年11月29日 (六) 19:47版本的缩略图483 × 503(382 KB)KrishnavedalaRecreated, better and smaller image using python and matplotlib. Source code included
2014年11月29日 (六) 16:582014年11月29日 (六) 16:58版本的缩略图640 × 640(3.79 MB)KrishnavedalaUser created page with UploadWizard

以下页面使用本文件:

全域文件用途

以下其他wiki使用此文件:

元数据