Asymptote using graph.asy – fig0220

Category: Asymptote,Examples 2D,graph.asyPh. Ivaldi @ 23 h 10 min

Figure 0021
(Compiled with Asymptote version 2.14svn-r5318)
    
/*One can see this graphe drawed with my package HERE*/
import graph;
import patterns;
usepackage("mathrsfs");

unitsize(2cm,1.5cm);
real xmin=-1,xmax=4;
real ymin=-1,ymax=5;

// Definition of fonctions f and g :
real f(real x) {return 4x-x^2+4/(x^2+1)^2;}
real g(real x) {return x-1+4/(x^2+1)^2;}

// Trace the curves :
path Cf=graph(f,xmin,xmax,n=400);
path Cg=graph(g,xmin,xmax,n=400);
draw(Cf,linewidth(1bp));
draw(Cg,linewidth(1bp));
xlimits(xmin,xmax,Crop);
ylimits(ymin,ymax,Crop);

// The grid :
xaxis(BottomTop, xmin, xmax, Ticks("%", Step=1, step=0.5, extend=true, ptick=lightgrey));
yaxis(LeftRight, ymin, ymax, Ticks("%", Step=1, step=0.5, extend=true, ptick=lightgrey));
// The axis.
xequals(Label("$y$",align=W),0,ymin=ymin-0.25, ymax=ymax+0.25,
        Ticks(NoZero,pTick=nullpen, ptick=grey),
        p=linewidth(1pt), Arrow(2mm));
yequals(Label("$x$",align=S),0,xmin=xmin-0.25, xmax=xmax+0.25,
        Ticks(NoZero,pTick=nullpen, ptick=grey),
        p=linewidth(1pt), Arrow(2mm));

labelx(Label("$O$",NoFill), 0, SW);
draw(Label("$\vec{\imath}$",align=S,UnFill),
     (0,0)--(1,0),scale(2)*currentpen,Arrow);
draw(Label("$\vec{\jmath}$",align=W,UnFill),
     (0,0)--(0,1),scale(2)*currentpen,Arrow);
dot((0,0));

label("$\mathscr{C}_f$",(2.25,f(2.25)),2N);
label("$\mathscr{C}_f$",(2.25,g(2.25)),2S);

// Les hachures.
path vline=(1,-1)--(1,5);
add("hachure",hatch(3mm));
fill(buildcycle(vline,graph(f,1,4),graph(g,1,4)),pattern("hachure")); 

Étiquettes : , , , , ,


Asymptote using graph.asy – fig0230

Category: Asymptote,Examples 2D,graph.asyPh. Ivaldi @ 0 h 10 min

Figure 0022
(Compiled with Asymptote version 2.14svn-r5318)
    
import graph;
unitsize(x=1cm,y=2cm);

struct rational
{
  int p;
  int q;
  real ep=1/10^5;
};

rational operator init() {return new rational;}

rational rational(real x, real ep=1/10^5)
{
  rational orat;
  int q=1;
  while (abs(round(q*x)-q*x)>ep)
    {
      ++q;
    }
  orat.p=round(q*x);
  orat.q=q;
  orat.ep=ep;
  return orat;
}

int pgcd(int a, int b)
{
  int a_=abs(a), b_=abs(b), r=a_;
  if (b_>a_) {a_=b_; b_=r; r=a_;}
  while (r>0)
    {
      r=a_%b_;
      a_=b_;
      b_=r;
    }
  return a_;
}

string texfrac(int p, int q,
               string factor="",
               bool signin=false, bool factorin=true,
               bool displaystyle=false,
               bool zero=true)
{
  if (p==0) return (zero ? "$0$" : "");
  string disp= displaystyle ? "$\displaystyle " : "$";
  int pgcd=pgcd(p,q);
  int num= round(p/pgcd), den= round(q/pgcd);
  string nums;
  if (num==1)
    if (factor=="" || (!factorin && (den !=1))) nums="1"; else nums="";
  else
    if (num==-1)
      if (factor=="" || (!factorin && (den !=1))) nums="-1"; else nums="-";
    else nums= (string) num;
  if (den==1) return "$" + nums + factor + "$";
  else
    {
      string dens= (den==1) ? "" : (string) den;
      if (signin || num>0)
        if (factorin)
          return disp + "\frac{" + nums + factor + "}{" + (string) dens + "}$";
        else
          return disp + "\frac{" + nums + "}{" + (string) dens + "}"+ factor + "$";
      else
        {
          if (num==-1)
            if (factor=="" || !factorin) nums="1"; else nums="";
          else nums=(string)(abs(num));
        if (factorin)
          return disp + "-\frac{" + nums + factor + "}{" + (string) dens + "}$";
        else
          return disp + "-\frac{" + nums + "}{" + (string) dens + "}"+ factor + "$";
        }
    }
}

string texfrac(rational x,
               string factor="",
               bool signin=false, bool factorin=true,
               bool displaystyle=false,
               bool zero=true)
{
  return texfrac(x.p, x.q, factor, signin, factorin, displaystyle, zero);
}

ticklabel labelfrac(real ep=1/10^5, real factor=1.0,
                    string symbol="",
                    bool signin=false, bool symbolin=true,
                    bool displaystyle=false,
                    bool zero=true)
{
  return new string(real x)
    {
      return texfrac(rational(x/factor), symbol, signin, symbolin, displaystyle, zero);
    };
}

ticklabel labelfrac=labelfrac();

xlimits( -2pi, 2pi);
ylimits( -1, 1);

yaxis("y",LeftRight , Ticks(labelfrac,Step=.5,step=.25, ptick=grey, extend=true));

