Graphical cgi-script "Graph"

Cgi-script "Graph" provides very simple way to draw the graphs online without installing any software. Just put the formula or points into the address string of the browser after the question mark of our script and press "Enter". As a result the graph will appear in the browser window as in the sample below.

Result of the work:

http://physics-animations.com/cgi-bin/gra.pl?sin(x)

All additional parameters are separated by comma-point (;). If you need to put the graph into the web-page you can do it as for an ordinary image:

<img src="http://physics-animations.com/cgi-bin/gra.pl?sin(x)">

You can also save this graph to computer as PNG-image with transparent background and insert it into documents. It you enter into the address string not the formula of function, but the mathematical expression as, for example, http://physics-animations.com/cgi-bin/gra.pl?exp(3)*sin(5)+ln(4), then instead of graph you will receive the result of calculations: -17.8742145641675.

There are many different parameters, which allows to calculate the roots of equation, construct the animation, draw the best approximation lines, work with 3D coordinates, trigonometric and complex values. All these possibilities are explained below. If it will be not convenient to enter the formula, data and parameters directly into the address string of the browser, then you can use a special form http://physics-animations.com/cgi-bin/graph.pl Type every parameter (function, point, etc.) on the new string without comma-point (;).

 Contents: Samples:

 

Basic functions

Function Description Sample
+  Addition  
-  Subtraction  
/  Division  
*  Multiplication  
**  Raising to power (or ^)  3**2=9,   3^2 = 9
abs(x)  Returns the absolute value of x  abs(-5) = 5
cos(x)  The cosine of x, where x is in radians.  cos(pi) = -1
sin(x)  The sine of x, where x is in radians.  sin(pi/2) = 1
atan2(x,y)  Calculates and returns the arctangent in radians
 of one value x divided by y, in the range -pi to pi.
 atan2(1,1) = 0.785398163397448
exp(x)  Returns a value of the exponent (e) to the power
 of the passed value x.
 exp(1) = 2.71828182845905
log(x)  Compute the natural log of the value.  log(2.71828182845905) = 1
sqrt(x)  Returns the square root of the value x.  sqrt(9) = 3, sqrt(-1) = i
rand(x)  Return a random real number between 0 and x. If no value is passed, 1 is assumed and a value between 0 and 1 is returned.  rand(10) =  6.18115656349016
hex(x)  Interprets a string expression x as an hexadecimal number
 and computes its decimal equivalent.
 hex(FF) = 255
oct(x)  Interprets a string expression x as an octal number
 and computes its decimal equivalent.
 oct(11) = 9
int(x)  Convert the value to an integer and truncate it if necessary.  int(3.1415926535) = 3
time  Returns the number of seconds since January 1, 1970.  
 

Additional functions

Additional functions Explanations
    tan         tan(z) = sin(z) / cos(z)
    csc, cosec, sec, sec, cot, cotan         csc(z) = 1 / sin(z)
        sec(z) = 1 / cos(z)
        cot(z) = 1 / tan(z)
    asin, acos, atan         asin(z) = -i * log(i*z + sqrt(1-z*z))
        acos(z) = -i * log(z + i*sqrt(1-z*z))
        atan(z) = i/2 * log((i+z) / (i-z))

         asin(1) = 1.5707963267949

    atan2(z1,z2)         atan2(z1,z2) = atan(z1/z2)
    acsc, acosec, asec, acot, acotan         acsc(z) = asin(1 / z)
        asec(z) = acos(1 / z)
        acot(z) = atan(1 / z)
    sinh, cosh, tanh         sinh(z) = 1/2 (exp(z) - exp(-z))
        cosh(z) = 1/2 (exp(z) + exp(-z))
        tanh(z) = sinh(z) / cosh(z)
    csch, cosech, sech, coth, cotanh         csch(z) = 1 / sinh(z)
        sech(z) = 1 / cosh(z)
        coth(z) = 1 / tanh(z)
    asinh, acosh, atanh         asinh(z) = log(z + sqrt(z*z+1))
        acosh(z) = log(z + sqrt(z*z-1))
        atanh(z) = 1/2 * log((1+z) / (1-z))
    acsch, acosech, asech, acoth, acotanh         acsch(z) = asinh(1 / z)
        asech(z) = acosh(1 / z)
        acoth(z) = atanh(1 / z) = 1/2 * log((1+z) / (z-1))
    pi         3.14159265358979
    radians=deg2rad(degrees)         deg2rad(90) = 1.5707963267949
    degrees=rad2deg(radians)          rad2deg(pi) = 180
    cbrt, log10, logn(z, n)         cbrt(z) = z ** (1/3)
        log10(z) = log(z) / log(10)
        logn(z, n) = log(z) / log(n)
    ln         ln(exp(1)) = 1
    Re(z), Im(z)         Complex and real part of a value z
    abs(z), arg(z)         Polar coordinates z (r,j)
 

