In this notebook, we will see some of the plot commands of Euler for functions in one variable. For a reference have a look at Reference for plot2d and plot3d Plots in Euler Most of the time, you will use the versatile function plot2d. This function can plot an expression in the variable "x", or a user defined function. Here is an expression.
>plot2d("x^3-x");
If your graphics window is hidden, either move it to the side of the text window, or press the tabulator key to move it to the foreground. Press the tabulator key a second time to return to the text window. If you are using the compressed one window interface of Euler, you need to press the TAB key to see the large plot. To insert the graphics into the text window, end the command line with ":".
>plot2d("x^4-x"):
You can change the grid or the frame styles. In the following example we use grid style number 3, and disable the frame. This can be done with frame=false (or simply <frame). Since we want more than the default 25 lines for the image, we use the insimg command and add the line height instead of a ":".
>plot2d("sin(x)/x",-4pi,4pi,grid=3,<frame); insimg(30);
For an overview of the grid style, have a look at the documentation of plot2d. Just double click on plot2d to see the HTML documentation in the browser.
>plot2d("exp(x)*sin(x)",-10,0):
To be able to zoom and move the plot, add the parameter >user. Then press the cursor keys to move along the function, the +/- keys to zoom, the space key to reset, and the return key to end the user action. You can also use the mouse to mark the plot region. <margin turns off the default margin between the plot and the frame.
>plot2d("x^3-x",<margin,title="Return, space, +/- or cursor keys", ... user=true):
plot2d evaluates the expression and determines the y-range automatically from the y-values. If you do not like the automatic y-range, you can set our own range. Use a,b,c,d to set the range [a,b] of x, and [c,d] of y.
>plot2d("sin(x)/x",a=-2*pi,b=2*pi,c=-2,d=2,thickness=2,grid=1):
Another way to scale the window is using a radius r. By default this is around (0,0). But it can also be around any point with coordinates (cx,cy).
>plot2d("x^x",r=1,cx=1,cy=1):
In the following example, we demonstrate how to add plots to another plot. We start by plotting the cosine function, and add Taylor expansions in different line styles and colors. Use >add to add a plot to another plot. The following command lines are connected by ... and form a multi-line command. We use the labelbox to explain the functions.
>plot2d("cos(x)",-pi,pi); plot2d("1-x^2/2",>add,style="--",color=4); ... plot2d("1-x^2/2+x^4/4!",>add,style="--",color=5); ... plot2d("1-x^2/2+x^4/4!-x^6/6!",>add,style="--",color=6); ... labelbox(["f","T2","T4","T6"],styles=["-","--","--","--"], ... colors=[1,4,5,6]):
To plot curves, plot2d can use two expressions in "x". The range of the parameter of the curve is [xmin,xmax]. The range of the plot can be set as usual with a,b,c,d or r. We remove the frame and the grid for the next plot.
>plot2d("cos(3*x)","sin(4*x)",r=1.1,xmin=0,xmax=2*pi,<grid,<frame, ... <margin):
Let us plot the functions cos(x) and x (dotted), and compute and plot the intersection of the two functions. We will later see how to plot points. For now we only plot a single point and add a label to the plot. We use a textbox to explain the result.
>plot2d("cos(x)",xmin=0,xmax=pi,>square); ... plot2d("x",>add,style="."); ... t=bisect("cos(x)-x",0,1); plot2d(t,t,>points,>add); ... label("Intersection",t+0.1,t+0.1); ... textbox(["Intersection of two functions","cos(x)=x"]):
Euler can also let the user drag values for a plot. We need a plot function depending on a vector of values.
>function plotf([a,b]) := plot2d("a*x^2+b*x";a,b,r=1);
Then we can use dragvalues() to display a box with values to drag. Each value needs a name, an initial value, a range and a number of stops. By default the number of stops is 100. Drag the numbers to the right or left. Return ends the dragging, space resets the values to the default values.
>dragvalues("plotf",["a","b"],[0.5,-0.2],[-1,1;-1,1],digits=2, ... heading="a*x^2+b*x",hcolor=black):
Here is another example using discrete values n=0,2,...,20.
>function plotf([n]) ...
plot2d("cos(x)",-2pi,2pi,c=-2,d=2); plot2d(&"taylor(cos(x),x,0,@n)",>add,color=blue,style="--"); endfunction
Now we have only one parameter n with 10 stops.
>vn:=dragvalues("plotf","n",4,[0,20],10,status="Drag n", ... heading="cos and Taylor expansion");
dragvalues() returns the vector of values. So we can plot it later.
>plotf(vn); labelbox(["cos","T_"+vn],["-","--"],[black,blue]):
Euler can also do implicit plots of functions of two variables. We plot the set
>plot2d("x^2+y^3+x*y",level=1,r=5):
Here is another example.
>expr &= (x^2+y^2-1)^3-x^2*y^3, ...
2 2 3 2 3 (y + x - 1) - x y
>plot2d(expr,niveau=0,r=2):
So far, we plotted expressions only. But plotting expressions is only an abbreviation of plotting data. The expressions are evaluated by plot2d using an adaptive algorithm and then these points are connected by lines. We can produce our own data too. In the following example, x is a vector of 201 values between -1 and 1. y is vector of function values. We get y simply by an expression using the matrix language. Then we connect the points x[i],y[i] by a line.
>x=-1:0.01:1; y=x^3-x; plot2d(x,y):
As you see, Euler scaled the plot range automatically so that it contains all data points. We can also plot the inverse function very easily. This time we use a square range with r=1.
>plot2d(y,x,r=1):
The same method can be used to plot spirals or other paths in the plane. In the example, we plot
>t=linspace(0,1,500); plot2d(t*cos(2pi*t),t*sin(2pi*t),r=1):
Such paths can be filled with the >filled parameter in different styles and colors. We produce the 7 corners of the heptagon with the matrix language of Euler.
>t=linspace(0,2pi,7); ... plot2d(cos(t),sin(t),r=1,>filled,style="/",fillcolor=red):
Now, we are going to plot several functions at once (not with >add). If we use several rows of x- and y-coordinates, Euler plots all rows and the corresponding columns as curves. For example, we compute all Bernstein polynomials
up to the degree 5 at once, using the Euler matrix language. The trick is to combine a row vector of t with a column vector of i. The result of the following command is a matrix with 101 values of each of the 6 functions in its rows.
>t=0:0.01:1; n=5; i=(0:5)'; s=bin(n,i)*t^i*(1-t)^(n-i); size(s)
[ 6 101 ]
Then we can plot the functions.
>plot2d(t,s,title="Bernstein Polynomials",a=0,b=1,c=0,d=1):
For point plots, you have to provide the x- and y-coordinates of the points, and add the >points parameter.
>x=1:25; plot2d(x,sqrt(x),>points):
In the following example, we plot the binomial distributions
with q=0.5 and q=0.3. We add one on top of the other with >add.
>n=10; i=0:n; ... q=0.5; plot2d(0:n,bin(n,i)*q^i*(1-q)^(n-i),>points,a=0,b=n,c=0,d=0.5); ... q=0.3; plot2d(0:n,bin(n,i)*q^i*(1-q)^(n-i), ... >points,style="o",>add,color=5); ... labelbox([" q=0.5"," q=0.3"],styles=["[]","o"],colors=[1,5],>points, ... x=0.7,y=0.3):
To get a bar plot, use x- and y-values, assuming the the x-values are increasing, and add the >bar parameter. The x-values determine the ends of the intervals, and the y-values the heights of the bars. In the following example, x runs only to 0.9. Here the right end of the last interval is added automatically using the last spacing 0.1.
>x=0:0.1:0.9; plot2d(x,(x+0.1)^2,style="/",a=0,b=1,c=0,d=1,>bar); ... plot2d(x,x^2,>add,>bar); title("Riemann Sums"):
There are many more bar plots in Euler. 00 - Overview of Plots in Euler E.g., The function histo(...) can produce bar plots from statistical data.
>plot2d(histo(normal(1,10000),v=-4:0.2:4),>bar):
The parameter logplot=1 shows the y axis in log scaling. logplot=1 is logarithmic in the y-axis only.
>plot2d("exp(x)-exp(0.5)*x+exp(1.2*x)",a=0,b=4,logplot=1):
Web plots are special plots, demonstrating the convergence of an iteration process. Here, we iterate x -> cos(x) 20 times.
>fwebplot("cos(x)",0,1,0.5,20):
There is also a parameter distribution=n, which shows the distribution of a data set. The sum of the hatched area will be 1.
>plot2d(normal(1,1000),distribution=20,a=-4,b=4,c=0,d=0.6,style="\/");
To compare with the density of the Gauß distribution, we plot this density function on top of the bar plot.
>plot2d("qnormal(x,0,1)",>add,thickness=2):
The parameter histogram=... does a similar bar plot, but shows the count, and not the proportion of the data.
>plot2d(random(1,600),histogram=10,style="\/"):
For a function, >filled will fill the area between the function and the x-axis.
>plot2d("qnormal",a=-5,b=5,c=0,d=0.5); ... plot2d("qnormal",a=1,b=5,>filled,>add,color=green,style="/"):
Using filled polygons, we can also mark the region between two functions.
>function f(x) := cos(x); >function g(x) := sin(x);
Now we plot f and g, and apply f and g for values between the intersections. The vector tf goes from left to right from pi/4 to 5pi/4, and tg from right to left. We append the two vectos with the concatenation operator | for the plot of the shaded area.
>plot2d("g(x)",0,2pi); plot2d("f(x)",>add); ... tf=linspace(pi/4,5pi/4,100); tg=flipx(tf); ... plot2d(tf|tg,f(tf)|g(tg),>filled,style="|",>add):
It is possible to subdivide the plot window into several small windows. The figure function sets a global variable, which contains the positions of the subwindows. Then figure(n) selects one of the windows. figure(0) resets to the full window.
>figure(2,2); clg; ... figure(1); plot2d("sin(x)",xmin=0,xmax=2*pi,margin=0); ... figure(2); plot2d("sin(2*x)",xmin=0,xmax=2*pi,margin=0); ... figure(3); plot2d("sin(3*x)",xmin=0,xmax=2*pi,margin=0); ... figure(4); plot2d("sin(5*x)",xmin=0,xmax=2*pi,margin=0); ... insimg(); figure(0);
If you ever get into troubles with graphics, you might try the reset command. Of course, it does also help to restart Euler.
>reset;