Template for Lab Exercises in PHYS 2502

I like to start every notebook with the following cell. It clears away any lingering definitions from the current Mathematica session, so can avoid lots of confusion when you have to debug things.

Remove["Global`*"]

Define an algebraic quantity “y” which is a function of “x”

y=a x^2+b x^3

Use the function “D” to take the derivative of y with respect to x. Note that all functions in Mathematica put the argument in square brackets.

yPrime=D[y,x]

Find values of x for which the derivative is zero. Note that the solution is a list of replacement statements.

sol=Solve[yPrime==0,x]

Prove that the second solution is in fact correct. (The parentheses around the replacement into yPrime are necessary.)

(yPrime/.sol[[2]])==0

Create versions of y and yPrime that put the derivative equal to zero at x=+1, and scale them by “a”. You might want to try removing the “Simplify” and also the semicolon at the end of the line, to see what happens.

sub=b->-2a/3;
yy=Simplify[(y/a)/.sub];
yyPrime=Simplify[(yPrime/a)/.sub];

Plot yy to check that there is an extremum at x=1

Plot[yy,{x,-1,2}]

Add yyPrime to the plot

Plot[{yy,yyPrime},{x,-1,2}]

Make the plot fancier, and add a vertical line at x=+1.

Show[
Plot[{yy,yyPrime},{x,-1,2},
PlotRange->{-0.5,1.0},
PlotStyle->{{Red,Thick},{Blue,Thick}},
LabelStyle->15,
PlotLegends->{"Function","Derivative"}],
ListLinePlot[{{1,-0.5},{1,0.5}},PlotStyle->{Black,Dotted}]
]