(*Video 1: Increasing/decreasing *)

f[x_]:=30 x-14 (x^2)-2 (x^3)+(x^4);
Manipulate[Grid[{{Row[{"Tangent line has slope = ",f'[x0]," and y-intercept =",f[x0]-x0 f'[x0]}]},{Plot[{f[x],f'[x0](x-x0)+f[x0]},{x,-5,5},PlotStyle->{Normal,Gray},PlotRange->{-100,200},ImageSize->500]}},Spacings->{1,1},Frame->All],{{x0,1},-5,5}]

(*Slope of tangent line is positive (value of derivative) --- function is locally increasing*)
(*Slope of tangent line is negative (value of derivative) --- function is locally decreasing*)

f[x]
f'[x]

Plot[{f[x],f'[x]},{x,-5,5}]

(*f[x] is increasing in the region where f'[x]>0*)

f'[x]>0

Factor[f'[x]](*To determine by hand where f'[x] is positive, we factor it:*)

Reduce[f'[x]>0](*With Mathematica we can use Reduce*)

(*f[x] is decreasing in the region where f'[x]<0*)

Reduce[f'[x]<0]

increase=Show[Plot[f[x],{x,-5/2,1},PlotRange->{{-5,5},{-100,200}},PlotStyle->Blue],
Plot[f[x],{x,3,5},PlotRange->{{-5,5},{-100,200}},PlotStyle->Blue]]

decrease=Show[Plot[f[x],{x,-5,-5/2},PlotRange->{{-5,5},{-100,200}},PlotStyle->Red],
Plot[f[x],{x,1,3},PlotRange->{{-5,5},{-100,200}},PlotStyle->Red]]

Show[increase,decrease]

(*Video 2: Extrema *)

f[x]

(*Points where f[x] goes from increasing to decreasing are local maxima,
i.e. f'[x] goes from being positive to being negative *)
(*Points where f[x] goes from decreasing to increasing are local minimum,
i.e. f'[x] goes from being negative to being positive.*)

f'[x]==0(*solutions to this equation are called critical points*)

Solve[30-28 x-6 (x^2)+4 (x^3)==0]

Plot[f'[x],{x,-5,5},PlotStyle->Orange]

(*x=-(5/2) is a local minimum:*)
f'[-(5/2)-0.1]
f'[-(5/2)]
f'[-(5/2)+0.1]

(*x=1 is a local maximum:*)
f'[1-0.1]
f'[1]
f'[1+0.1]

(*x=1 is a local minimum:*)
f'[3-0.1]
f'[3]
f'[3+0.1]

Plot[{f[x],f'[x0](x-x0)+f[x0]/.{x0->1},f'[x0](x-x0)+f[x0]/.x0->3,f'[x0](x-x0)+f[x0]/.x0->-5/2},{x,-5,5}]

(*In graph above, all critical points (values of x such that f'[x]=0) are either local minima or local maxima. These are called local extrema for f[x]! *)

(*For global minima and maxima on an interval [a,b], check values at critical points as well as the endpoints x=a and x=b*)

(*Values of f[x] at endpoints of [-5,5] *)f[-5]

f[5]

(*Values of f[x] at critical points in the interior of [-5,5] *)
N[f[-5/2]]
f[1]
f[3]

(*Global maximum for f[x] is 375, achieved at x=-5*)
(*Global minimum for f[x] is -92.1875 achieved at x=-5/2*)

(*Video 3: Mean Value Theorem *)

(*If f[x] is continuous on [a,b] and differentiable on (a,b), then there exists (at least one) value x=c such that
f'[c](b-a) = f(b)-f(a),
equivalently:
 f'[c] = (f(b)-f(a))/(b-a),
i.e., tangent line to y=f[x] at x=c has same slope as line through (a,f[a]) and (b,f[b]).
*)

f[x]

(*let's take some different values of a,b to see these lines:*)
a=-4;b=4;
(f[b]-f[a])/(b-a)

secantline

pts=NSolve[f'[x0]==(f[b]-f[a])/(b-a)]

secantline=Plot[{f[x],(f[b]-f[a])/(b-a)(x-a)+f[a]},{x,-5,5},PlotRange->{{-5,5},{-100,200}},PlotStyle->{Normal,Red}];
tangentlines=Plot[{f'[x0](x-x0)+f[x0]/.pts[[1,1]],f'[x0](x-x0)+f[x0]/.pts[[2,1]],f'[x0](x-x0)+f[x0]/.pts[[3,1]]},{x,-5,5},PlotRange->{{-5,5},{-100,200}},PlotStyle->Gray];
Show[secantline,tangentlines]

a=-3;b=5;
(f[b]-f[a])/(b-a)

pts=NSolve[f'[x0]==(f[b]-f[a])/(b-a)];
secantline=Plot[{f[x],(f[b]-f[a])/(b-a)(x-a)+f[a]},{x,-5,5},PlotRange->{{-5,5},{-100,200}},PlotStyle->{Normal,Red}];
tangentlines=Plot[{f'[x0](x-x0)+f[x0]/.pts[[1,1]],f'[x0](x-x0)+f[x0]/.pts[[2,1]],f'[x0](x-x0)+f[x0]/.pts[[3,1]]},{x,-5,5},PlotRange->{{-5,5},{-100,200}},PlotStyle->Gray];
Show[secantline,tangentlines]

pts

a=-3;b=1;
(f[b]-f[a])/(b-a)

pts=NSolve[f'[x0]==(f[b]-f[a])/(b-a)];
secantline=Plot[{f[x],(f[b]-f[a])/(b-a)(x-a)+f[a]},{x,-5,5},PlotRange->{{-5,5},{-100,200}},PlotStyle->{Normal,Red}];
tangentlines=Plot[{f'[x0](x-x0)+f[x0]/.pts[[1,1]],f'[x0](x-x0)+f[x0]/.pts[[2,1]],f'[x0](x-x0)+f[x0]/.pts[[3,1]]},{x,-5,5},PlotRange->{{-5,5},{-100,200}},PlotStyle->Gray];
Show[secantline,tangentlines]

pts