11.07.2015 Views

Advanced Programming Guide

Advanced Programming Guide

Advanced Programming Guide

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

6.6 Vector Field Plots • 265> vectorfieldplot := proc( F, r1, r2 )> local v, m, n, a, b, dx, dy, opts, p, v1, v2,> L, i, j, norms, colorinfo,> xscale, yscale, mscale;>> v := makevectors( F, r1, r2, args[4..nargs] );> v1 := v[3]; v2 := v[4];> n := nops(v1); m := nops( v1[1] );>> a,b,dx,dy := domaininfo(v[1], v[2], m, n);>> # Determine the function used for coloring the arrows.> opts := [ args[ 4..nargs] ];> if not hasoption( opts, color, colorinfo, ’opts’ ) then> # Default coloring will be via> # the scaled magnitude of the vectors.> L := max( seq( seq( v1[j][i]^2 + v2[j][i]^2,> i=1..m ), j=1..n ) );> colorinfo := ( F[1]^2 + F[2]^2 )/L;> end if;>> # Generate the information needed to color the arrows.> colorinfo := convert( colorinfo, ’colorgrid’,> r1, r2, op(opts) );>> # Get all the norms of the vectors using zip.> norms := zip( (x,y) -> zip( (u,v)->> if u=0 and v=0 then 1 else sqrt(u^2 + v^2) end if,> x, y), v1, v2);> # Normalize v1 and v2 (again using zip ).> v1 := zip( (x,y) -> zip( (u,v)-> u/v, x, y),> v1, norms );>> v2 := zip( (x,y) -> zip( (u,v)-> u/v, x, y),> v2, norms );>> # Generate scaling information and plot data structure.> xscale := dx/2.0; yscale := dy/2.0;> mscale := max(xscale, yscale);>> PLOT( seq( seq( myarrow(> [ a + (i-1)*dx - v1[j][i]*xscale/2,> b + (j-1)*dy - v2[j][i]*yscale/2 ],> [ v1[j][i]*xscale, v2[j][i]*yscale ],> mscale/4, mscale/2, 1/3,> ’color’=colorinfo[j][i]> ), i=1..m ), j=1..n ) );> end proc:With this new version you can obtain the following plots.> vectorfieldplot( [y*cos(x*y), x*sin(x*y)],> x=0..Pi, y=0..Pi,grid=[15,20] );

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!