馃挴

Gr脿fics amb Asymptote

Exemples r脿pids

  1. Entrar al compilador online
  1. Copiar i enganxar-hi els codis que trobareu m茅s avall
  1. Donar-li a [Run] a dalt a l鈥檈squerra
  1. Seleccionar [SVG] o [PDF] com a ouput i clicar [Download]

Funci贸 random

notion image
Codi
import graph; size(0,150); int a=-1, b=1; real f(real x) {return x^3-x+2;} real g(real x) {return x^2;} draw(graph(f,a,b,operator ..),red); draw(graph(g,a,b,operator ..),blue); xaxis(); int n=5; real width=(b-a)/(real) n; for(int i=0; i <= n; ++i) { real x=a+width*i; draw((x,g(x))--(x,f(x))); } labelx("$a$",a); labelx("$b$",b); draw((a,0)--(a,g(a)),dotted); draw((b,0)--(b,g(b)),dotted); real m=a+0.73*(b-a); arrow("$f(x)$",(m,f(m)),N,red); arrow("$g(x)$",(m,g(m)),E,0.8cm,blue); int j=2; real xi=b-j*width; real xp=xi+width; real xm=0.5*(xi+xp); pair dot=(xm,0.5*(f(xm)+g(xm))); dot(dot,darkgreen+4.0); arrow("$\left(x,\frac{f(x)+g(x)}{2}\right)$",dot,NE,2cm,darkgreen);

Mapa conceptual

notion image
Codi
size(0,22cm); texpreamble(" \usepackage{bm} \def\v{\bm} \def\grad{\v\nabla} \def\cross{{\v\times}} \def\curl{\grad\cross} \def\del{\nabla} "); defaultpen(fontsize(10pt)); real margin=1.5mm; object IC=draw("initial condition $\v U_0$",box,(0,1), margin,black,FillDraw(palegray)); object Adv0=draw("Lagrangian state $\v U(t)$",ellipse,(1,1), margin,red,FillDraw(palered)); object Adv=draw("Lagrangian prediction $\v U(t+\tau)$",ellipse,(1,0), margin,red,FillDraw(palered)); object AdvD=draw("diffused parcels",ellipse,(1.8,1), margin,red,FillDraw(palered)); object Ur=draw("rearranged $\v \widetilde U$",box,(0,0), margin,orange+gray,FillDraw(paleyellow)); object Ui=draw("interpolated $\v \widetilde U$",box,(1,-1), margin,blue,FillDraw(paleblue)); object Crank=draw("${\cal L}^{-1}(-\tau){\cal L}(\tau)\v \widetilde U$", box,(0.5,-1),margin,blue,FillDraw(paleblue)); object CrankR=draw("${\cal L}^{-1}(-\tau){\cal L}(\tau)\v \widetilde U$", box,(0,-1),margin,orange+gray,FillDraw(paleyellow)); object Urout=draw(minipage("\center{Lagrangian rearranged solution~$\v U_R$}", 100pt),box,(0,-2),margin,orange+gray, FillDraw(paleyellow)); object Diff=draw("$\v D\del^2 \v \widetilde U$",box,(0.75,-1.5), margin,blue,FillDraw(paleblue)); object UIout=draw(minipage("\center{semi-Lagrangian solution~$\v U_I$}",80pt), box,(0.5,-2),margin,FillDraw(palered+paleyellow)); object psi=draw("$\psi=\del^{-2}\omega$",box,(1.6,-1), margin,darkgreen,FillDraw(palegreen)); object vel=draw("$\v v=\v{\hat z} \cross\grad\psi$",box,(1.6,-0.5), margin,darkgreen,FillDraw(palegreen)); add(new void(frame f, transform t) { pair padv=0.5*(point(Adv0,S,t)+point(Adv,N,t)); picture pic; draw(pic,"initialize",point(IC,E,t)--point(Adv0,W,t),RightSide,Arrow, PenMargin); draw(pic,minipage("\flushright{advect: Runge-Kutta}",80pt), point(Adv0,S,t)--point(Adv,N,t),RightSide,red,Arrow,PenMargin); draw(pic,Label("Lagrange $\rightarrow$ Euler",0.45), point(Adv,W,t)--point(Ur,E,t),5LeftSide,orange+gray, Arrow,PenMargin); draw(pic,"Lagrange $\rightarrow$ Euler",point(Adv,S,t)--point(Ui,N,t), RightSide,blue,Arrow,PenMargin); draw(pic,point(Adv,E,t)--(point(AdvD,S,t).x,point(Adv,E,t).y),red, Arrow(Relative(0.7)),PenMargin); draw(pic,minipage("\flushleft{diffuse: multigrid Crank--Nicholson}",80pt), point(Ui,W,t)--point(Crank,E,t),5N,blue,MidArrow,PenMargin); draw(pic,minipage("\flushleft{diffuse: multigrid Crank--Nicholson}",80pt), point(Ur,S,t)--point(CrankR,N,t),LeftSide,orange+gray,Arrow,PenMargin); draw(pic,"output",point(CrankR,S,t)--point(Urout,N,t),RightSide, orange+gray,Arrow,PenMargin); draw(pic,point(Ui,S,t)--point(Diff,N,t),blue,MidArrow,PenMargin); draw(pic,point(Crank,S,t)--point(Diff,N,t),blue,MidArrow,PenMargin); label(pic,"subtract",point(Diff,N,t),12N,blue); draw(pic,Label("Euler $\rightarrow$ Lagrange",0.5), point(Diff,E,t)--(point(AdvD,S,t).x,point(Diff,E,t).y)-- (point(AdvD,S,t).x,point(Adv,E,t).y),RightSide,blue, Arrow(position=1.5),PenMargin); dot(pic,(point(AdvD,S,t).x,point(Adv,E,t).y),red); draw(pic,(point(AdvD,S,t).x,point(Adv,E,t).y)--point(AdvD,S,t),red,Arrow, PenMargin); draw(pic,"output",point(Crank,S,t)--point(UIout,N,t),RightSide,brown,Arrow, PenMargin); draw(pic,Label("$t+\tau\rightarrow t$",0.45), point(AdvD,W,t)--point(Adv0,E,t),2.5LeftSide,red,Arrow,PenMargin); draw(pic,point(psi,N,t)--point(vel,S,t),darkgreen,Arrow,PenMargin); draw(pic,Label("self-advection",4.5),point(vel,N,t)-- arc((point(vel,N,t).x,point(Adv,E,t).y),5,270,90)-- (point(vel,N,t).x,padv.y)-- padv,LeftSide,darkgreen,Arrow,PenMargin); draw(pic,Label("multigrid",0.5,S),point(Ui,E,t)--point(psi,W,t),darkgreen, Arrow,PenMargin); add(f,pic.fit()); });