Сomplex numbers and associated mathematical functions

This package lets you create and manipulate complex numbers. Complex numbers can be written in cartesian form as a+bi or in polar form as r·exp(it). A complex number z can be transformed to cartesian form by operator z->{d} and to polar form by operator z->{p}. Absolute value of the complex number z can be found as abs(z), angle in radians is arg(z). Re(z), Im(z) are consequently the real and the complex parts of z. You can enter the complex number simply as 3+4i (or cplx(a,b)). In polar coordinates enter z=cplxe(r,t), where r is radius and t is angle. Result of calculations in polar coordinated are displayed as [r,t]. And finally if zn = [r,t], then n-th root of [r,t] can be calculated ambiguously. For example the cube root of [8,pi] could be [2,pi/3], or [2,pi], or [2,5pi/3]. The k-th solution of n-th root for z = [r,t] is given by (root(z,n))[k]. In the opposite cbrt(z) can find only one solution of the cube root.

You can add, subtract, divide complex numbers act to them by different functions. Some examples are given below.

If z1 = a + bi = r1 * exp(i * t1) и z2 = c + di = r2 * exp(i * t2)
Then z1 + z2 = (a + c) + i(b + d)
z1 - z2 = (a - c) + i(b - d)
z1 * z2 = (r1 * r2) * exp(i * (t1 + t2))
z1 / z2 = (r1 / r2) * exp(i * (t1 - t2))
z1 ** z2 = exp(z2 * log z1)
abs(z) = r1 = sqrt(a*a + b*b) sqrt(z) = sqrt(r1) * exp(i * t/2)
exp(z) = exp(a) * exp(i * b)
log(z) = log(r1) + i*t
sin(z) = 1/2i (exp(i * z1) - exp(-i * z))
cos(z) = 1/2 (exp(i * z1) + exp(-i * z))

Function Explanation
     cplxe(r,t) or [r,t] Input of a complex number in polar coordinates
     cplx(a,b) or a+b*i or (a,b) Input of a complex number in cartesian coordinates
     Re(z), Im(z), abs(z), arg(z) Real, coplex part of z as well as an absolute value of z and its angle in polar coordinates respectively.
     z->{d},   z->{p} Transfer of a complex number z in cartesian and polar coordinates respectively.
     cbrt(z) Cube root of a complex number z.
     (root(z,n))[k] The k-th solution of n-th root for z:
(root(z, n))[k] = r**(1/n) * exp(i * (t + 2*k*pi)/n)
Example Result
a=i
a->{p}
a
[1,pi/2]
a=cplxe(1,pi/2)
a->{d}
a
6.12303176911189e-17 + i
(1+i)(2+i)/(3+i)(4+i) 1.6+3.8i
sqrt(-1) i
abs(3+4i) 5
Re(3+4i) 3
Im(3+4i) 4
arg(1+i) pi/4
a=exp(i*pi/2)
a->{d}
a
6.12303176911189e-17 + i
a=cplxe(1, 2*pi/3)
b=a**3
1 - 2.44921270764475e-16i
 

 Graphs of functions.