xaxis("$\theta$",BottomTop, Ticks(labelfrac(factor=pi,symbol="\pi",symbolin=false),
                           Step=pi/2, step=pi/4, ptick=grey, extend=true));

draw(graph(new real(real x){return sin(x);},-2pi,2pi));
draw(graph(new real(real x){return cos(x);},-2pi,2pi), .8red);

Étiquettes : , , , , ,


Asymptote using graph.asy – fig0240

Category: Asymptote,Examples 2D,graph.asyPh. Ivaldi @ 1 h 10 min

Figure 0023
(Compiled with Asymptote version 1.84svn-r4619)
    
import graph;

// public real xunit=1cm,yunit=1cm;

void graphicrules(picture pic=currentpicture, string prefix=defaultfilename, real unit=1cm,
                  real xunit=unit != 0 ? unit : 0,
                  real yunit=unit != 0 ? unit : 0,
                  real xmin, real xmax, real ymin, real ymax)
{
  xlimits(xmin, xmax);
  ylimits(ymin, ymax);
  unitsize(x=xunit, y=yunit);
}

struct rational
{
  int p;
  int q;
  real ep=1/10^5;
};

rational operator init() {return new rational;}

rational rational(real x, real ep=1/10^5)
{
  rational orat;
  int q=1;
  while (abs(round(q*x)-q*x)>ep)
    {
      ++q;
    }
  orat.p=round(q*x);
  orat.q=q;
  orat.ep=ep;
  return orat;
}

int pgcd(int a, int b)
{
  int a_=abs(a), b_=abs(b), r=a_;
  if (b_>a_) {a_=b_; b_=r; r=a_;}
  while (r>0)
    {
      r=a_%b_;
      a_=b_;
      b_=r;
    }
  return a_;
}

string texfrac(int p, int q,
               string factor="",
               bool signin=false, bool factorin=true,
               bool displaystyle=false,
               bool zero=true)
{
  if (p==0) return (zero ? "$0$" : "");
  string disp= displaystyle ? "$\displaystyle " : "$";
  int pgcd=pgcd(p,q);
  int num= round(p/pgcd), den= round(q/pgcd);
  string nums;
  if (num==1)
    if (factor=="" || (!factorin && (den !=1))) nums="1"; else nums="";
  else
    if (num==-1)
      if (factor=="" || (!factorin && (den !=1))) nums="-1"; else nums="-";
    else nums= (string) num;
  if (den==1) return "$" + nums + factor + "$";
  else
    {
      string dens= (den==1) ? "" : (string) den;
      if (signin || num>0)
        if (factorin)
          return disp + "\frac{" + nums + factor + "}{" + (string) dens + "}$";
        else
          return disp + "\frac{" + nums + "}{" + (string) dens + "}"+ factor + "$";
      else
        {
          if (num==-1)
            if (factor=="" || !factorin) nums="1"; else nums="";
          else nums=(string)(abs(num));
          if (factorin)
            return disp + "-\frac{" + nums + factor + "}{" + (string) dens + "}$";
          else
            return disp + "-\frac{" + nums + "}{" + (string) dens + "}"+ factor + "$";
        }
    }
}

string texfrac(rational x,
               string factor="",
               bool signin=false, bool factorin=true,
               bool displaystyle=false,
               bool zero=true)
{
  return texfrac(x.p, x.q, factor, signin, factorin, displaystyle, zero);
}

ticklabel labelfrac(real ep=1/10^5, real factor=1.0,
                    string symbol="",
                    bool signin=false, bool symbolin=true,
                    bool displaystyle=false,
                    bool zero=true)
{
  return new string(real x)
    {
      return texfrac(rational(x/factor), symbol, signin, symbolin, displaystyle, zero);
    };
}

ticklabel labelfrac=labelfrac();

void grid(picture pic=currentpicture,
          real xmin=pic.userMin().x, real xmax=pic.userMax().x,
          real ymin=pic.userMin().y, real ymax=pic.userMax().y,
          real xStep=1, real xstep=.5,
          real yStep=1, real ystep=.5,
          pen pTick=nullpen, pen ptick=grey, bool above=true)
{
  xaxis(pic, BottomTop, xmin, xmax, Ticks("%",extend=true,Step=xStep,step=xstep,pTick=pTick,ptick=ptick), above=above);
  yaxis(pic, LeftRight, ymin, ymax, Ticks("%",extend=true,Step=yStep,step=ystep,pTick=pTick,ptick=ptick), above=above);
}

void cartesianaxis(picture pic=currentpicture,
                   Label Lx=Label("$x$",align=S),
                   Label Ly=Label("$y$",align=W),
                   real xmin=pic.userMin().x, real xmax=pic.userMax().x,
                   real ymin=pic.userMin().y, real ymax=pic.userMax().y,
                   real extrawidth=1, real extraheight=extrawidth,
                   pen p=currentpen,
                   ticks xticks=Ticks("%",pTick=nullpen, ptick=grey),
                   ticks yticks=Ticks("%",pTick=nullpen, ptick=grey),
                   bool above=true,
                   arrowbar arrow=Arrow)
{
  extraheight= cm*extraheight/(2*pic.yunitsize);
  extrawidth = cm*extrawidth/(2*pic.xunitsize);
  yequals(pic, Lx, 0, xmin-extrawidth, xmax+extrawidth, p, above, arrow=arrow);
  yequals(pic, 0, xmin, xmax, p, xticks, above);
  xequals(pic, Ly, 0, ymin-extraheight, ymax+extraheight, p, above, arrow=arrow);
  xequals(pic, 0, ymin, ymax, p, yticks, above);
}