Coordenades polars

notion image
Codi
import math; import graph; size(0,100); real f(real t) {return 2*cos(t);} pair F(real x) {return (x,f(x));} draw(polargraph(f,0,pi,operator ..)); defaultpen(fontsize(10pt)); xaxis("$x$"); yaxis("$y$"); real theta=radians(50); real r=f(theta); draw("$\theta$",arc((0,0),0.5,0,degrees(theta)),red,Arrow,PenMargins); pair z=polar(r,theta); draw(z--(z.x,0),dotted+red); draw((0,0)--(z.x,0),dotted+red); label("$r\cos\theta$",(0.5*z.x,0),0.5*S,red); label("$r\sin\theta$",(z.x,0.5*z.y),0.5*E,red); dot("$(x,y)$",z,N); draw("r",(0,0)--z,0.5*unit(z)*I,blue,Arrow,DotMargin); dot("$(a,0)$",(1,0),NE); dot("$(2a,0)$",(2,0),NE);

Diagrama de Feynman

notion image
Codi
import feynman; // set default line width to 0.8bp currentpen = linewidth(0.8); // scale all other defaults of the feynman module appropriately fmdefaults(); // define vertex and external points real L = 50; pair zl = (-0.75*L,0); pair zr = (+0.75*L,0); pair xu = zl + L*dir(+120); pair xl = zl + L*dir(-120); pair yu = zr + L*dir(+60); pair yl = zr + L*dir(-60); // draw propagators and vertices drawFermion(xu--zl); drawFermion(zl--xl); drawPhoton(zl--zr); drawFermion(yu--zr); drawFermion(zr--yl); drawVertex(zl); drawVertex(zr); // draw momentum arrows and momentum labels drawMomArrow(xl--zl, Relative(left)); label(Label("$k'$",2RightSide), xl--zl); label(Label("$k$",2LeftSide), xu--zl); drawMomArrow(zl--zr, Relative(left)); label(Label("$q$",2RightSide), zl--zr); drawMomArrow(zr--yu, Relative(right)); label(Label("$p'$",2LeftSide), zr--yu); label(Label("$p$",2RightSide), zr--yl); // draw particle labels label("$e^-$", xu, left); label("$e^+$", xl, left); label("$\mu^+$", yu, right); label("$\mu^-$", yl, right);

Funci贸 anal铆tica

notion image
Codi
import graph; size(200,0); real f(real x) {return (x != 0) ? sin(1/x) : 0;} real T(real x) {return 2/(x*pi);} real a=-4/pi, b=4/pi; int n=150,m=5; xaxis("$x$",red); yaxis(red); draw(graph(f,a,-T(m),n)--graph(f,-m,-(m+n),n,T)--(0,f(0))--graph(f,m+n,m,n,T)-- graph(f,T(m),b,n)); label("$\sin\frac{1}{x}$",(b,f(b)),SW);

Funci贸 Gamma

notion image
Codi
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);

Diagrama de barres

notion image
Codi
import graph; import stats; size(400,200,IgnoreAspect); int n=10000; real[] a=new real[n]; for(int i=0; i < n; ++i) a[i]=Gaussrand(); draw(graph(Gaussian,min(a),max(a)),blue); // Optionally calculate "optimal" number of bins a la Shimazaki and Shinomoto. int N=bins(a); histogram(a,min(a),max(a),N,normalize=true,low=0,lightred,black,bars=true); xaxis("$x$",BottomTop,LeftTicks); yaxis("$dP/dx$",LeftRight,RightTicks(trailingzero));

Camp vectorial

notion image
Codi
import graph; size(100); pair a=(0,0); pair b=(2pi,2pi); path vector(pair z) {return (sin(z.x),cos(z.y));} add(vectorfield(vector,a,b));

Fua fua a veure si funcions

/
Codi
import graph3; import palette; size(200,300,keepAspect=false); //settings.nothin=true; currentprojection=orthographic(10,10,30); currentlight=(10,10,5); triple f(pair t) {return (exp(t.x)*cos(t.y),exp(t.x)*sin(t.y),t.y);} surface s=surface(f,(-4,-2pi),(0,4pi),8,16,Spline); s.colors(palette(s.map(zpart),Rainbow())); draw(s,render(merge=true));

Prova gr脿fics interactius Flourish

Mapa lios
Quiz
Un altre