Expressions of functions, constants and additional functions are written in the separate lines or separated by symbol comma-point (;) You can write for example gр, g(t)=sin(t) and then function will be g, and argument is t. Comments are separated by symbol %%. By default f is the function and x is the argument (variable). Both function and its argument can be real or complex. If function is a complex value, then vertical axis shows complex pert of function and horizontal axis shows its real value. In this case complex number fmin define left bottom point of the graph and complex fmax define right top point. Argument is changed linearly from xmin to xmax. Both xmin and xmax can be complex too. If  fmin or fmax are not defined, then they are calculated automatically. There are 2 regimes of scaling of complex function: ratio=0 or 1. If ratio=0, then the graph of function is stretched vertically and horizontally to fmin, fmax. If ratio=1, then scale of both axis are the same and  fmin and fmax are chosen automatically to show all the function. If you have two function of one parameter and you wish to draw these functions in axes XY, then output  them as f1(t)+i*f2(t). 

Parameters.

Parameter Default
value
Explanation
xmin xmin=0 Minimal value of argument. In case of complex function xmin may be complex too.
xmax xmax=2*pi Maximal value of argument. In case of complex function xmax may be complex too.
fmin - Minimal value of the function. If fmin is absent, then it is calculated automatically. If function is a complex value, then fmin must be complex too. In this case fmin defines the coordinate of the left bottom corner of the graph.
fmax - Maximal value of the function. If fmax is absent, then it is calculated automatically. If function is a complex value, then fmax must be complex too. In this case fmax defines the coordinate of the right top corner of the graph.
points points=100 Number of the points are used to draw the graph. The greater the points number, the smother the curve, but the bigger time requires for calculations. If parameter points is too big, then calculation time can exceed the server limit and error 500 will appear. In such a case diminish the points number.
fbox fbox=1 Parameter fbox may be 0 or 1. If fbox=1, then the frame box, grid and captions are drawn; if fbox=0, then not.
spline spline=0 Parameter fbox may be 0 or 1. If spline=0, then the graph is drawn from point to point by lines. If spline=1, then the graph is drawn by parabolas/splines. In some cases splines can be used to make the curve more smooth, while this increase the time of calculations and may distort the curve (for rapidly varying function).
ratio ratio=0 If the graph of complex function is drawn, then the curve is automatically stretched to the border of the rectangle (ratio=0). In some cases this can be not convenient because distort the proportions between real and complex value of function. For example, if we need to draw the graph of function exp(i*x), then we expect to see a circle, but not an ellipse.  In such a case please use spline=1. Scale in both axes will be identical and graph will be stretched automatically in one direction taking into account proportions of graph and rectangle sizes.
nmax nmax=1 If we need to draw several functions which differ by a parameter, then nmax sets the maximal number of functions, while the variable n will be changed from 0 to nmax-1.
n - If nmax is set, then n varies from 0 to nmax-1.
roots roots=1 Parameter roots may be set to 0 or 1. If roots=0, then the roots of equation f(x)=0 are not calculated. If roots=1, then the roots of equation are calculated. By defolt the roots are calculated. If there are several functions, then the roots are calculated for the main function (without indexesЕсли имеется несколько функций, то корни вычисляются у главной (без цифровых индексов).
width width=300 The width of graph in pixels.
height height=200 The height of graph in pixels.
animated(time,transparent) animated(0,0) Add operator "animated" if you need to construct GIF-animation. Every frame differs by value of n. For this reason for animation you can set the total number of frames nmax. Parameter time sets the time delay between the frames of animation in milliseconds. time=0 sets the maximal possible frame-per-second speed. transparent=1 sets transparent background. In this case curves will overlap each other.
Curve by points:
begin - Begin of points array.
end - End of points array.
polygon - Drawing of the filled area by points. Next parameter after begin.
polyline On Drawing of the broken line by points. Next parameter after begin. By default.
stat - Drawing an approximation line y=a+bx by points (linear regression). Next parameter after begin.
points On If operator "points" follows operator "begin", then besides of broken line or filled area the points will be displayed in the form of arrows.
ymin Calculated Minimal value by Y
ymax Calculated Maximal value by Y
inv - If there is operator "inv" in any place of code, then points with z<0 will not be displayed.

Sample 1:

xmin=0 %% Minimal value of argument (by default xmin=0)
xmax=5 %% Maximal value of argument (by default xmax=2*pi)
fmin=-2 %% Minimal value of function (if empty, then calculated)
fmax=10 %% Maximal value of function (if empty, then calculated)
points=50 %% Number of points at the curve (by default - 100)
width=400 %% Width of the graph in pixels (by default - 300)
height=200 %% Height of the graph in pixels (by default - 200)
fbox=1 %% Presence of frame box (by default - 1, i.e. yes)
a=x^4 %% Additional function
b=10*x^3 %% Additional function
c=35*x^2 %% Additional function
d=50*x %% Additional function
e=24 %% Additional functionя
a-b+c-d+e %% Main function

f=a-b+c-d+e; xmin=0, xmax=5
fmin=-2, fmax=10

Корни: 1; 2; 3; 4

Sample 2:

f=sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(sin(x)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
; xmin=0, xmax=6.28318530717959
fmin=-0.214933498715024, fmax=0.214933498715024

Корни: 0; 3.14159265358979; 6.28318530717959

Sample 3:

fbox=0; width=700; points=500; roots=0
sin(6*sin(5*x))

f=sin(6*sin(5*x))
; xmin=0, xmax=6.28318530717959, fmin=-0.99690813246534, fmax=0.996908132465327

Sample 4: Attenuated harmonic oscillation.

xmax=4*pi; ratio=1
exp(-0.5*x)*exp(i*x)


f=exp(-0.5*x)*exp(i*x)

xmin=0, xmax=12.5663706143592
fmin=(-0.234424144407471 + i*-0.207821413368276)
fmax=(1 + i*0.615128016236704)

Functions of the complex value.

Program allows also to draw the complex functions of a complex value. In the next sample value x changes from i to 1. Let us draw the function x^n for several different n. Result is shown below.

f=x; xmin=i, xmax=1
fmin=(-1 + i*-1), fmax=(1 + i*1)
f=x**2; xmin=i, xmax=1
fmin=(-1 + i*-1), fmax=(1 + i*1)
f=x**3; xmin=i, xmax=1
fmin=(-1 + i*-1), fmax=(1 + i*1)
f=x**4; xmin=i, xmax=1
fmin=(-1 + i*-1), fmax=(1 + i*1)
f=x**9; xmin=i, xmax=1
fmin=(-1 + i*-1), fmax=(1 + i*1)
x;
xmin=i; xmax=1; fmin=-1-i; fmax=1+i;
roots=0; points=2; width=200

Verification for f=x^2: x=t+i*(1-t), where t=0..1
f=x^2=(2t-1)+i*2t(1-t)
Re(f)=2t-1; Im(f)=2t(1-t)
We can see that Im(f)=0.5(1-Re(f)^2) That is if we draw this dependence in coordinates XY, then it will be y=0.5(1-x^2), x=-1..1. (see in the right side). We can see that this is the same as for the complex form of the function.

f=0.5*(1-x**2); xmin=1, xmax=-1
fmin=-1, fmax=1

Several graphs with different parameter n.

 

If nmax is set, then n varies from 0 to nmax-1.


roots=0; nmax=25
a=(n+1)/nmax
xmin=-pi
xmax=pi
f=a*(sin(2*x))*1/(x)+0.3*(cos(2*x))

f=a*(sin(2*x))*1/(x)+0.3*(cos(2*x));
xmin=-3.14159265358979, xmax=3.14159265358979
fmin=-0.574449044754396, fmax=2.3


Animation.

For animation of the graphs the function animated(time,transparent) is used. In this case the plotted image is stored as Animated GIF file. Parameter time set the time delay between the frames in milliseconds. Parameter transparent can be -1 or 0. If parameter "transparent" is set to 1, then the transparent background is used and all drawn graphs are seen. If parameter "transparent" is 0, the the background is white and frames consequently substitute each other. Graphs in different frames of animation differ by parameter n which is changed from 0 to nmax-1.

animated(0,0)
nmax=4
xmax=2*pi
roots=0
nmax=20
fi=2*pi*n/nmax
a1=sin(x)
a2=sin(2*x+fi)
XY(t)=cplx(a1,a2)
XY=cplx(a1,a2); xmin=0, xmax=6.28318530717959
fmin=(-1 + i*-1), fmax=(1 + i*1)

The points and the curves drawn with these points can also be transformed in GIF animation with varying parameter n.

In a sample below the best line is drawn by points (least-squares method). Function "rand" adds to coordinates of all the points the random value simulating an error of the  measurement.

begin stat points
nmax=50; animated(10,0)
0 0; 1 1; 2 2; 3 3; 4 4;
5 5; 6 6; 7 7; 8 8; 9 9
x=x+rand(1)
y=y+rand(1)
end

xmin=0.018121094411935, xmax=9.96057783549489
fmin=0.00498491452561467, fmax=9.94779613951097

Transformation of 3D-coordinates.

There are following function for transformation and conversion of 3D-coordinates:

($rho, $theta, $z) = cartesian_to_cylindrical($x, $y, $z);
($rho, $theta, $phi) = cartesian_to_spherical($x, $y, $z);
($x, $y, $z) = cylindrical_to_cartesian($rho, $theta, $z);
($rho_s, $theta, $phi) = cylindrical_to_spherical($rho_c, $theta, $z);
($x, $y, $z) = spherical_to_cartesian($rho, $theta, $phi);
($rho_c, $theta, $z) = spherical_to_cylindrical($rho_s, $theta, $phi);

 

In the next sample we used conversion from Spherical coordinates to Cartesian one by means of the function "spherical_to_cartesian".

animated(10,0)
roots=0; nmax=10; points=50; width=200
R=1
THETA=pi*n/nmax
PHI=x
(X,Y,Z)=spherical_to_cartesian(R,THETA,PHI)
f=X+Z*i

f=X+Z*i; xmin=0, xmax=6.28318530717959
fmin=(-0.998026728428272 + i*-1),
fmax=(0.998026728428272 + i*1)

Rotation in Cartesian coordinates.

nmax=50; animated(10,0)
width=200
xmin=-2; xmax=2
ymin=-2; ymax=2
begin
 1  1  1
 1 -1  1
-1 -1  1
-1  1  1
 1  1  1
 1 -1  1
 1 -1 -1
 1  1 -1
 1  1  1
 1  1 -1
 1 -1 -1
-1 -1 -1
-1  1 -1
 1  1 -1
-1  1 -1
-1 -1 -1
-1 -1  1
-1  1  1
-1  1 -1
end
(x,y,z)=rotate(x,y,z,pi/10,pi/10)
(x,y,z)=y_rotate(x,y,z,2*n*pi/50)
Functions of rotation in Cartesian coordinates:

    (x,y,z)=rotate(x,y,z,φ,θ)
    (x,y,z)=x_rotate(x,y,z,φ)
    (x,y,z)=y_rotate(x,y,z,φ)
    (x,y,z)=z_rotate(x,y,z,φ)

 

xmin=-2, xmax=2, fmin=-2, fmax=2

Different functions in one field.

The main function "f" must be without the number. Other functions must begin from the same letter "f" followed by the arbitrary integer numbers. If the main function is redefined as, for example, g(t)=..., then other functions must be g1=.., g2=.., g101=.. etc. In this case all additional functions will also have t as an argument. Roots are calculated only for the main function. Color of every curve is set as color0=.., color1=... followed by RGB components of color in hexadecimal form (as in HTML tegs). color0 defines the color of the main function. If the color is omitted, then the default value is darck-blue.

points=50; width=200; roots=0
f=exp(i*x); color0=0000FF
f1=0.3*exp(i*x); color1=FF0000
f2=sin(2*x)*exp(i*x); color2=00AA00

f=exp(i*x); xmin=0, xmax=6.28318530717959
fmin=(-1 + i*-0.998 ), fmax=( 1 + i*0.998 )

Drawing of the curves and the figures by points

Curves and filled geometrical figures can be drawn by points. Array of points is given by pairs of digits "x y" in a column. This array begins by heading begin and  finished by operator end. You can draw several curves in one field. Filled figure is called polygon. Colors: black, white, red, green, blue и darkblue (by default). xmin, xmax, fmin and fmax define the external frame. In case of the curves - polyline - you can use smoothing tool: spline=1. If  parameter spline is set to 1, then the curve is drawn by parabolas instead of the lines (see sample below).
 
In a sample below 3 figures are drawn: two filled areas (polygon) and one not filled curve (polyline).
begin polygon blue
0 23
 5 30
 12 32
 16 31
 20 28
 22 25
 23 20
 21 11
 16 1
 0 -17
 -16 1
 -21 11
 -23 20
 -22 25
 -20 28
 -16 31
 -12 32
 -5 30
 0 23
 5 30
 end
begin polygon red
 24 8
 29 15
 36 17
 40 16
 44 13
 47 5
 40 -14
 24 -32
 8 -14
 3 -4
 1 5
 4 13
 8 16
 13 17
 19 15
 24 8
 29 15
 end
begin
 -32 -6
 -29 -2
 -25 0
 -19 -3
 -17 -8
 -19 -14
 -22 -20
 -32 -32
 -43 -20
 -46 -14
 -47 -8
 -45 -3
 -40 0
 -35 -2
 -32 -6
 -29 -2
 end
xmin=-47, xmax=47, fmin=-32, fmax=32

In the next sample the curve is drawn by 3 points using parabolas. This takes more processor time, but makes an inquiry string very compact and smoothes the curve well in the case of insufficiency of the points.

begin
spline=1
0 5
5 0
0 -5

xmin=0, xmax=5, fmin=-5, fmax=5

Coordinates may be transferred using the rules as follows:
x = f(x); y= g(y); z=h(z)
Например: x=x**2; y=log10(y); z=2*z;

Drawing a line y=a+bx by least-squares method (regression analysis).

begin stat points
-9.1 -4.7
-8.5 -3.1
-7.3 -2.1
-6.1 -1.7
-5.9 -0.1
-4.1 0.2
-3.7 1.5
-2.7 2.4
-2.1 4.7
-0.8 4.7
0.4 5.1
1.8 6.4
2.2 7.7
3.7 8.2
4.9 9.6
5.3 10.1
7.0 11.9
7.8 12.3
8.7 13.0
9.9 14.4
end
Drawing a line y = a + bx by least-squares method (regression analysis).

xmin=-9.1, xmax=9.9, fmin=-4.7, fmax=14.4
a = 4.96 ± 0.13
b = 0.97 ± 0.02
 
Dx=<x2>-<x>2; Dy=<y2>-<y>2
b=(<xy>-<x><y>)/Dx; a=<y>-b<x>
σb2=(Dy/Dx-b2)/N; σa2= σb2Dx
Outliers (deviation exceeds 3σ): (-2.1,4.7)

Functions of two arguments

The sense of the color scheme is as follows. We draw the graph of function f(x,y). Red is the area (x,y) where f>0 and blue is the area where f<0. Color is more saturated near zero. The border between red and blue shows the solution of the equation f(x,y)=0. Other regions are filled in green color (which can be removed by bitmap=3). There are other color schemes which can be set by parameter bitmap=1...9

xmin=-8; xmax=8
ymin=-5; ymax=5
height=200; width=320
a=6; b=8
f(x,y)=(x^2+y^2)^2-2*(b^2)*(a^2-y^2)-a^4+b^4

 

Kassiny oval.


 



Rambler's Top100