Initialization

SetOptions[Plot,PlotStyle->{Blue,AbsoluteThickness[2], Dashed},
ImageSize->500,AxesStyle->Directive[FontFamily->"Arial",FontSize->18,Black,AbsoluteThickness[0.5],Arrowheads[0.04]]];

Work

PH424 Mathematica Template

Basics

To execute a line of code, press "enter" (not return) or "shift return".
In this worksheet, text, or comment, is in Times font, formatted to appear against a blue background.  You can change this and other formatting under Edit Stylesheet in the Format menu.

It is often helpful to clear variables of any value that may be assigned to them.  Either of the following command works to clear the variables x and A.  It does NOT set them to zero, it just clears them.

Clear[x,A]
x=.
A=.

Define a function of x, which contains the variable A.
Always use the := (or "delayed set") to define a function rather than = (or "set").  
Use an underscore after the variable on the lhs when you define the function (but not on the rhs).

f[x_]:=A*x^2+3

Check that the function is what you thought it was.  Don't use an underscore after the variable on the lhs when you ask for the function to be returned.  You can also ask for the function for a specific value of the variable.

f[x] 
f[2]

Now assign A a value.  Use the = (or "set") or the := (delayed set) for this simple assignment.

A=0.5

Check the function again, now.

f[x]
f[3]

Plots

Plot the function.  Notice that it's fine to use another variable, in this case t.
The style of the plot was set earlier in the intialization section.

Plot[f[t],{t,0,5}]

You can change the plot style

Plot[f[t],{t,0,5},PlotStyle->{Red,AbsoluteThickness[1]}]

Plot two functions on the same graph

g[t_]:=3*t

g[t]

Plot[{f[t],g[t]},{t,0,5},PlotStyle->{{Blue,AbsoluteThickness[2],Dotted},{Black,AbsoluteThickness[1]}}]

Animation

Plot a traveling wave.  This is a snapshot of the wave displacement ψ as a function of horizontal position x, at successive time intervals.  Note the time slider, and the start/stop and faster/slower controls.

ψ[x__,t__]:=Cos[2*Pi*x-2*Pi*t];
Animate[Plot[ψ[x,t],{x,-1,1},PlotStyle->{Red,Thick},AxesLabel->{x,ψ}],{t,0,2},AnimationRunning->False]

Copyright 2017 Oregon State University
Version Feb 13, 2017