Asymptote using graph.asy – fig0270

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

 (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

 (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

 (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

 (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

 (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

 (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

 (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 : , , , ,

Official Asymptote example – HermiteSpline

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

 (Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */

import graph;

size(140mm,70mm,IgnoreAspect);
scale(false);
real[] x={1,3,4,5,6};
real[] y={1,5,2,0,4};

marker mark=marker(scale(1mm)*cross(6,false,r=0.35),red,Fill);

draw(graph(x,y,Hermite),"Hermite Spline",mark);
xaxis("$x$",Bottom,LeftTicks(x));
yaxis("$y$",Left,LeftTicks);
attach(legend(),point(NW),40S+30E,UnFill);



Étiquettes : , , , ,

Official Asymptote example – errorbars

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

 (Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */

import graph;

picture pic;
real xsize=200, ysize=140;
size(pic,xsize,ysize,IgnoreAspect);

pair[] f={(5,5),(50,20),(90,90)};
pair[] df={(0,0),(5,7),(0,5)};

errorbars(pic,f,df,red);
draw(pic,graph(pic,f),"legend",
marker(scale(0.8mm)*unitcircle,red,FillDraw(blue),above=false));

scale(pic,true);

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

picture pic2;
size(pic2,xsize,ysize,IgnoreAspect);

frame mark;
filldraw(mark,scale(0.8mm)*polygon(6),green,green);
draw(mark,scale(0.8mm)*cross(6),blue);

draw(pic2,graph(pic2,f),marker(mark,markuniform(5)));

scale(pic2,true);

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

yequals(pic2,55.0,red+Dotted);
xequals(pic2,70.0,red+Dotted);

// Fit pic to W of origin:

// Fit pic2 to E of (5mm,0):



Étiquettes : , , , ,

Official Asymptote example – legend

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

 (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

 (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

 (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));



Étiquettes : , , , ,

Official Asymptote example – spline

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

 (Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */

import graph;
import interpolate;

size(15cm,15cm,IgnoreAspect);

real a=1997, b=2002;
int n=5;
real[] xpt=a+sequence(n+1)*(b-a)/n;
real[] ypt={31,36,26,22,21,24};
horner h=diffdiv(xpt,ypt);
fhorner L=fhorner(h);

scale(false,true);

pen p=linewidth(1);

draw(graph(L,a,b),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+gray(0.3));

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



Étiquettes : , ,