跳至內容

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檔案的原始碼通過W3C驗證
 
vector image使用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:創用CC
姓名標示 相同方式分享
您可以自由:
  • 分享 – 複製、發佈和傳播本作品
  • 重新修改 – 創作演繹作品
惟需遵照下列條件:
  • 姓名標示 – 您必須指名出正確的製作者,和提供授權條款的連結,以及表示是否有對內容上做出變更。您可以用任何合理的方式來行動,但不得以任何方式表明授權條款是對您許可或是由您所使用。
  • 相同方式分享 – 如果您利用本素材進行再混合、轉換或創作,您必須基於如同原先的相同或兼容的條款,來分布您的貢獻成品。

說明

添加單行說明來描述出檔案所代表的內容

在此檔案描寫的項目

描繪內容

檔案歷史

點選日期/時間以檢視該時間的檔案版本。

日期/時間縮⁠圖尺寸使用者備⁠註
目前2017年11月13日 (一) 15:20於 2017年11月13日 (一) 15:20 版本的縮圖479 × 484(352 KB)Krishnavedalarecompiled image using python code given in the description. No SVG errors
2014年11月29日 (六) 20:30於 2014年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:47於 2014年11月29日 (六) 19:47 版本的縮圖483 × 503(382 KB)KrishnavedalaRecreated, better and smaller image using python and matplotlib. Source code included
2014年11月29日 (六) 16:58於 2014年11月29日 (六) 16:58 版本的縮圖640 × 640(3.79 MB)KrishnavedalaUser created page with UploadWizard

下列頁面有用到此檔案:

全域檔案使用狀況

以下其他 wiki 使用了這個檔案:

詮釋資料