void labeloij(picture pic=currentpicture,
              Label Lo=Label("$O$",NoFill),
              Label Li=Label("$\vec{\imath}$",NoFill),
              Label Lj=Label("$\vec{\jmath}$",NoFill),
              pair diro=SW, pair diri=S, pair dirj=W,
              pen p=scale(2)*currentpen,
              filltype filltype=NoFill, arrowbar arrow=Arrow(2mm))
{
  if (Lo.filltype==NoFill) Lo.filltype=filltype;
  if (Li.filltype==NoFill) Li.filltype=filltype;
  if (Lj.filltype==NoFill) Lj.filltype=filltype;
  labelx(pic, Lo, 0, diro, p);
  draw(pic, Li, (0,0)--(1,0), diri, p, arrow);
  draw(pic, Lj, (0,0)--(0,1), dirj, p, arrow);
  dot(pic, (0,0), dotsize(p)+p);
}

void labeloIJ(picture pic=currentpicture,
              Label Lo=Label("$O$",NoFill),
              Label LI=Label("$I$",NoFill),
              Label LJ=Label("$J$",NoFill),
              pair diro=SW, pair dirI=S, pair dirJ=W,
              pen p=currentpen,
              filltype filltype=NoFill, arrowbar arrow=Arrow)
{
  if (Lo.filltype==NoFill) Lo.filltype=filltype;
  if (LI.filltype==NoFill) LI.filltype=filltype;
  if (LJ.filltype==NoFill) LJ.filltype=filltype;
  labelx(pic, LI, 1, dirI, p);
  labely(pic, LJ, 1, dirJ, p);
  labelx(pic, Lo, 0, diro, p);
  dot(pic, (0,0), dotsize(p)+p);
}

graphicrules(xunit=1cm, yunit=3cm,
             xmin=-2pi, xmax=2pi, ymin=-1, ymax=1);
grid(xStep=pi/2, xstep=pi/4, yStep=.5, ystep=.25);
cartesianaxis(xticks=Ticks(Label(UnFill),labelfrac(factor=pi,symbol="\pi",symbolin=true, zero=false),Step=pi/2, step=pi/4, ptick=grey),
              yticks=Ticks(Label(UnFill),labelfrac(zero=false),Step=.5,step=.25, ptick=grey), arrow=None);
dot("$O$",(0,0),2SW);

Étiquettes : , , , , ,


Asymptote using graph.asy – fig0250

Category: Asymptote,Examples 2D,graph.asyPh. Ivaldi @ 2 h 10 min

Figure 0024
(Compiled with Asymptote version 2.14svn-r5318)
    
size(10cm,0);
import contour;
import graph;

xlimits( -3, 3);
ylimits( -3, 3);
yaxis( "$y$" , Ticks());
xaxis( "$x$", Ticks());

real f(real x, real y) {return x*y;}

draw(contour(f,(-3,-3),(3,3),new real[] {1}));

Étiquettes : , , , ,


Asymptote using graph.asy – fig0260

Category: Asymptote,Examples 2D,graph.asyPh. Ivaldi @ 3 h 10 min

Figure 0025
(Compiled with Asymptote version 2.14svn-r5318)
    
size(10cm,0);
import contour;
import stats;
import graph;

xlimits( -5, 5);  
ylimits( -4, 5);  
yaxis( "$y$" , Ticks(Label(currentpen+fontsize(8),align=E)));
xaxis( "$x$", Ticks(Label(currentpen+fontsize(8))));

real f(real x, real y) {return x^2-x-y^2+3y-6;}

int min=-5,
  max=5,
  n=max-min+1;

real[] value=sequence(min,max);

pen[] p=sequence(new pen(int i) {
    return (value[i] >= 0 ? solid : dashed) + 
    (value[i] >= 0 ? (value[i]/max)*red : (value[i]/min)*blue) + 
    fontsize(4);
  },n);

Label[] Labels=sequence(new Label(int i) {
    return Label(value[i] != 0 ? (string) value[i] : "",Relative(unitrand()),(0,0),
                 UnFill(1bp));
  },n);

draw(Labels,contour(f,(-5,-5),(5,5),value),p);

Étiquettes : , , , , ,


Asymptote using graph.asy – fig0270

Category: Asymptote,Examples 2D,graph.asyPh. Ivaldi @ 4 h 10 min

Figure 0026
(Compiled with Asymptote version 2.14svn-r5318)
    
//Author: John Bowman
import graph;

size(250,200,IgnoreAspect);

real Sin(real t, real w) {return sin(w*t);}

draw(graph(new real(real t) {return Sin(t,pi);},0,1),blue,"$\sin(\pi x)$");
draw(graph(new real(real t) {return Sin(t,2pi);},0,1),red,"$\sin(2\pi x)$");

xaxis("$x$",BottomTop,Ticks);
yaxis("$y$",LeftRight,Ticks);

attach(legend(),point(E),20E,UnFill);

Étiquettes : , ,


Asymptote using graph.asy – fig0280

Category: Asymptote,Examples 2D,graph.asyPh. Ivaldi @ 5 h 10 min

Figure 0027
(Compiled with Asymptote version 2.14svn-r5318)
    
import graph;

size(10cm,6cm,IgnoreAspect);

typedef real realfcn(real);
realfcn F(real p){
  return new real(real x){return sin(x)/sqrt(p);};
};

real pmax=5;
for (real p=1; p<=pmax; p+=1)
  {
    draw(graph(F(p),-2pi,2pi),
         ((p-1)/(pmax-1)*blue+(1-(p-1)/(pmax-1))*red),
         "$\frac{\sin(x)}{\sqrt{" + (string) p +"}}$");
  }

