0
$\begingroup$

Well, I've the following problem. I need to plot the error that the data gives with the prediction. I've the following code:

Show[{Plot[
   0.5*((1/Sqrt[(1 - (2530253 f^2 \[Pi]^2)/12500000000)^2 + ((
             59 f \[Pi])/62500 - (2783 f^3 \[Pi]^3)/625000000000)^2])/
      Sqrt[2]), {f, 10, 32}], 
  ListPlot[{{10, 0.4471}, {15, 0.6430}, {20, 1.66965}, {21, 
     2.577}, {22, 4.5811}, {22.1, 4.7738}, {22.2, 4.91835}, {22.3, 
     4.9966}, {22.3505609997, 5.005}, {22.4, 4.9940}, {22.5,}, {22.6, 
     4.7640}, {22.7, 4.5625}, {22.8, 4.33145}, {22.9, 4.08285}, {23, 
     3.8312}, {25, 1.3533}, {27, 0.76625}, {30, 0.45355}}]}]

And the plot part is the prediction and the listplot are the data points that I found by measuring on a system. Now how can I write a code that gives the plot of the error between the prediction and the measurement?

$\endgroup$
3
$\begingroup$

Letting

data = {{10, 0.4471}, {15, 0.6430}, {20, 1.66965}, {21, 2.577}, {22, 4.5811},
        {22.1, 4.7738}, {22.2, 4.91835}, {22.3, 4.9966}, {22.3505609997, 5.005},
        {22.4, 4.9940}, {22.5,}, {22.6, 4.7640}, {22.7, 4.5625}, {22.8, 4.33145},
        {22.9, 4.08285}, {23, 3.8312}, {25, 1.3533}, {27, 0.76625}, {30, 0.45355}};

(note the missing value corresponding to 22.5)

and

jopi[f_] := 0.5*((1/Sqrt[(1 - (2530253 f^2 π^2)/12500000000)^2 +
                ((59 f π)/62500 - (2783 f^3 π^3)/625000000000)^2])/Sqrt[2])

here is one way to plot your model, data, and residuals:

{Show[{Plot[jopi[f], {f, 10, 32}], ListPlot[data]}, PlotLabel -> "Data and Model"], 
 ListPlot[#2 - jopi[#1] & @@@ data,
          Filling -> Axis, PlotLabel -> "Residuals"]} // GraphicsRow

plot

where ListPlot[] automagically filtered out the bad point.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Where are the @@@ signs for? $\endgroup$ – jopi Mar 12 '18 at 14:58
  • 1
    $\begingroup$ To give you a tip: anytime you encounter an unfamiliar symbol like @@@ in any code you see in Mathematica, highlight it and press F1. In this case, it should take you to the documentation for Apply[]. $\endgroup$ – J. M.'s discontentment Mar 12 '18 at 16:04
2
$\begingroup$

@J.M. has already answered your question but if you're looking for a better prediction, you might consider the following fit using the same model structure but just with different coefficients:

data = {{10, 0.4471}, {15, 0.6430}, {20, 1.66965}, {21, 2.577}, {22, 4.5811},
   {22.1, 4.7738}, {22.2, 4.91835}, {22.3, 4.9966}, {22.3505609997, 5.005},
   {22.4, 4.9940}, {22.6, 4.7640}, {22.7, 4.5625}, {22.8, 4.33145},
   {22.9, 4.08285}, {23, 3.8312}, {25, 1.3533}, {27, 0.76625}, {30, 0.45355}};

nlm = NonlinearModelFit[data, 
   a1/Sqrt[(1 - a2 f^2)^2 + (a3 f - a4 f^3)^2],
   {{a1, 0.35}, {a2, 0.002}, {a3, 0.003}, {a4, 1.4 10^(-7)}}, f];

nlm["BestFitParameters"]
(* {a1 -> 0.352095, a2 -> 0.00199721, a3 -> 0.00314666, a4 -> -1.0251*10^-14} *)

{Show[{Plot[nlm[f], {f, 10, 32}], ListPlot[data]}, 
   PlotLabel -> "Data and Model"],
  ListPlot[Transpose[{nlm["PredictedResponse"], nlm["FitResiduals"]}],
   Filling -> Axis, PlotLabel -> "Residuals", 
   AxesLabel -> {"Prediction", "Residual"}]} // GraphicsRow

Data, fit, and residuals

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks for your answer. $\endgroup$ – jopi Mar 12 '18 at 21:43

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.