0
$\begingroup$

These are the three errors I was given. I so far wrote the code which was computing the polynomial interpolation but now I am asked to find those three norms: norm error mean norm error maximal absolute error

This is code I found that computers interpolation without the built in.

Clear["Global`*"]

f[x_] = x e^-x - 1
f[1];
f[2];
f[3];
f[4];

XY = {{1, -1 + 1/e}, {2, -1 + 2/e^2}, {3, -1 + 3/e^3}, {4, -1 + 4/e^4}};
p4[x_] = Fit[XY, {1, x, x^2, x^3, x^4}, x]
dots = ListPlot[XY, PlotStyle -> {PointSize[0.02]}];
gr5 = Plot[{f[x], p4[x]}, {x, -0.6, 2.1}, PlotStyle -> {Red, Green}];

Show[gr5, dots, PlotRange -> {{0, 1.2}, {0.0, 1.15}}]
$\endgroup$
5
$\begingroup$

First, a basic mistake: in Mathematica, the natural log base is entered as E (or as Esc e Esc), and not plain e. And many folks prefer Exp[x] to E^x.

Also, your lines f[1];, f[2];, etc., since they end with semicolons, just suppress the output from them.

Second, even with that corrected, everything is fine until your final Show, where you have so restricted your PlotRange as to exclude everything.

This works:

    f[x_] := x Exp[-x] - 1
    f[{1, 2, 3, 4}]
    XY = {{1, -1 + 1/E}, {2, -1 + 2/E^2}, {3, -1 + 3/E^3}, {4, -1 + 4/E^4}};

(* Out: {-1 + 1/E, -1 + 2/E^2, -1 + 3/E^3, -1 + 4/E^4}  *)

    p4[x_] = Fit[XY, {1, x, x^2, x^3, x^4}, x];
    dots = ListPlot[XY, PlotStyle -> {PointSize[0.02]}];
    gr5 = Plot[{f[x], p4[x]}, {x, -0.6, 2.1}, PlotStyle -> {Red, Green}];
    Show[dots, gr5]

Figure 1

    Show[gr5, dots, PlotRange -> {{0, 1.2}, {-0.75, -0.5}}, 
          AxesOrigin -> {0, -0.5}]

Figure 2

$\endgroup$
  • $\begingroup$ Thank you for the corrections as well! I appreciate it! $\endgroup$ – Sherien Hassan Oct 20 '19 at 22:54
4
$\begingroup$
Clear["Global`*"];

f[x_] = x E^-x - 1; (* Note `E` vice `e` *)

XY = {#, f[#]} & /@ Range[4]

(* {{1, -1 + 1/E}, {2, -1 + 2/E^2}, {3, -1 + 3/E^3}, {4, -1 + 4/E^4}} *)

p4[x_] = Fit[XY, {1, x, x^2, x^3, x^4}, x]

(* -0.589552 - 0.0147616 x - 0.0248196 x^2 - 0.00460293 x^3 + 0.00161548 x^4 *)

Plot[{p4[x], f[x]}, {x, 0, 4},
 PlotStyle -> {Green, Red},
 PlotLegends -> Placed["Expressions", {0.75, 0.75}],
 Epilog -> {PointSize[0.02], Point[XY]}]

enter image description here

Adding a point for x == 0

XY2 = {#, f[#]} & /@ Range[0, 4]

(* {{0, -1}, {1, -1 + 1/E}, {2, -1 + 2/E^2}, {3, -1 + 3/E^3}, {4, -1 + 4/E^4}} *)

p42[x_] = Fit[XY2, {1, x, x^2, x^3, x^4}, x]

(* -1. + 0.840339 x - 0.62339 x^2 + 0.166417 x^3 - 0.0154865 x^4 *)

Plot[{p42[x], f[x]}, {x, 0, 4},
 PlotStyle -> {Green, Red},
 PlotRange -> All,
 PlotLegends -> Placed["Expressions", {0.75, 0.75}],
 Epilog -> {PointSize[0.02], Point[XY2]}]

enter image description here

$\endgroup$
  • $\begingroup$ Thank you so much, that helped a lot and it is helping me see how interpolation should look graphically! I appreciate the corrections! $\endgroup$ – Sherien Hassan Oct 20 '19 at 22:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.