(*Video 1: Optimization Problem 1 *)

(*A farmer wants to build a rectangular fenced backyard for the chickens to run free. The farmer only has 20m of fence available for the project. What is the largest area that can be fenced under those constraints? *)

Manipulate[
Grid[{{Column[{"Width = ",DecimalForm[a,4]," Height = ",DecimalForm[b,4]}]},{Column[{" Area = ",DecimalForm[a b,4]," Perimeter = ",DecimalForm[2a+2b,4]}]},
{Graphics[{{White,Rectangle[{0,0},{10,10}]},EdgeForm[Thick],Green,Rectangle[{0,0},{a,b}]}]}},Spacings->{1,1},Frame->All],{{a,1},0,10},{{b,1},0,10}]

(*Constraint: Perimeter of a rectangle of width = a and height = b is 2a+2b == 20 *)

2a+2b==20

(*Solve for b:*)
Solve[2a+2b==20,b]

(*Target function: Area of a rectangle of width = a and height = b is a*b -- would like to max*)
a*b/.{b->10-a}(*replace from constraint*)

Manipulate[
Grid[{{Column[{"Width = ",DecimalForm[a,4]," Height = ",DecimalForm[(10-a),4]}]},{Column[{" Area = ",DecimalForm[a (10-a),4]," Perimeter = ",DecimalForm[2a+2(10-a),4]}]},
{Graphics[{{White,Rectangle[{0,0},{10,10}]},EdgeForm[Thick],Green,Rectangle[{0,0},{a,(10-a)}]}]}},Spacings->{1,1},Frame->All],{{a,1},0,10}]

(*Target function, with constraint substituted in:*)
f[a_]:=(10-a) a

(*Critical points?*)
Solve[f'[a]==0,a]

(**a=5 is the only critical point. Let's apply the second derivative test:)

f''[5]

(*f'' < 0 so a=5 is a local maximum!*)

Expand[f[a]]
Plot[f[a],{a,0,10}]

f[5]

(*Maximum area, under the above constraints, is achieved when a=5. Geometrically, this corresponds to the square being the rectangle of largest area for a fixed perimeter.*)

(*This computation can be confirmed by Mathematica as follows, without restricting domain:*)
Maximize[f[a],a]
Minimize[f[a],a]

(*Restricting the domain:*)
Maximize[{f[a],0≤a≤10},a]
Minimize[{f[a],0≤a≤10},a]

(*Video 2: Optimization Problem 2 *)

(*A farmer wants to build a rectangular fenced backyard for the chickens to run free. The farmer only has 20m of fence available for the project but will build the yard adjacent to a straight river which serves as a natural fence. What is the largest area that can be fenced under those constraints? *)

Manipulate[
Grid[{{Column[{"Width = ",DecimalForm[a,4]," Height = ",DecimalForm[b,4]}]},{Column[{" Area = ",DecimalForm[a b,4]," Perimeter = ",DecimalForm[a+2b,4]}]},
{Graphics[{{White,Rectangle[{0,0},{10,10}]},{EdgeForm[Thick],Green,Rectangle[{0,0},{a,b}]},Blue,Rectangle[{-5,0.2},{15,-5}]}]}},Spacings->{1,1},Frame->All],{{a,1},0,10},{{b,1},0,10}]

(*Constraint: Total perimeter only includes 3 of the sides of the rectangle, namely it is a+2b*)

a+2b==20
Solve[a+2b==20,b]

(*Target function*)

a*b/.{b->((20-a)/2)}

g[a_]:=(1/2) (20-a) a

Manipulate[
Grid[{{Column[{"Width = ",DecimalForm[a,4]," Height = ",DecimalForm[((20-a)/2),4]}]},{Column[{" Area = ",DecimalForm[a (((20-a)/2)),4]," Perimeter = ",DecimalForm[a+2(((20-a)/2)),4]}]},
{Graphics[{{White,Rectangle[{0,0},{10,10}]},{EdgeForm[Thick],Green,Rectangle[{0,0},{a,((20-a)/2)}]},Blue,Rectangle[{-5,0.2},{25,-5}]}]}},Spacings->{1,1},Frame->All],{{a,1},0,20}]

Expand[g[a]]

(*To find extremal points (minima/maxima) we first compute critical points:*)
Solve[g'[a]==0,a]

g''[10]

(*So a=10 is the maximum of the function g[a] on the interval 0≤a≤10*)

Plot[g[a],{a,0,20}]

(*Maximum area is: *)g[10]
(*Sides achieving this are:*){a,((20-a)/2),((20-a)/2)}/.a->10

(*The maximum area under these new constraints is 50m^2 and is achieved at a rectangle with one side of length a=10 and two sides of length 5*)

Maximize[g[a],a]

(*Video 3: Optimization Problem 3 *)

(*Find the minimum and maximum of the function f(x)=3x^4-4x^3+8 for x in the interval [-2,4] *)

Plot[3x^4-4x^3+8,{x,-2,4}]

(*Step 1: Identify target function and constraints*)
f[x_]:=3x^4-4x^3+8 (*target function*)
-2≤x≤4 (*constraints*)

(*Step 2: Find critical points of the target function, after possibly substituting in the constraints to reduce to one variable*)

Solve[f'[x]==0,x]

(*Step 3: Determine the type of each critical point*)
f''[0]
f''[1]

(*x=1 is a local minimum*)

Plot[f'[x],{x,-1/2,1/2}]

(*Since tangent line has negative slope both before and after x=0, this critical point is not a local min nor a local max*)

Plot[f[x],{x,-1/2,1/2}]

(*Step 4: Compare value of the function at local min/max inside the domain with values of the function at the boundary of its domain (endpoints) *)

(*Left endpoint*)f[-2]
(*Right endpoint*)f[4]

(*Critical points in the interior*)f[1]

(*Conclusion: the largest among these values is the global max; the smallest is the global min.*)

(*Global minimum value is 7, achieved at the point x=1 *)
(*Global maximum value is 520, achieved at the point x=4 *)

Plot[f[x],{x,-2,4}]

Maximize[{3x^4-4x^3+8,-2≤x≤4},x]
Minimize[{3x^4-4x^3+8,-2≤x≤4},x]