xlimits(-2pi,2pi);
ylimits(-1,1);

xaxis("$x$",BottomTop,Ticks);
yaxis("$y$",LeftRight,Ticks);

attach(legend(),point(E),20E,UnFill);

Étiquettes : , , , ,


Asymptote using graph.asy – fig0290

Category: Asymptote,Examples 2D,graph.asyPh. Ivaldi @ 6 h 10 min

Figure 0028
(Compiled with Asymptote version 2.14svn-r5318)
    
import graph;
size(10cm);

xaxis("$x$", -2*pi,2*pi, Arrow);
yaxis("$y$", -4,4, Arrow);

typedef real realfcn(real); // Define new type: real function of real

realfcn TPC(int n) { //Return Taylor polynomial (degrees 2*n) of cos
  return new real(real x) {
    return sum(sequence(new real(int m){return (-1)^m*x^(2*m)/gamma(2*m+1);}, n+1));
  };
}
draw(graph(cos,-2pi,2pi), linewidth(2bp), legend="$\cos$");

int n=6; // Number of curves
pen[] p={palered, lightred, red, blue, purple, green};
p.cyclic=true; // p[6]=p[0], p[7]=p[1], etc...

for (int i=0; i < n; ++i) {
  draw(graph(TPC(i),-2*pi,2*pi), bp+p[i], legend="$T_{"+(string)i+"}$");
}

xlimits(-2*pi,2*pi, Crop);
ylimits(-4,4, Crop);

attach(legend(linelength=3mm),point(E),5E);
shipout(bbox(Fill(lightgrey)));

Étiquettes : , , , ,


Asymptote using graph.asy – fig0300

Category: Asymptote,Examples 2D,graph.asyPh. Ivaldi @ 7 h 10 min

Figure 0029
(Compiled with Asymptote version 2.14svn-r5318)
    
//Beta distribution
import graph;
unitsize(10cm,3cm);

typedef real realfcn(real);

realfcn betaFunction(real alpha, real beta){
  return new real(real x){
    return gamma(alpha+beta)/(gamma(alpha)+gamma(beta))*x^(alpha-1)*(1-x)^(beta-1);
  };
};


real[][] ab=new real[][] {{0.5,0.5},{5,1},{1,3},{2,2},{2,5}};
pen[] p=new pen[] {0.8*red, 0.8*green, 0.8*blue, 0.8*magenta, black};

for (int i=0; i < 5; ++i) {
  draw(graph(betaFunction(ab[i][0],ab[i][1]),1e-5,1-1e-5), bp+p[i],
       legend="$\alpha="+(string)ab[i][0]+",\;\beta="+(string)ab[i][1]+"$");
}

xlimits(0,1,Crop);
ylimits(0,2.6,Crop);

xaxis("$x$",BottomTop,linewidth(bp),Ticks);
yaxis("$y$",LeftRight,linewidth(bp),Ticks(Step=0.2));

attach(scale(0.75)*legend(linelength=3mm),point(N),5S,UnFill);

Étiquettes : , , ,


Asymptote using graph.asy – fig0310

Category: Asymptote,Examples 2D,graph.asyPh. Ivaldi @ 8 h 10 min

Figure 0030
(Compiled with Asymptote version 2.14svn-r5318)
    
// Other examples of interpolations can be found here
import graph;
unitsize(1cm);

typedef real hermite(real);

hermite hermite(pair [] m, real [] d)
{/*DOC Retourne la fonction polynôme de Hermite
   passant par les points m(x_i,y_i) de nombre dérivée d_i en ce point.
   Return Hermite polynomial interpolation function
   passing by the points m (x_i, y_i) of derived number d_i in this point.
   DOC*/
  return new real(real x){
    int n=m.length;
    if (n != d.length) abort("Hermite: nombres de paramètres incorrectes.");
    real q,qk,s,y=0;
    for (int k=0; k<n ; ++k) {
      real q=1, qk=1, s=0;
      for (int j=0; j<n; ++j)
        {
          if (j!=k){
            q=q*(x-m[j].x)^2;
            qk=qk*(m[k].x-m[j].x)^2;
            s=s+1/(m[k].x-m[j].x);
          }
        }
      y=y+q/qk*(m[k].y+(x-m[k].x)*(d[k]-2*s*m[k].y));
    }
    return y;
  };
}

pair[] m;
real[] d;
int nbpt=5;
real xmin=-2pi,
  xmax=2pi,
  l=xmax-xmin,
  step=l/(nbpt+1);
for (int i=1; i<=nbpt; ++i)
  {
    real x=xmin+i*step;
    m.push((x,sin(x)));
    draw(m[m.length-1],linewidth(2mm));
    d.push(cos(x));
  }

xlimits(-2pi,2pi);
ylimits(-2,2);
xaxis("$x$",BottomTop,Ticks);
yaxis("$y$",LeftRight,Ticks);

draw(graph(sin,xmin,xmax),1mm+.8red,"$x\longmapsto{}\sin x$");
draw(graph(hermite(m,d),xmin,xmax),"$x\longmapsto{}H(x)$");

attach(legend(),point(10S),30S);

Étiquettes : , , , ,


Asymptote using graph.asy – fig0320

Category: Asymptote,Examples 2D,graph.asyPh. Ivaldi @ 9 h 10 min

Figure 0031
(Compiled with Asymptote version 2.14svn-r5318)
    
import graph;
import interpolate;

size(15cm,10cm,IgnoreAspect);

real[] xpt,ypt;
real [] xpt={1, 2, 4, 5, 7, 8, 10};
real [] ypt={1, 2, 2, 3, 1, 0.5, 3};


