source: http://calculuswithjulia.github.io/precalc/plotting.html

The Graph of a Function

Graphing a function with Julia

using CalculusWithJulia
gr()
## Plots.GRBackend()
f(x)=1-x^2/2
## f (generic function with 1 method)
plot(f,-3,3)

plot(sin, 0, 2pi)

f(x) = 1/(1+x^2)
## f (generic function with 1 method)
plot(f,-3,3)

f(x)=exp(-x^2/2)
## f (generic function with 1 method)
plot(f,-2,2)

plot(x->cos(x)-x, 0, pi/2)

f(x) = tan(x)
## f (generic function with 1 method)
plot(f, -10, 10)

The details of graph making

f(x) = sin(x)
## f (generic function with 1 method)
xs = range(0, 2pi, length=10)
## 0.0:0.6981317007977318:6.283185307179586
ys = f.(xs)
## 10-element Array{Float64,1}:
##   0.0                   
##   0.6427876096865393    
##   0.984807753012208     
##   0.8660254037844387    
##   0.3420201433256689    
##  -0.34202014332566866   
##  -0.8660254037844385    
##  -0.9848077530122081    
##  -0.6427876096865396    
##  -2.4492935982947064e-16

plot(xs, ys)

NaN values

f(x)=1/x
## f (generic function with 1 method)
xs=range(-1,1,length=251)
## -1.0:0.008:1.0
ys=f.(xs)
## 251-element Array{Float64,1}:
##  -1.0               
##  -1.0080645161290323
##  -1.016260162601626 
##  -1.0245901639344261
##  -1.0330578512396695
##  -1.0416666666666667
##  -1.050420168067227 
##  -1.0593220338983051
##  -1.0683760683760684
##  -1.0775862068965516
##   â‹®                 
##   1.0683760683760684
##   1.0593220338983051
##   1.050420168067227 
##   1.0416666666666667
##   1.0330578512396695
##   1.0245901639344261
##   1.016260162601626 
##   1.0080645161290323
##   1.0
ys[xs .== 0.0] .= NaN
## 1-element view(::Array{Float64,1}, [126]) with eltype Float64:
##  NaN
plot(xs,ys)

g(x)=abs(x)<.05 ? NaN : f(x)
## g (generic function with 1 method)
plot(g, -1, 1)

Reflections

xs = range(-pi, pi, length=100)
## -3.141592653589793:0.06346651825433926:3.141592653589793
ys=sin.(xs)
## 100-element Array{Float64,1}:
##  -1.2246467991473532e-16
##  -0.06342391965656484   
##  -0.12659245357374938   
##  -0.1892512443604105    
##  -0.2511479871810793    
##  -0.31203344569848734   
##  -0.3716624556603276    
##  -0.4297949120891718    
##  -0.4861967361004687    
##  -0.5406408174555978    
##   â‹®                     
##   0.4861967361004687    
##   0.4297949120891718    
##   0.3716624556603276    
##   0.31203344569848734   
##   0.2511479871810793    
##   0.1892512443604105    
##   0.12659245357374938   
##   0.06342391965656484   
##   1.2246467991473532e-16
plot(xs,ys)

plot(-xs, ys)

plot(xs, -ys)

plot(-xs, -ys)

An even function is one where reflection through the \[y\] axis leaves the graph unchanged. That is, \(f(-x)=f(x)\). An odd function is one where a reflection through the origin leaves the graph unchaged, or symbolically \(f(-x)=-f(x)\).

plot(ys, xs) # reflect through the line y=x

xs=range(-pi/2, pi/2, length=100)
## -1.5707963267948966:0.03173325912716963:1.5707963267948966
ys=[sin(x) for x in xs]
## 100-element Array{Float64,1}:
##  -1.0               
##  -0.9994965423831851
##  -0.9979866764718844
##  -0.9954719225730846
##  -0.9919548128307953
##  -0.9874388886763943
##  -0.9819286972627067
##  -0.975429786885407 
##  -0.9679487013963562
##  -0.9594929736144974
##   â‹®                 
##   0.9679487013963562
##   0.975429786885407 
##   0.9819286972627067
##   0.9874388886763943
##   0.9919548128307953
##   0.9954719225730846
##   0.9979866764718844
##   0.9994965423831851
##   1.0
plot(ys, xs)

Layers

f(x) = 1-x^2/2
## f (generic function with 1 method)
plot([cos,f], -pi/2, pi/2)

f(x)=x^5-x+1
## f (generic function with 1 method)
plot([f,zero],-1.5,1.4)

Adding layers explicitly

plot(f, -1.5, 1.4)

plot!(zero)

f(x)=x*(x-1)
## f (generic function with 1 method)
plot(f,-1,2)

scatter!([0,1],[0,0])

Parametric graphs

f(x)=cos(x);g(x)=sin(x)
## g (generic function with 1 method)
xs=range(0,2pi,length=100)
## 0.0:0.06346651825433926:6.283185307179586
plot(f.(xs),g.(xs))

plot(f,g,0,2pi)

g(x)=x^2
## g (generic function with 1 method)
f(x)=x^3
## f (generic function with 1 method)
plot([g,f],0,25)

xs = range(0,5,length=100)
## 0.0:0.050505050505050504:5.0
plot(g,f,0,25)

g(x)=x-sin(x)
## g (generic function with 1 method)
f(x)=x^3
## f (generic function with 1 method)
plot(g,f,-pi/2,pi/2)

R, r, rho = 1, 1/4, 1/4
## (1, 0.25, 0.25)
g(t) = (R-r) * cos(t) + rho * cos((R-r)/r * t)
## g (generic function with 1 method)
f(t) = (R-r) * sin(t) - rho * sin((R-r)/r * t)
## f (generic function with 1 method)

plot(g, f, 0, max((R-r)/r, r/(R-r))*2pi)