Mathematica in 3×60 minutes - Part III

Gergely Gyebrószki | Dept. of Applied Mechanics, Budapest University of Technology and Economics

Plotting basic functions

f1[x_]:=(x^3)-x
f2[x_,y_]:=(x^2)+Sin[6y]+2x y

Most of the plotting functions have the same structure
Plot[expression, {variable, min, max}, option -> value1, option2 -> value2, ...]

Plot[f1[x],{x,-1,1}]

Plot[{f1[x],2f1[x]},{x,-1,1},PlotLegends->{"a","b"}]

Generating several plots

f/@{a,b,c}

Plot[#,{x,-1,1}]&/@{f1[x],((f1[x])^2),2f1[x]}

{graph1,graph2}=Plot[#,{x,-1,1}]&/@{f1[x],((f1[x])^2)};

graph2

?f2

Plot3D[f2[x,y],{x,-1,1},{y,-1,1},AxesLabel->{"x","y","f"}]

All Plotting functions have a plenty of options, every option is a Rule

Plot[{Exp[x],Log[x],x},{x,-3,3},PlotRange->5,PlotStyle->{Red,Green,Dashed},AspectRatio->5/3,AxesLabel->{"x","f(x)"}]

Options[Plot]

Plotting lists

numbers=Array[5+RandomReal[3]&,50]

ListPlot@numbers

ListPlot[numbers,Joined->True,PlotRange->{0,10},AxesLabel->{"t","N"},PlotStyle->{Lighter@Lighter@Red,Thickness[0.005]}]

Plotting parametric functions

x = Sin[u], y = Sin[2 u]

ParametricPlot[{Sin[u],Sin[2u]},{u,0,1.7π}]

ParametricPlot[{r Sin[u],r Cos[u]},{u,0,1.7π},{r,.5,1},Mesh->True]

ParametricPlot3D[{Sin[u],Cos[u],u+v},{u,0,4π},{v,0,1},BoxRatios->{1,1,1.5},Mesh->None,PlotPoints->100]

Plotting relations

Options[RegionPlot]

RegionPlot[(x^2)+(y^2)<(2^2),{x,-3,3},{y,-3,3},FrameLabel->{"x","y"},Mesh->All]

RegionPlot[x+y<1,{x,-3,3},{y,-3,3}]

RegionPlot[(x^2)+(y^2)<(2^2)&&x+y<1,{x,-3,3},{y,-3,3}]

Animating and Manipulating expressions

Manipulate[a,{a,0,1}]

Manipulate[
Plot[Sin[ω t],{t,0,10}],
{ω,1,10}]

ω

Manipulate[
Plot[{Sin[ω t],Sin[ω t+β]},{t,0,10},PlotLegends->{"Sin[ω t]","Sin[ω t+β]"}],
{ω,1,10},{β,0,π}]

Manipulate[
Plot[f[ω t+β],{t,0,10},Axes->ax],
{{ω,5,"Freq"},1,10,Appearance->"Labeled"},
{{β,1,"Phase shift"},0,π,Appearance->"Labeled"},
{f,{Sin,Cos,Tan}},
{ax,{True,False}}]

Animate[
Plot[Sin[ω t],{t,0,10},Filling->Axis],
{{ω,3,"Frequency"},1,10,Appearance->"Labeled"}]

Manipulate[
Plot[1,{x,0,1},PlotLabel->p],
{p,Locator}]

y = m x + b

GetLine[{x1_,y1_},{x2_,y2_}]:=Module[{m,b},
m=(y1-y2)/(x1-x2);
b=y1-m x1;
{M->m,B->b}
]

GetLine[{0,1},{2,2}]

M x+B/.GetLine[{0,1},{2,2}]

Manipulate[
Plot[M x+B/.GetLine[p1,p2],{x,-2,5},PlotRange->{{-2,5},{-2,5}},PlotLabel->GetLine[p1,p2]],
{{p1,{3,3}},Locator},
{{p2,{1,1}},Locator}
]

Dynamic variables

Dynamic[a]

a=0.6

Slider[Dynamic[a]]

Dynamic[Plot[Sin[a t],{t,0,10}]]

Creating Graphics

Graphics[{
Red,Rectangle[{-1,-1},{2,2}]
}]

Graphics[{
Red,Rectangle[{-1,-1},{2,2}],
Blue,Disk[{-1,0},1.5]},
Axes->True,AxesLabel->{"x","y"}]

Live Example

1 DoF damped oscillator

sol=First@DSolve[{x''[t]+(1/2)x'[t]+x[t]==0,x[0]==1,x'[0]==0},x[t],t]

Plot[x[t]/.sol,{t,0,10},PlotRange->All]

graphic[x_]:=Module[{w=0.5,h=0.3},
Graphics[{
White,EdgeForm[Black],Rectangle[{x-w/2,0},{x+w/2,h}],
Blue,Thick,Line[{{0,h/2},{x,h/2}}]
},PlotRange->{{-1,2},{-.2,.7}},Axes->True,AxesOrigin->{-1,0},ImageSize->600]
]
graphic[1]

Manipulate[
Module[{sol,x1},
sol=First@DSolve[{x''[t]+d x'[t]+x[t]==0,x[0]==1,x'[0]==0},x[t],t];
x1=(x[t]/.sol)/.t->t1;
Column[{
Plot[x[t]/.sol,{t,0,30},PlotRange->All,ImageSize->400,Epilog->{Red,PointSize[Large],Point[{t1,x1}]}],
graphic[x1]
}]
],
{t1,0,30,Appearance->"Labeled"},
{{d,0.5},0,1,Appearance->"Labeled"}]