Grafika v Mathematici

Andrej Bauer

Risanje grafov

Grafi funkcij ene spremenljivke

Plot[Sin[x],{x,-5,5}]

Plot[{Sin[x], -Sin[x],Sin[x]*Cos[16 x]},{x,-5,5},PlotStyle->{RGBColor[1,0,0],RGBColor[0,1,0],{Thin,RGBColor[0.5,0.5,0.5]}},
AspectRatio->Automatic,
PlotRange->{{-5,5},{0,3}}]

{RGBColor[1,0,0],RGBColor[0,1,0],RGBColor[0.5,0.5,0.5]}

RGBColor[1,1,0]

Table[f[k],{k,3,8}]

Table[(x^k),{k,0,10}]

Plot[Table[(x^k),{k,0,10}],{x,-0.1,1.1},PlotStyle->Thin,AxesOrigin->{0,0}]

Grafi funkcij dveh spremenljivk

f[x_,y_]:=Sin[(x^2)+2(((y-x))^2)]+Cos[3 x+4y]

Plot3D[f[x,y],{x,-2,2},{y,-2,2}]

Plot3D[2*x-3*y+2,{x,-5,5},{y,-5,5}]

Plot3D[(x^2)-(y^2),{x,-2,2},{y,-2,2},Mesh->False,PlotPoints->5]

DensityPlot[f[x,y],{x,-2,2},{y,-2,2}]

DensityPlot[f[x,y],{x,-2,2},{y,-2,2},ColorFunction->"SunsetColors",PlotLegends->Automatic]

ContourPlot[f[x,y],{x,-2,2},{y,-2,2}]

ContourPlot[f[x,y]==0,{x,-2,2},{y,-2,2},PlotPoints->50]

ContourPlot[((((x^2)+(y^2)-1))^3)==4*(x^2)*(y^3),{x,-2,2},{y,-2,2},PlotPoints->50]

VectorPlot[{1,0},{x,-2,2},{y,-2,2}]

VectorPlot[{0,1},{x,-2,2},{y,-2,2}]

VectorPlot[{x,0},{x,-5,5},{y,-2,2},AspectRatio->Automatic]

VectorPlot[{x,y},{x,-2,2},{y,-2,2}]

VectorPlot[{-y,x},{x,-2,2},{y,-2,2}]

f[x,y]

Grad[f[x,y],{x,y}]

VectorPlot[Evaluate[Grad[f[x,y],{x,y}]],{x,-1,1},{y,-1,1}]

pic=Plot[Sin[1/x]*(x^2),{x,-0.1,0.1}]

Export["~/Desktop/pic.pdf",pic]

Export["~/Desktop/pic.png",pic]

Export["~/Desktop/pic-large.png",pic,ImageResolution->500]

Graphics in Graphics3D

V Mathematici lahko predstavimo 2D in 3D grafične objekte z ukazoma Graphics in Graphics3D. Grafične objekte naštejemo v seznamu.

Graphics[{Circle[{1,1},2], Point[{1,0}],Line[{{0,0}, {3,4}, {4,3}}]},Axes->True]

Graphics3D[{Cylinder[{{0,0,0},{1,2,1}},1/3],Red, Sphere[{0,0,3},1/2], Polygon[{{0,0,1},{1,0,0},{1,1,1}}]}]

Poleg objektov lahko določamo tudi barve in ostale lastnosti. Objekti so lahko hierarhično gnezdeni v sezname in podsezname.

Graphics[{{Red,Polygon[{{-5,5},{5,5},{0,7}}]}, {Black,Thickness[0.01], Line[{{-4,0},{4,0},{4,5},{-4,5}, {-4,0}}]}}]

Grafične elemente lahko tudi transformiramo:

hisa ={
{Red,Polygon[{{-5,5},{5,5},{0,7}}]}, {Black,Thickness[0.01], Line[{{-4,0},{4,0},{4,5},{-4,5}, {-4,0}}]}
}

Graphics[hisa]

Graphics[Rotate[hisa,π/10],Axes->True]

Graphics[{hisa,Rotate[Scale[hisa,0.2],π/6]}]

Graphics[Table[Rotate[Scale[Translate[hisa,{k/3,0}],(0.7^k)],k*π/6],{k,1,10}]]

Manipulate

Manipulate[Plot[Sin[a*x+3],{x,-5,5},AspectRatio->Automatic],{a,-4,20}]

Manipulate[Plot[Sin[a*x+b],{x,-5,5},AspectRatio->Automatic],{a,-4,20},{b,0,5}]

Z ukazom Manipulate lahko prikažemo grafični objekt, ki je odvisen od parametrov.

ClearAll[tangenta]
tangenta::usage="tangenta[f[x],x,(Subscript[x, 0])] je enačba tangente na f[x] v točki x = (Subscript[x, 0])";
tangenta[f_,x0_,x_]:=Function[t,f'[x0]*(t-x0)+f[x0]][x]

tangenta[Sin,0,x]

ClearAll[f]
f[t_]:=Cos[t]*t

Plot[{f[x],tangenta[f,0.3,x]},{x,-5,5}]

Manipulate[
Plot[{f[x],tangenta[f,x0,x]},{x,-5,5},PlotRange->{{-5,5},{-5,5}},AspectRatio->Automatic],
{x0,-5,5}]

Animate

Animacije delamo z ukazom Animate, ki sicer deluje podobno kot Manipulate, le da samodejno spreminja vrednost parametra.

ClearAll[f]
f[t_]:=Cos[t]*t
Animate[Plot[{f[x],tangenta[f,x0,x]},{x,-5,5},PlotRange->{{-5,5},{-5,5}},AspectRatio->Automatic],{x0,-5,5}]

Animacijo lahko tudi shranimo:

Table[Hue[r],{r,0,1,0.05}]

anime = Animate[Graphics[{Hue[r],Disk[{0,0},1/2+Sin[π*5*r]/3]},PlotRange->{{-1,1},{-1,1}}],{r,0,1}]

Export["~/Desktop/anime.gif",anime]

Animacija vsebuje tudi rob in kontrolne elemente. Če se ji želimo izogniti, lahko shranimo seznam slik.

cartoon = Table[
Graphics[{Hue[r],Disk[{0,0},1/2+Sin[π*5*r]/3]},PlotRange->{{-1,1},{-1,1}}],{r,0,1,0.01}];

Export["~/Desktop/cartoon.gif",cartoon]

Kamera

Mathematica dostopa tudi do kamere in ima bogat nabor funkcij za obdelavo slik.

CurrentImage[]

a=5

Dynamic[(a^2)]

a=6

Z ukazom Dynamic lahko pridobivamo sliko v realnem času:

Dynamic[CurrentImage[]]

HighlightImage[CurrentImage[],FindFaces]

HighlightImage[,FindFaces]

Dynamic[HighlightImage[CurrentImage[],FindFaces]]