# How to get error(difference between point and fitted curve) and plot error with curve?

It's embarassing to ask this type of question but I searched a lot and there is no way I can find to get the difference between plot and curve.

I plotted prime numbers and got fitted curve. But I cannot get difference between the plot and fitted curve. R squared and least square are not appropriate I think. Please can anyone give me a hint?

Some code:

ws = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}
p1 = ListPlot[ws]
fit = Fit[ws, {1, x, x^2}, x];
p2 = Plot[fit, {x, 0, 12}];
Show[p1, p2]

• Please post your code as text rather than in a picture. You'll get more folks willing to help. – JimB Jan 28 '17 at 5:53
• Please don't replace your question with "Thank you!" – Michael E2 Jan 29 '17 at 2:43
• Could a kind soul please add the code in text format, as Woochul seems to be unable or unwilling to do so? – J. M. is in limbo Jan 29 '17 at 17:23

When one needs such things as the residuals (which I would argue is all of the time), use LinearModelFit:

ws = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}
ListPlot[ws]
lm = LinearModelFit[ws, {x, x^2}, x]
lm["FitResiduals"]

(* {0.398352, -0.297952, -0.293457, -0.588162, 0.817932, -0.0751748,
0.732517, -0.758991, -0.5497, 1.36039, -1.02872, 0.282967} *)

• I really appreciate it Mr. code master – Woochul Shin Jan 29 '17 at 2:14

Another way is simply to compute the difference between the fit function you defined and the original values at the given points e.g.

ws = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
p1 = ListPlot[ws];
wsfit[x_] = Fit[ws, {1, x, x^2}, x];
p2 = Plot[wsfit[x], {x, 0, 12}];
ListPlot[wsfit[Range[12]]-ws]


which yields the same values

(*{-0.398352, 0.297952, 0.293457, 0.588162, -0.817932, 0.0751748, -0.732517,
0.758991, 0.5497, -1.36039, 1.02872, -0.282967}*)

• Thank you so much I appreciate it!! – Woochul Shin Jan 29 '17 at 2:14