Message Boards Message Boards

0
|
2881 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

I get an error when I do this, so how would you do this?

Anonymous User
Anonymous User
Posted 10 years ago

Enter the following input to create a command that will plot a collection of data, a fit function, and the residuals for the data and the given function. Test the command with the fit function f(x)= 3-.25x+.125x^2

residualPlot[data_,function_,{x_,xmin_,xmax_},opts___Rule]:= 
Show[ListPlot[{data,Table[{x,function}, {x,data[[All,1]]}]}, Filling -> {1->{2}},
FillingStyle-> {Red,Green}, PlotMarkers-> {"*",""}, opts],
Plot[function, {x,xmin,xmax}]]
POSTED BY: Anonymous User
3 Replies
Posted 10 years ago

Your code is almost correct :)

residualPlot[data_, function_, {xmin_, xmax_}, opts : OptionsPattern[]] := 
 Show[ListPlot[{data, Table[{x, function[x]}, {x, data[[All, 1]]}]}, 
   Filling -> {1 -> {2}}, FillingStyle -> {Red, Green}, PlotMarkers -> {{"*", Large}, ""}, opts], 
  Plot[function[x], {x, xmin, xmax}]]

However, once you have defined the function above, let's say:

f[x_] := 3 - .25 x + .125 x^2 

there is not need to include it as an argument of the residualPlot function:

residualPlot[data_, {xmin_, xmax_}, opts : OptionsPattern[]] := 
 Show[ListPlot[{data, Table[{x, f[x]}, {x, data[[All, 1]]}]}, 
   Filling -> {1 -> {2}}, FillingStyle -> {Red, Green}, PlotMarkers -> {{"*", Large}, ""}, opts], Plot[f[x], {x, xmin, xmax}]]

Here is how it works:

enter image description here

Looks fine, but you may encounter a problem when plotting the same function with data which is not generated from xmin to xmax. There is a trick which allows you to deal with it without specifying the {ymin, ymax} in the PlotRange - you can just change the places of the plots:

residualPlot[data_, {xmin_, xmax_}, opts : OptionsPattern[]] := 
 Show[Plot[f[x], {x, xmin, xmax}], 
  ListPlot[{data, Table[{x, f[x]}, {x, data[[All, 1]]}]}, Filling -> {1 -> {2}}, 
    FillingStyle -> {Red, Green}, PlotMarkers -> {{"*", Large}, ""}],opts]

then the LogPlot will inherit the "properties" of the first Plot (but now you need to change the "opts" to Plot[], or better to Show[]).

POSTED BY: Sandu Ursu
Anonymous User
Anonymous User
Posted 10 years ago

I get an error of incomplete and it says more input is needed, i dont know why.

POSTED BY: Anonymous User

That works, only correction needed was to give the argument x to the function function

Clear[residualPlot]
residualPlot[data_, function_, {x_, xmin_, xmax_}, opts___Rule] := 
 Show[ListPlot[{data, Table[{x, function[x]}, {x, data[[All, 1]]}]}, 
   Filling -> {1 -> {2}}, FillingStyle -> {Red, Green}, 
   PlotMarkers -> {"*", ""}, opts], Plot[function[x], {x, xmin, xmax}]]

Clear[theFct]
theFct[x_] := 3 - .25 x + .125 x^2

someData = 
  Table[{x, theFct[x] + RandomReal[{-1, 4/5}]}, {x, 0, 4, 0.1}];

residualPlot[someData, theFct, {x, 0, 4}]
POSTED BY: Udo Krause
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract