0
$\begingroup$

Question: I am unsure how to graph the interpolated graph with the regular graph on the interval $[1,4]$. Not even the Show line works about even if I'm pretty sure it's written correctly.

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$

1 Answer 1

1
$\begingroup$

Here you go!

f[x_] := x*Exp[-x - 1]; 
XY = {{1, -1 + 1/Exp[1]}, {2, -1 + 2/Exp[2]}, {3, -1 + 3/Exp[3]}, {4, -1 + 4/Exp[4]}}; 
p4 = Fit[XY, {1, x, x^2, x^3, x^4}, x]; 
dots = ListPlot[XY, PlotStyle -> {PointSize[0.02]}]
gr5 = Plot[{f[x], p4}, {x, -0.6, 2.1}, PlotStyle -> {Red, Green}]
Show[gr5, dots, PlotRange -> {{0, 1.2}, {0., 1.15}}]

enter image description here

$\endgroup$
1
  • $\begingroup$ OHH yes now I see why my code was incomplete.Thank you so much! Although, now I think there must be a mistake with the code itself for I assumed the dots would be on the interpolated functions line, since isn't that the whole point of interpolation hm.. $\endgroup$ Commented Oct 20, 2019 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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