2
$\begingroup$

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?

enter image description here

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]
$\endgroup$
  • 3
    $\begingroup$ Please post your code as text rather than in a picture. You'll get more folks willing to help. $\endgroup$ – JimB Jan 28 '17 at 5:53
  • 1
    $\begingroup$ Please don't replace your question with "Thank you!" $\endgroup$ – Michael E2 Jan 29 '17 at 2:43
  • $\begingroup$ Could a kind soul please add the code in text format, as Woochul seems to be unable or unwilling to do so? $\endgroup$ – J. M. will be back soon Jan 29 '17 at 17:23
8
$\begingroup$

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} *)
$\endgroup$
  • $\begingroup$ I really appreciate it Mr. code master $\endgroup$ – Woochul Shin Jan 29 '17 at 2:14
1
$\begingroup$

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}*)
$\endgroup$
  • $\begingroup$ Thank you so much I appreciate it!! $\endgroup$ – Woochul Shin Jan 29 '17 at 2:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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