Skip to main content
A typo in the title.
Link
user64494
  • 29.1k
  • 4
  • 29
  • 56

How to use Module function?

added 48 characters in body
Source Link

Here is my issue; I want to do the same processiterate a fit to this data for a setpolynomial of degree 2 up till degree 6. However, at each iteration I drop certain data points changing somethat have a poor residual value. So each time the degree is raised, a subset of the variables values slightly between runsprevious data set's fit is used. However I do not want to havewill also need to rename everything in this sectionchange the value of codethe max residual that I want to use over and over. How can I set up the program so that it can use the same definitions of variables throughoutinclude the program? I understand Module is a good way to do this, however I cannot get it to work. Here is07 in the procedure I want to do:code below).

data = Table[{x, RandomReal[{-.1, .1}] + x^2}, {x, 0, 15}]

lstplt = LinearModelFit[data, Table[x^i, {i, 2}], x]
Plot[lstplt[x], {x, 0, 15}]
reslist = 
  Inner[List, {data}[[1, All, 1]], lstplt["FitResiduals"], List];
Show[ListPlot[data], Plot[lstplt[x], {x, 0, 15}]]
ListPlot[reslist]
bres = Select[reslist, Abs[#[[2]]] > .07 &];
gres = DeleteCases[reslist, Alternatives @@ bres];
gpoints = gres[[All, 1]] \[Intersection] reslist[[All, 1]];
ListPlot[listtr3 = Select[data, gpoints~MemberQ~First[#] &]]

Again, the idea is that I could copy and paste this code below the output of the first run throughit would fit a quadratic, change some numbers slightly (say thethen take out residual values greater than .07 to. Take those data points that have a residual greater than .05)07 and execute againdelete them. Then take that set and fit a cubic polynomial, without havingand find points that have greater than a different residual value and delete them. Iterate this up to change the definition of 'bres' for exampledegree n polynomial.

Here is my issue; I want to do the same process to a set of data points changing some of the variables values slightly between runs. I do not want to have to rename everything in this section of code that I want to use over and over. How can I set up the program so that it can use the same definitions of variables throughout the program? I understand Module is a good way to do this, however I cannot get it to work. Here is the procedure I want to do:

data = Table[{x, RandomReal[{-.1, .1}] + x^2}, {x, 0, 15}]

lstplt = LinearModelFit[data, Table[x^i, {i, 2}], x]
Plot[lstplt[x], {x, 0, 15}]
reslist = 
  Inner[List, {data}[[1, All, 1]], lstplt["FitResiduals"], List];
Show[ListPlot[data], Plot[lstplt[x], {x, 0, 15}]]
ListPlot[reslist]
bres = Select[reslist, Abs[#[[2]]] > .07 &];
gres = DeleteCases[reslist, Alternatives @@ bres];
gpoints = gres[[All, 1]] \[Intersection] reslist[[All, 1]];
ListPlot[listtr3 = Select[data, gpoints~MemberQ~First[#] &]]

Again, the idea is that I could copy and paste this code below the output of the first run through, change some numbers slightly (say the .07 to .05) and execute again, without having to change the definition of 'bres' for example.

Here is my issue; I want to iterate a fit to this data for a polynomial of degree 2 up till degree 6. However, at each iteration I drop certain data points that have a poor residual value. So each time the degree is raised, a subset of the previous data set's fit is used. However I will also need to change the value of the max residual that I want to include the .07 in the code below).

data = Table[{x, RandomReal[{-.1, .1}] + x^2}, {x, 0, 15}]

lstplt = LinearModelFit[data, Table[x^i, {i, 2}], x]
Plot[lstplt[x], {x, 0, 15}]
reslist = 
  Inner[List, {data}[[1, All, 1]], lstplt["FitResiduals"], List];
Show[ListPlot[data], Plot[lstplt[x], {x, 0, 15}]]
ListPlot[reslist]
bres = Select[reslist, Abs[#[[2]]] > .07 &];
gres = DeleteCases[reslist, Alternatives @@ bres];
gpoints = gres[[All, 1]] \[Intersection] reslist[[All, 1]];
ListPlot[listtr3 = Select[data, gpoints~MemberQ~First[#] &]]

Again, the idea is that it would fit a quadratic, then take out residual values greater than .07. Take those data points that have a residual greater than .07 and delete them. Then take that set and fit a cubic polynomial, and find points that have greater than a different residual value and delete them. Iterate this up to degree n polynomial.

Source Link

How to use Module function

Here is my issue; I want to do the same process to a set of data points changing some of the variables values slightly between runs. I do not want to have to rename everything in this section of code that I want to use over and over. How can I set up the program so that it can use the same definitions of variables throughout the program? I understand Module is a good way to do this, however I cannot get it to work. Here is the procedure I want to do:

data = Table[{x, RandomReal[{-.1, .1}] + x^2}, {x, 0, 15}]

lstplt = LinearModelFit[data, Table[x^i, {i, 2}], x]
Plot[lstplt[x], {x, 0, 15}]
reslist = 
  Inner[List, {data}[[1, All, 1]], lstplt["FitResiduals"], List];
Show[ListPlot[data], Plot[lstplt[x], {x, 0, 15}]]
ListPlot[reslist]
bres = Select[reslist, Abs[#[[2]]] > .07 &];
gres = DeleteCases[reslist, Alternatives @@ bres];
gpoints = gres[[All, 1]] \[Intersection] reslist[[All, 1]];
ListPlot[listtr3 = Select[data, gpoints~MemberQ~First[#] &]]

Again, the idea is that I could copy and paste this code below the output of the first run through, change some numbers slightly (say the .07 to .05) and execute again, without having to change the definition of 'bres' for example.