horner h=diffdiv(xpt,ypt);
fhorner L=fhorner(h);

scale(false,true);

pen p=linewidth(1);

draw(graph(L,min(xpt),max(xpt)),dashed+black+p,"Lagrange interpolation");
draw(graph(xpt,ypt,Hermite(natural)),red+p,"natural spline");
draw(graph(xpt,ypt,Hermite(monotonic)),blue+p,"monotone spline");
xaxis("$x$",BottomTop,LeftTicks(Step=1,step=0.25));
yaxis("$y$",LeftRight,RightTicks(Step=5));
dot(pairs(xpt,ypt),4bp+0.7black);

attach(legend(),point(10S),30S);

Étiquettes : , , ,


Asymptote using graph.asy – fig0330

Category: Asymptote,Examples 2D,graph.asyPh. Ivaldi @ 10 h 10 min

Figure 0032
(Compiled with Asymptote version 2.14svn-r5318)
    
import slopefield;
import graph;
size(8cm,0);
real f(real t) {return exp(-t^2);}
defaultpen();

xlimits( 0,1);  
ylimits( 0,1);  
yaxis( "$y$" ,LeftRight, RightTicks);
xaxis( "$x$", Ticks());
draw(graph(f,0,1),"$x\longmapsto{}e^{-x^2}$");
draw(curve((0,0),f,(0,0),(1,10)),linecap(0)+red,"$\displaystyle x\longmapsto\int_{0}^{x}e^{-t^2}\;dt$");

//Test with three values calculated with Maxima:
dot((.25,0.13816319508411845*sqrt(pi))^^(.5 , 0.26024993890652326*sqrt(pi)));
dot((.75, 0.3555778168267576*sqrt(pi)));

attach(legend(),point(10S),30S);

Étiquettes : , , , ,


Asymptote using graph.asy – fig0350

Category: Asymptote,Examples 2D,graph.asyPh. Ivaldi @ 12 h 10 min

Figure 0034
(Compiled with Asymptote version 2.14svn-r5318)
    
// From Asymptote's FAQ
import graph; 
 
real width=15cm; 
real aspect=0.3; 
 
picture pic1,pic2; 
 
size(pic1,width,aspect*width,IgnoreAspect); 
size(pic2,width,aspect*width,IgnoreAspect); 
 
scale(pic1,false); 
scale(pic2,false); 
 
real xmin1=6; 
real xmax1=9; 
real xmin2=8; 
real xmax2=16; 
 
real a1=1; 
real a2=0.001; 
 
real f1(real x) {return a1*sin(x/2*pi);} 
real f2(real x) {return a2*sin(x/4*pi);} 
 
draw(pic1,graph(pic1,f1,xmin1,xmax1)); 
draw(pic2,graph(pic2,f2,xmin2,xmax2)); 
 
xaxis(pic1,Bottom,LeftTicks()); 
yaxis(pic1,"$f_1(x)$",Left,RightTicks); 
 
xaxis(pic2,Bottom,LeftTicks(Step=4)); 
yaxis(pic2,"$f_2(x)$",Left,RightTicks); 
 
yequals(pic1,0,Dotted); 
yequals(pic2,0,Dotted); 
 
pair min1=point(pic1,SW); 
pair max1=point(pic1,NE); 
 
pair min2=point(pic2,SW); 
pair max2=point(pic2,NE); 
 
real scale=(max1.x-min1.x)/(max2.x-min2.x); 
real shift=min1.x/scale-min2.x; 
 
transform t1 = pic1.calculateTransform(); 
transform t2 = pic2.calculateTransform(); 
transform T=xscale(scale*t1.xx)*yscale(t2.yy); 
 
add(pic1.fit()); 
real height=truepoint(N).y-truepoint(S).y; 
add(shift(0,-height)*(shift(shift)*pic2).fit(T)); 

Étiquettes : , , , ,


Official Asymptote example – Bode

Category: Asymptote,Official Gallery One-PagerPh. Ivaldi @ 9 h 57 min

Figure 0016
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import graph;
texpreamble("\def\Arg{\mathop {\rm Arg}\nolimits}");

size(10cm,5cm,IgnoreAspect);

real ampl(real x) {return 2.5/(1+x^2);}
real phas(real x) {return -atan(x)/pi;}

scale(Log,Log);
draw(graph(ampl,0.01,10));
ylimits(0.001,100);

xaxis("$\omega\tau_0$",BottomTop,LeftTicks);
yaxis("$|G(\omega\tau_0)|$",Left,RightTicks);

picture q=secondaryY(new void(picture pic) {
    scale(pic,Log,Linear);
    draw(pic,graph(pic,phas,0.01,10),red);
    ylimits(pic,-1.0,1.5);
    yaxis(pic,"$\Arg G/\pi$",Right,red,
          LeftTicks("$% #.1f$",
                    begin=false,end=false));
    yequals(pic,1,Dotted);
  });
label(q,"(1,0)",Scale(q,(1,0)),red);
add(q);


Étiquettes : , , ,


Official Asymptote example – floor

Category: Asymptote,Official Gallery One-PagerPh. Ivaldi @ 9 h 57 min

Figure 0070
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import graph;
unitsize(1cm);

real Floor(real x) {return floor(x);}

pair[] Close;
pair[] Open;

bool3 branch(real x) {
  static real lasty;
  static bool first=true;
  real y=floor(x);
  bool samebranch=first || lasty == y; 
  first=false;
  if(samebranch) lasty=x;
  else {
    Close.push((x,lasty));
    Open.push((x,y));
  }
  lasty=y;
  return samebranch ? true : default;
};

