<< Graphics`Colors`
Off[General::"spell1"];
Off[General::"spell"];
\(\(AnimateFrames[
          inFrames_] := 
Module[{frames = inFrames, cells, 
            i}, 
cells = 
            Table[Cell[
		GraphicsData[\ \ "PostScript", 
                  frames[\([i]\)]\ \ ], 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ "Graphics", 
		AnimationDisplayTime -> If[i == Length[frames], 1000000, 0.2]], 
{i, 
                1, Length[frames]}]; 
\ \ \ NotebookWrite[\ 
            EvaluationNotebook[], 
            CellGroupData[cells, 
              Closed]\ ];
];\)\)
\(\(AnimateLinearModels[inData_, inMRange_] := 
        Module[
{minM = inMRange[\([1]\)], 
            maxM = inMRange[\([2]\)], itM = 0.01, maxX, nm, data = inData, 
            graphicalPoints, i, n = Length[inData], graphicalLines, m, 
            frames}, 
nm = 
            Length[Range[minM, maxM, 
                itM]]; 
graphicalPoints = 
            Table[Graphics[{PointSize[0.018], Blue, 
                  Point[data[\([i]\)]]}], {i, 1, 
                n}]; 
maxX = 
            data[\([n, 1]\)]; 
maxY = 
            data[\([n, 2]\)]; 
graphicalModelCurves = 
            Table[Plot[m*x, {x, 0, maxX}, PlotStyle -> {Red}, 
                DisplayFunction -> Identity], {m, minM, maxM, 
                itM}]; 
graphicalText = 
            Table[Graphics[{Purple, 
                  Text["m = " <> ToString[m], {3, 0.2}, {\(-1\), \(-1\)}, 
                    Background -> LightPink, 
                    TextStyle -> {FontSize -> 16}]}], {m, minM, 
                maxM, itM}]; 
leftGraphs = 
            Table[Join[
                graphicalPoints, {graphicalModelCurves[\([m]\)]}, {graphicalText[\([m]\)]}], {m, 1, nm}]; 
leftGraphs = 
            Table[Show[leftGraphs[\([m]\)], Axes -> True, 
                DisplayFunction -> Identity, 
                PlotRange -> {{0, maxX}, {0, maxY}}, 
                AspectRatio -> Automatic], {m, 1, 
                nm}]; 
frames = 
            Table[Show[leftGraphs[\([i]\)], 
                DisplayFunction -> DisplayString, 
                ImageSize -> {900, 500}], {i, 1, 
                nm}]; 
AnimateFrames[
            frames];
];\)\)

Enter the data:

testData = {{20, 95}, {27, 90}, {30, 98}, {35, 103}, {41, 112}, {44, 
            109}, {46, 115}};

Question 1

p1 = 
        ListPlot[testData, PlotStyle -> {PointSize[0.02], Blue}, 
          DisplayFunction -> Identity];

Clear[H4];
\(\(H4[m_, 
          b_] = \((\((20 - \((95 - b)\)/m)\)^4 + \((27 - \((90 - b)\)/
                    m)\)^4 + 
		\((30 - \((98 - b)\)/
                    m)\)^4 + \((35 - \((103 - b)\)/
                    m)\)^4 + 
		\((41 - \((112 - b)\)/
                    m)\)^4 + \((44 - \((109 - b)\)/
                    m)\)^4 + 
		\((46 - \((115 - b)\)/
                    m)\)^4)\);\)\)

We plot the graph (a surface) of (Subscript[H, 4]):

Plot3D[{H4[m, b], Hue[H4[m, b]/300000000]}, {m, 0.2, 1.5}, {b, 50, 
          120}, 
PlotPoints -> 
          40, 
ImageSize -> {800, 
            550}];

A contour plot will show the region of lowest level in the darkest color:

ContourPlot[
        H4[m, b], {m, 0.5, 1.5}, {b, 50, 
          100}, 
PlotPoints -> 
          40, 
Contours -> 
          20, 
ImageSize -> {800, 550}];

To find exactly where the minimum of (Subscript[V, 2])(m, b) is we must, simultaneously solve (∂_m) (Subscript[V, 2])(m, b) = 0 and (∂_b) (Subscript[V, 2])(m, b) = 0

Solve[{D[H4[m, b], m] == 0, D[H4[m, b], b] == 0.0}, {m, 
        b}]

Plot the optimal y  =  m x + b model:

mv=;
bv=;
p2 = 
        Plot[mv*x + bv, {x, 0, 50}, PlotStyle -> {Red}, 
          DisplayFunction -> Identity];
Show[{p1, p2}, 
        DisplayFunction -> $DisplayFunction];

Question 2

\(\(P1[m_, 
          b_] = \((1/
              Sqrt[m^2 + 1])\)*\((\((95 - 20  m - b)\) + \((90 - 27  m - 
                  b)\) + 
		\((98 - 30  m - 
                  b)\) + \((103 - 35  m - 
                  b)\) + 
		\((112 - 41  m - 
                  b)\) + \((109 - 44  m - 
                  b)\) + 
		\((115 - 46  m - 
                  b)\))\);\)\)

We plot the surface:

Plot3D[{P1[m, b], Hue[P1[m, b]/200]}, {m, 0, 2}, {b, 50, 
          100}, 
PlotPoints -> 
          40, 
ImageSize -> {800, 
            550}, 
PlotRange -> {0, 
            200}];

Solve[P1[m, b] == 0, {m, b}]

badb = 243;
badm = (722/243) - ((7 badb)/243)
p2 = 
        Plot[badm*x + badb, {x, 0, 50}, PlotStyle -> {Red}, 
          DisplayFunction -> Identity];
Show[{p1, p2}, 
        DisplayFunction -> $DisplayFunction];

Question 3

Enter the data:

testData := {{0, 0}, {1, 1.2}, {2, 3.8}, {3, 4.2}, {4, 6.3}, {5, 
            7.2}, {6, 7.3}};

Plot data

p1 = 
        ListPlot[testData, PlotStyle -> {PointSize[0.02], Blue}, 
          DisplayFunction -> Identity];

Objective Function:

V2[m_, b_] = 
        Sqrt[(1.2 - m - b)^2 + (3.8 - 2  m - b)^2 + (4.2 - 3  m - 
                  b)^2 + (6.3 - 4  m - b)^2 + (7.2 - 5  m - 
                  b)^2 + (7.3 - 6  m - b)^2];

Plot3D[{V2[m, b], Hue[V2[m, b]/30]}, {m, 1, 2}, {b, -10, 
          10}, 
PlotPoints -> 
          40, 
ImageSize -> {800, 550}];

Solve[{D[V2[m, b], m] == 0, D[V2[m, b], b] == 0.0}, {m, 
        b}]

Plot the optimal y  =  m x + b model:

mv = 1.2228571428571429`;
bv = 0.72`;
p2 = 
        Plot[mv*x + bv, {x, 0, 10}, PlotStyle -> {Red}, 
          DisplayFunction -> Identity];
Show[{p1, p2}, 
        DisplayFunction -> $DisplayFunction];