Course 3 : Functions  - definition and plots

Clear["Global`*"]

Standard Mathematical functions in Mathematica

Basic functions

x=Sin[w]
y=Cos[w]
k=ArcTan[1/2] 
z=Sqrt[x^2+y^2] 
v=Exp[I]

k=ArcSin[1/2] 
l=Sin[30 Degree]
z=Sqrt[x^2+y^2]  //Simplify
v=Exp[I]  //ExpToTrig

Special Functions

Gamma[w] 
Gamma[1.5]
EllipticK[2.1]

User definition functions

Definition

Clear[x,y,k]

y[x_]=x^2-x

a=y[2]
b=y[k]
c=y[k]^2-y[3] /.{k->3/2}

Delayed definition

k=2;
f[x_]=1+k x^2 ;    (* without suspension *)
g[x_]:=1+k x^2   (* with suspension *)

f[x]
g[x]

k=5;
f[x]
g[x]

Multivariable functions

h[x_,y_]=x^2+y^2
h[1,1]

Alternative definition 1

Remove[x,f,a]
f=Function[x,5 x^2 +1]
f[a]
f[2]

Attributes[f]={Listable,Protected}

x=5  (* δεν επειρεάζει τον ορισμό της συνάρτησης *) 
f[2]

Alternative definition 2

f=5 #^2+1&
g=#1+5 Exp[#2]&
f[2]
g[1,0]

Plotting

function assigned to a symbol

y=Exp[- t/4] Sin[t]

Plot[y,{t,0,30}]

Plot[y,{t,0,30}, PlotRange->{-1,1}]    (* All *)

Plot[y,{t,0,30}, PlotRange->{{10,20},{-0.1,0.1}}]

function defined as a Mathematica function

Clear[z]
z[t_]=Exp[t]^t

Plot[z[t],{t,0,10}]

Styles

Plot[y,{t,0,30}, PlotRange->All, PlotStyle->{RGBColor[0.9,0.5,0]}, PlotLabel->"My Plot"]
Plot[y,{t,0,20}, PlotStyle->Red,PlotRange->All, Frame->True, FrameLabel->"My Plot"]
Plot[y,{t,0,20}, PlotStyle->Thickness[0.02],AxesLabel->{"x","y"}]
Plot[y,{t,0,20}, PlotStyle->{Blue, Dashed, Thin}, PlotRange->All]

Plotting many functions

A method

y1=Exp[- t/4] Sin[t];
y2=Exp[- t/3] Sin[t];
y3=Exp[- t/2] Sin[t];
Plot[{y1,y2,y3},{t,0,20}]

Plot[{y1,y2,y3},{t,0,20},PlotStyle->{Red,Green,Blue}]

Β method

kampili1=Plot[y1,{t,0,20}, PlotStyle->Red];
kampili2=Plot[y2,{t,0,20}, PlotStyle->Green, PlotRange->All];
kampili3=Plot[y3,{t,0,20}, PlotStyle->Blue];
Show[{kampili1,kampili2,kampili3}]