draw(graph(Floor,-5.5,5.5,500,branch)); 
axes("$x$",rotate(0)*"$\lfloor x\rfloor$",red);

dot(Close);
dot(Open,UnFill);

Étiquettes : ,


Official Asymptote example – gamma

Category: Asymptote,Official Gallery One-PagerPh. Ivaldi @ 15 h 57 min

Figure 0077
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import graph;
size(300,IgnoreAspect);

bool3 branch(real x)
{
  static int lastsign=0;
  if(x <= 0 && x == floor(x)) return false;
  int sign=sgn(gamma(x));
  bool b=lastsign == 0 || sign == lastsign;
  lastsign=sign;
  return b ? true : default;
}

draw(graph(gamma,-4,4,n=2000,branch),red);
 
scale(false);
xlimits(-4,4);
ylimits(-6,6);
crop();

xaxis("$x$",RightTicks(NoZero));
yaxis(LeftTicks(NoZero));

label("$\Gamma(x)$",(1,2),red);

Étiquettes : ,


Official Asymptote example – jump

Category: Asymptote,Official Gallery One-PagerPh. Ivaldi @ 13 h 57 min

Figure 0104
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import graph;
size(4inches,0);

real f1(real x) {return (1+x^2);} 
real f2(real x) {return (4-x);}

xaxis("$x$",LeftTicks,Arrow);
yaxis("$y$",RightTicks,Arrow);

draw("$y=1+x^2$",graph(f1,-2,1)); 
dot((1,f1(1)),UnFill);

draw("$y=4-x$",graph(f2,1,5),LeftSide,red,Arrow);
dot((1,f2(1)),red);

Étiquettes : ,


Official Asymptote example – legend

Category: Asymptote,Official Gallery One-PagerPh. Ivaldi @ 23 h 57 min

Figure 0119
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import graph; 
size(8cm,6cm,IgnoreAspect); 
 
typedef real realfcn(real); 
realfcn F(real p) { 
  return new real(real x) {return sin(p*x);}; 
}; 
 
for(int i=1; i < 5; ++i)
  draw(graph(F(i*pi),0,1),Pen(i),
       "$\sin("+(i == 1 ? "" : (string) i)+"\pi x)$"); 
xaxis("$x$",BottomTop,LeftTicks); 
yaxis("$y$",LeftRight,RightTicks(trailingzero)); 
 
attach(legend(2),(point(S).x,truepoint(S).y),10S,UnFill); 

Étiquettes : , , ,


Official Asymptote example – lineargraph

Category: Asymptote,Official Gallery One-PagerPh. Ivaldi @ 2 h 57 min

Figure 0123
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import graph;

size(250,200,IgnoreAspect);

real Sin(real t) {return sin(2pi*t);}
real Cos(real t) {return cos(2pi*t);}

draw(graph(Sin,0,1),red,"$\sin(2\pi x)$");
draw(graph(Cos,0,1),blue,"$\cos(2\pi x)$");

xaxis("$x$",BottomTop,LeftTicks);
yaxis("$y$",LeftRight,RightTicks(trailingzero));

label("LABEL",point(0),UnFill(1mm));

attach(legend(),truepoint(E),20E,UnFill);

Étiquettes : , , , ,


Official Asymptote example – lineargraph0

Category: Asymptote,Official Gallery One-PagerPh. Ivaldi @ 3 h 57 min

Figure 0122
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import graph;

size(400,200,IgnoreAspect);

real Sin(real t) {return sin(2pi*t);}
real Cos(real t) {return cos(2pi*t);}

draw(graph(Sin,0,1),red,"$\sin(2\pi x)$");
draw(graph(Cos,0,1),blue,"$\cos(2\pi x)$");

xaxis("$x$",BottomTop,LeftTicks);
yaxis("$y$",LeftRight,RightTicks(trailingzero));

label("LABEL",point(0),UnFill(1mm));

add(legend(),point(E),20E,UnFill);

Étiquettes : , , , ,


Official Asymptote example – log2graph

Category: Asymptote,Official Gallery One-PagerPh. Ivaldi @ 8 h 57 min

Figure 0127
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import graph;
size(200,IgnoreAspect);

// Base-2 logarithmic scale on y-axis:

real log2(real x) {static real log2=log(2); return log(x)/log2;}
real pow2(real x) {return 2^x;}

scaleT yscale=scaleT(log2,pow2,logarithmic=true);
scale(Linear,yscale);

real f(real x) {return 1+x^2;}

draw(graph(f,-4,4));

yaxis("$y$",ymin=1,ymax=f(5),RightTicks(Label(Fill(white))),EndArrow);
xaxis("$x$",xmin=-5,xmax=5,LeftTicks,EndArrow);

Étiquettes : , ,


Official Asymptote example – logdown

Category: Asymptote,Official Gallery One-PagerPh. Ivaldi @ 9 h 57 min

Figure 0129
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import graph;
size(200,IgnoreAspect);

real log10Down(real x) {return -log10(x);}
real pow10Down(real x) {return pow10(-x);}

scaleT LogDown=scaleT(log10Down,pow10Down,logarithmic=true);
scale(Linear,LogDown);

draw(graph(exp,-5,5));

yaxis("$y$",RightTicks(Label(Fill(white)),DefaultLogFormat),BeginArrow);
xaxis("$x$",LeftTicks(NoZero),EndArrow);

Étiquettes : , ,


Official Asymptote example – loggraph

Category: Asymptote,Official Gallery One-PagerPh. Ivaldi @ 10 h 57 min

Figure 0130
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import graph;

size(200,200,IgnoreAspect);

real f(real t) {return 1/t;}

scale(Log,Log);

