Tutorial II

More Toot

Syntax of   ==   vs.   :=   vs.   =

                              ==
==   is used to indicate an equation or equality.

lhs == rhs   evaluates to either True or False.

Most often, the == is used in commands like Solve,   e.g.,     Solve[ x2 - 5x + 6 == 0,   x ]

                  =   vs.   :=
Both of these symbols are used to assign values, e.g.,

area = π (r^2)
area :=Pi r^2    (Change with Esc p Esc and Ctrl 6 )
Both set the value of symbol ``area’’;
the first evaluates it immediately (using the current value of r);

the second uses delayed evaluation -- waiting until area is actually used before doing a calculation with r.

The difference between the two is illustrated by the sample commands below

ClearAll["Global`*"]
c=2;(*define f and g and stick in a particularc*)
f[x_]=c x^2;
g[x_]:=c x^2;

{f[z],g[z],f[5],g[5]}         (*so far,f and g are the same*)

c=3;(*change c*)
{f[z],g[z],f[5],g[5]}         (*now f and g are different; g waits to form itself until you run it!*)

c = 5; t = 20;(*both c and t are changed;   f yields 3 t^2;   g yields 5 t^2 *)
{f[t],g[t],f[5],g[5]}

Derivatives

ClearAll["Global`*"]
y[x_] = a (x^3) + (1/(b x))
D[y[x],x]

g[x_] := ArcTan[x]
D[g[x],x]
                              (*  ESC dt ESC  *)
(∂_x)g[x]      
(∂_x) (g[x])
g'[x]   (*  single variable  *)

ClearAll["Global`*"]
y[x_] := Exp[-x] ( (x^2) - 1);
D[y[x],x]
  (*  Simplify[%]  *)
 Limit[((y [x+ δx]- y[x] )/(δx)),δx-> 0]

ClearAll["Global`*"]

D[f[x] g[x],x]
D[f[x]/g[x], x]
D[f[g[x]],x]

z = g[x]
g[x_] := Sin[x]
f[z_] := (z^2)
f[g[x]]
f'[g[x]]
g'[x]
f'[g[x]]  g'[x]
D[f[g[x]],x]

Freefall with initial velocity (Subscript[v, o]) and at an initial height (Subscript[s, o])

s[t_] = -(1/2) g (t^2)+(Subscript[v, o]) t + (Subscript[s, 0])

Velocity:

D[s[t], t]
s'[t]

Acceleration:

s''[t]

Second Derivative (single variable)

g[x_]:= ArcCos[x]
(∂_x) g[x]
(∂_x) %
(∂_x)((∂_x)g[x])
(∂_(x,x))g[x]
D[g[x],{x,2}]  (* note the 2 *)
g''[x]

For multivariable functions, use (∂_(□,□))□.  Note that the parentheses are necessary.

(∂_(x,y))((x^3) (y^4))

(∂_x)((∂_y) ((x^3) (y^4)))

Notation that arises when taking derivatives of undefined functions

reminder for single derivative

ClearAll["Global`*"]
(∂_x) g[x]

(∂_(x,x))s[x,y]      (*  ((∂^2)/(∂(x^2)))s[x,y]  *)
(∂_(y,y))s[x,y]      (*  ((∂^2)/(∂(y^2)))s[x,y]  *)
(∂_(x,y))s[x,y]      (* ((∂^2)/(∂x∂y))s[x,y]    *)
(∂_(y,x))s[x,y]     (* ((∂^2)/(∂y∂x))s[x,y]    *)

Three derivatives:

(∂_(y,x,z))s[x,y,z]

(∂_(y,x,z))(x+ y+ z)

(∂_(y,x,z))(Exp[x y z])
Simplify[%]
Factor[%%]

2D Gradient (Cartesian) acting on a f[x,y]   (((∂ )/(∂ x)) (OverHat[x]) +  ((∂ )/(∂ y)) (OverHat[y]) )  f[x,y]

ClearAll["Global`*"]
homemadegrad={1,0}(∂_x) h[x,y]+ {0,1} (∂_y) h[x,y]
h[x_,y_] := Cos[ x y]
gradh1 = homemadegrad
gradh2 = Grad[h[x,y],{x,y}]

Plot3D[h[x,y],{x,-3,3},{y,-3,3}]
vecyh = VectorPlot[gradh2,{x,-3,3},{y,-3,3}]

contouryh = ContourPlot[h[x,y],{x,-3,3},{y,-3,3}]

Show[contouryh,vecyh]

Define f[x] = (x^(-2));  Plot the function over the range  0<x<20

Clear["Global`*"];
f[x_] := (x^(-2))
Plot[f[x],{x,0,20}]

Use PlotRange

fplotty = Plot[f[x],{x,1,20}, PlotRange->{{0,20},{0,1}},PlotStyle->Purple]

Try just y axis PlotRange

Plot[f[x],{x,1,20}, PlotRange->{0,1}]

Now create a table of ordered pairs {x,f[x]} from x = 1 to x = 20 in steps of 0.    THIS IS LIKE DATA.

inversesquares =Table[{x,f[x]}, {x,1,20,.1}]

Now use ListPlot to plot these ordered pairs

listplotty = ListPlot[inversesquares,PlotRange-> {0,1},PlotStyle->Red]

Put the two together using Show

Show[listplotty, fplotty]

Put all the code into a single cell;  just keep the good stuff  PUT ; where it makes sense

Clear["Global`*"];
f[x_] := (x^(-2))
fplotty = Plot[f[x],{x,1,20}, PlotRange->{{0,20},{0,1}},PlotStyle->Red];
inversesquares =Table[{x,f[x]}, {x,1,20,0.1}];
listplotty = ListPlot[inversesquares,PlotRange-> {0,1},PlotStyle->Blue];
Show[listplotty, fplotty]
(*  Show [fplotty,listplotty]  *)