Asymptote: Graphing

Revision as of 21:57, 19 July 2009 by Dojo (talk | contribs) (Created page with 'There are two parts to a graph. The axises codes: <pre><nowiki>Label f; f.p=fontsize(6); xaxis(-8,8,Ticks(f, 2.0)); yaxis(-8,8,Ticks(f, 2.0)); </nowiki></pre> This means …')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

There are two parts to a graph.

The axises codes:

Label f; 
f.p=fontsize(6); 
xaxis(-8,8,Ticks(f, 2.0)); 
yaxis(-8,8,Ticks(f, 2.0)); 

This means ticks at an interveral of 2.0 between -8 and 8 for both the x and y axises.

And the graph code:

real f(real x) 
{ 
return x^3; 
} 
draw(graph(f,-2,2));

The return $x^3$ is the function of the graph, and the draw command draws the graph from the $x$ coordinate of $x = - 2$ to $x = 2$.

[asy] import graph;  size(300);  Label f;  f.p=fontsize(6);  xaxis(-8,8,Ticks(f, 2.0));  yaxis(-8,8,Ticks(f, 2.0));  real f(real x)  {  return x^3;  }  draw(graph(f,-2,2)); [/asy]

We can also alter the ticks code. For example, if we want intervals of 2 labeled, then 0.5 in between each one, we can use the following code:

Label f; 
f.p=fontsize(6); 
xaxis(-8,8,Ticks(f, 2.0,0.5)); 
yaxis(-8,8,Ticks(f, 2.0,0.5)); 

[asy] Label f;  f.p=fontsize(6);  xaxis(-8,8,Ticks(f, 2.0,0.5));  yaxis(-8,8,Ticks(f, 2.0,0.5)); [/asy] This graph, like with any draw command, can have several attributes fixed to it, such as color:

[asy] import graph;  size(300);  Label f;  f.p=fontsize(6);  xaxis(-8,8,Ticks(f, 2.0));  yaxis(-8,8,Ticks(f, 2.0));  real f(real x)  {  return x^3;  }  draw(graph(f,-2,2),green+linewidth(1)); [/asy]

import graph; 
size(300); 
Label f; 
f.p=fontsize(6); 
xaxis(-8,8,Ticks(f, 2.0)); 
yaxis(-8,8,Ticks(f, 2.0)); 
real f(real x) 
{ 
return x^3; 
} 
draw(graph(f,-2,2),green+linewidth(1));

Remember, you can still draw normal functions, so you can create lines, circles and ellipses.