draw(graph(f,0.1,10));

//xlimits(1,10,Crop);
//ylimits(0.1,1,Crop);

dot(Label("(3,5)",align=S),Scale((3,5)));

xaxis("$x$",BottomTop,LeftTicks);
yaxis("$y$",LeftRight,RightTicks);


Étiquettes : , ,


Official Asymptote example – loggrid

Category: Asymptote,Official Gallery One-PagerPh. Ivaldi @ 11 h 57 min

Figure 0131
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import graph;
size(200,200,IgnoreAspect);

real f(real t) {return 1/t;}

scale(Log,Log);
draw(graph(f,0.1,10),red);
pen thin=linewidth(0.5*linewidth());
xaxis("$x$",BottomTop,LeftTicks(begin=false,end=false,extend=true,
                                ptick=thin));
yaxis("$y$",LeftRight,RightTicks(begin=false,end=false,extend=true,
                                 ptick=thin));


Étiquettes : , , ,


Official Asymptote example – multicontour

Category: Asymptote,Official Gallery One-PagerPh. Ivaldi @ 2 h 57 min

Figure 0147
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import contour;

size(200);

real f(real x, real y) {return x^2-y^2;}
int n=10;
real[] c=new real[n];
for(int i=0; i < n; ++i) c[i]=(i-n/2)/n;

pen[] p=sequence(new pen(int i) {
    return (c[i] >= 0 ? solid : dashed)+fontsize(6pt);
  },c.length);

Label[] Labels=sequence(new Label(int i) {
    return Label(c[i] != 0 ? (string) c[i] : "",Relative(unitrand()),(0,0),
                 UnFill(1bp));
  },c.length);

draw(Labels,contour(f,(-1,-1),(1,1),c),p);

Étiquettes : , , , ,


Official Asymptote example – oneoverx

Category: Asymptote,Official Gallery One-PagerPh. Ivaldi @ 6 h 57 min

Figure 0153
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import graph;
size(200,IgnoreAspect);

real f(real x) {return 1/x;};

bool3 branch(real x)
{
  static int lastsign=0;
  if(x == 0) return false;
  int sign=sgn(x);
  bool b=lastsign == 0 || sign == lastsign; 
  lastsign=sign;
  return b ? true : default;
}

draw(graph(f,-1,1,branch));
axes("$x$","$y$",red);

Étiquettes : , ,


Official Asymptote example – spiral

Category: Asymptote,Official Gallery One-PagerPh. Ivaldi @ 6 h 57 min

Figure 0215
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
size(0,150);
import graph;

real f(real t) {return exp(-t/(2pi));}

draw(polargraph(f,0,20*pi,operator ..));

xaxis("$x$",-infinity,1.3);
yaxis("$y$",-infinity,1);

labelx(1);
labelx("$e^{-1}$",1.0/exp(1),SE);

Étiquettes : , , ,


Official Asymptote example – tanh

Category: Asymptote,Official Gallery One-PagerPh. Ivaldi @ 20 h 57 min

Figure 0229
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import graph;
size(100,0);

real f(real x) {return tanh(x);}
pair F(real x) {return (x,f(x));}

xaxis("$x$");
yaxis("$y$");

draw(graph(f,-2.5,2.5,operator ..));

label("$\tanh x$",F(1.5),1.25*N);


Étiquettes : , ,


Official Asymptote example – xsin1x

Category: Asymptote,Official Gallery One-PagerPh. Ivaldi @ 2 h 57 min

Figure 0263
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import graph;
size(300,0);

real f(real x) {return (x != 0.0) ? x * sin(1.0 / x) : 0.0;}
pair F(real x) {return (x,f(x));}

xaxis("$x$",red);
yaxis(red);
draw(graph(f,-1.2/pi,1.2/pi,1000));
label("$x\sin\frac{1}{x}$",F(1.1/pi),NW);

picture pic;
size(pic,50,IgnoreAspect);
xaxis(pic,red);
yaxis(pic,red);
draw(pic,graph(pic,f,-0.1/pi,0.1/pi,1000));

add(new void(frame f, transform t) {
    frame G=shift(point(f,N+0.85W))*align(bbox(pic,blue),10SE);
    add(f,G);
    draw(f,t*box(min(pic,user=true),max(pic,user=true)),blue);
    draw(f,point(G,E)--t*point(pic,W),blue);
  });


Étiquettes : , ,


Animation with Asymptote – fig0080

Category: Animation,AsymptotePh. Ivaldi @ 15 h 20 min

Figure 0008
(Compiled with Asymptote version 1.86svn-r4626)
This animation is available in the Syracuse web site.
    
import graph3;
import animation;
import solids;

settings.render=0;
animation A;
A.global=false;

int nbpts=500;
real q=2/5;
real pas=5*2*pi/nbpts;
int angle=3;
real R=3;

unitsize(1cm);

real x(real t){return R*cos(q*t)*cos(t);}
real y(real t){return R*cos(q*t)*sin(t);}
real z(real t){return R*sin(q*t);}

triple[] P;
real t=-pi;
for (int i=0; i<nbpts; ++i) {
  t+=pas;
  P.push((x(t),y(t),z(t)));
}

currentprojection=orthographic((0,5,2));
currentlight=(3,3,5);

pen p=rgb(0.1,0.1,0.58);
transform3 t=rotate(angle,(0,0,0),(1,0.25,0.25));

filldraw(box((-R-0.5,-R-0.5),(R+0.5,R+0.5)), p, 3mm+black+miterjoin);

revolution r=sphere(O,R);
draw(surface(r),p);

for (int phi=0; phi<360; phi+=angle) {
  bool[] back,front;
  save();

  for (int i=0; i<nbpts; ++i) {
    P[i]=t*P[i];
    bool test=dot(P[i],currentprojection.camera) > 0;
    front.push(test);
  }

  draw(segment(P,front,operator ..),linewidth(1mm));
  draw(segment(P,!front,operator ..),grey);
  A.add();
  restore();
}

A.movie(options="-density 350 -resample 96 -quality 100 -depth 8 -strip");

Étiquettes : , , , , , ,


Animation with Asymptote – fig0090

Category: Animation,AsymptotePh. Ivaldi @ 16 h 20 min

Figure 0009
(Compiled with Asymptote version 1.86svn-r4626)
    
size(0,10cm);
import graph3;
import animation;
import solids;

currentlight.background=black;
settings.render=0;
animation A;
A.global=false;

int nbpts=500;
real q=2/5;
real pas=5*2*pi/nbpts;
int angle=4;
real R=0.5;
pen p=rgb(0.1,0.1,0.58);
triple center=(1,1,1);
transform3 T=rotate(angle,center,center+X+0.25*Y+0.3*Z);

real x(real t){return center.x+R*cos(q*t)*cos(t);}
real y(real t){return center.y+R*cos(q*t)*sin(t);}
real z(real t){return center.z+R*sin(q*t);}

currentprojection=orthographic(1,1,1);
currentlight=(0,center.y-0.5,2*(center.z+R));

triple U=(center.x+1.1*R,0,0), V=(0,center.y+1.1*R,0);
path3 xy=plane(U,V,(0,0,0));
path3 xz=rotate(90,X)*xy;
path3 yz=rotate(-90,Y)*xy;

triple[] P;
path3 curve;
real t=-pi;
for (int i=0; i < nbpts; ++i) {
  t+=pas;
  triple M=(x(t),y(t),z(t));
  P.push(M);
  curve = curve..M;
}

curve=curve..cycle;

draw(surface(xy), grey);
draw(surface(xz), grey);
draw(surface(yz), grey);

triple xyc=(center.x,center.y,0);
path3 cle=shift(xyc)*scale3(R)*unitcircle3;
surface scle=surface(cle);
draw(scle, black);
draw(rotate(90,X)*scle, black);
draw(rotate(-90,Y)*scle, black);

draw(surface(sphere(center,R)), p);

triple vcam=1e5*currentprojection.camera-center;
for (int phi=0; phi<360; phi+=angle) {
  bool[] back,front;
  save();

  for (int i=0; i<nbpts; ++i) {
    P[i]=T*P[i];
    bool test=dot(P[i]-center,vcam) > 0;
    front.push(test);
  }

  curve=T*curve;
  draw(segment(P,front,operator ..), paleyellow);
  draw(segment(P,!front,operator ..),0.5*(paleyellow+p));
  draw((planeproject(xy)*curve)^^
       (planeproject(xz)*curve)^^
       (planeproject(yz)*curve), paleyellow);

  A.add();
  restore();
}

A.movie(options="-density 350 -resample 96 -quality 100 -depth 8 -strip");

Étiquettes : , , , , , , , ,


Animation with Asymptote – fig0100

Category: Animation,AsymptotePh. Ivaldi @ 17 h 20 min

Figure 0010
(Compiled with Asymptote version 1.86svn-r4626)
Movie flash (swf)
This animation is available in the Syracuse web site.
    
import animation;
import graph;

settings.tex="pdflatex";
settings.outformat="pdf";

unitsize(x=2cm,y=1.5cm);

typedef real realfcn(real);

real lambda=4;
real T=2;
real [] k=new real[3];
real [] w=new real[3];
k[0]=2pi/lambda;
w[0]=2pi/T;
real dk=-.5;
k[1]=k[0]-dk;
k[2]=k[0]+dk;
real dw=1;
w[1]=w[0]-dw;
w[2]=w[0]+dw;

real vp=w[1]/k[1];
real vg=dw/dk;

realfcn F(real x) {
  return new real(real t) {
    return cos(k[1]*x-w[1]*t)+cos(k[2]*x-w[2]*t);
  };
};

realfcn G(real x) {
  return new real(real t) {
    return 2*cos(0.5*(k[2]-k[1])*x+0.5*(w[1]-w[2])*t);
  };
};

realfcn operator -(realfcn f) {return new real(real t) {return -f(t);};};

animation A;

real tmax=abs(2pi/dk);
real xmax=abs(2pi/dw);

pen envelope=0.8*blue;
pen fillpen=lightgrey;

int n=50;
real step=tmax/(n-1);
for(int i=0; i < n; ++i) {
  save();
  real t=i*step;
  real a=xmax*t/tmax-xmax/pi;
  real b=xmax*t/tmax;
  path f=graph(F(t),a,b);
  path g=graph(G(t),a,b);
  path h=graph(-G(t),a,b);
  fill(buildcycle(reverse(f),g),fillpen);
  draw(f);
  draw(g,envelope);
  draw(h,envelope);
  A.add();
  restore();
}

for(int i=0; i < n; ++i) {
  save();
  real t=i*step;
  real a=-xmax/pi;
  real b=xmax;
  path f=graph(F(t),a,b);
  path g=graph(G(t),a,b);
  path h=graph(-G(t),a,b);
  path B=box((-xmax/pi,-2),(xmax,2));
  fill(buildcycle(reverse(f),g,B),fillpen);
  fill(buildcycle(f,g,reverse(B)),fillpen);
  draw(f);
  draw(g,envelope);
  draw(h,envelope);
  A.add();
  restore();
}

A.movie();

Étiquettes : , , , ,