Re: Modeling and Array Problem

*To*: mathgroup at smc.vnet.net*Subject*: [mg58753] Re: Modeling and Array Problem*From*: Curtis Osterhoudt <gardyloo at mail.wsu.edu>*Date*: Sun, 17 Jul 2005 03:04:04 -0400 (EDT)*References*: <200507160503.BAA14933@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

Hi Peter, You might want to check out the documentation for "ReplacePart"; I think it'll do just what you want (e.g., array = ReplacePart[array, 42, {1,1}] as per your example). On the topic of finding the data's curvature, I've written a package called ListOps, which I use quite a bit. I've included it below. It strikes me that if you apply "SuccessiveDifferential" a couple of times to your data, it'll do what you want. Try ListPlot-ting the results! Good luck! C. O. (****** Save the following lines in a file called "ListOps.m" in a folder called "Osterhoudt" in your Addons/ExtraPackages directory.******) BeginPackage["Osterhoudt`ListOps`", {"Statistics`DataManipulation`"}] Osterhoudt::usage = "'Osterhoudt' is just a directory in the Mathematica directory, which contains the packages which I write for Mathematica." SuccessiveDifferences::usage = "SuccessiveDifferences[data] calculates and returns the successive differences of points in a discrete data list. This list must be one-dimensional. For example, SuccessiveDifferences[{1, 2, 3, 4, -8}] = {1, 1, 1, -12}." \ SuccessiveSums::usage = "SuccessiveSums[data] calculates and returns the successive sums of points in a discrete data list. This list must be one-dimensional. For example, SuccessiveSums[{1, 2, 3, 4, -8}] = {3, 5, 7, -4}." SuccessiveAverages::usage = "SuccessiveAverages[data] calculates and returns the successive averages of points in a data set. The input list must be one-dimensional. For example, SuccessiveAverages[{1, 2, 3, 4, -8}] = {1.5, 2.5, 3.5, -2}." SuccessiveDifferential::usage = "SuccessiveDifferential[data] calculates and returns the point-by-point derivative of points in a data set. The input list must be two-dimensional." Begin["`Private`"] SuccessiveDifferences[data_] := ListCorrelate[{-1, 1}, data]; SuccessiveSums[data_]:= ListCorrelate[{1, 1}, data]; SuccessiveAverages[data_]:= SuccessiveSums[data]/2; SuccessiveDifferential[list_]:= Transpose[{SuccessiveAverages[Column[list, 1]], SuccessiveDifferences[Column[list, 2]]/SuccessiveDifferences[Column[list, 1]] } ]; End[] (* Tells Mathematica to revert to the previous context (makes ListOps` "private") *) EndPackage[] (*Ends the package, prepending ListOps` to the context search path. *) (* Now, the symbols "data", and "list" are in the private context ListOps`Private` and, as such, won't interfere wit h other uses for these symbols, because they're no longer accessible by their "short names" *) (*****************************************************************************************************************) Sycamor at gmail.com wrote: >Hello, > >I am a high school student and am interning/volunteering at a >university over the summer. My ultimate goal is to model the movement >of charge in a Nickel Metal Hydride Sphere using Mathematica. This >goal is beyond my ability as it requires calculus and differential >equations and so forth. But I am to progress as best I can, using >iterative processes to replace calculus where possible. The professor >I am working with has started me with the simpler task of finding the >curvature of a set of data points (in this first easy case, the 'data' >is the values of 101 points of the x^2 function). > >While programming, I have found it necessary to change the value of >certain elements of an array of ordered pairs, but have been unable to >do so. > >In[1]:= array = {{1,1},{2,2},{3,3},{4,4},{5,5}}; >In[2]:= array[[1]][[1]] >Out[2]= 1 >In[2]:= array[[1]][[1]] = 42 >Out[2]= Set::setps: test in assignment of part is not a symbol. > >I suppose the error arises when I attempt to set the numerical contents >of the array equal to a certain value. Is there anyway to simply >overwrite the element at a certain address? Why does the syntax used >above seem to work for lists? > >In[3]:= list = {1,2,3,4,5}; >In[4}:= list[[1]] >Out[4]= 1 >In[5]:= list[[1]] = 42; >In[6]:= list >Out[6]= {42,2,3,4,5} > >I have included what I have written so far at the end of this message >to put my problem in context. As I am new to Mathematica, I do not >know hoe to write elegant, concise programs using the array of powerful >built in functions, so I appologize for my clunky code. There is >probably a much easier way to accomplish my task. > >I would appreciate any help on this matter. > > >Thank you, > >Peter Hedman > > > > > > >Bellow is what I have written so far. The comments are mostly meant >for myself, and probably do not make much sense. > >g = 1; > (*sets value of liberated proton constant*) > >d=0.1; > (*sets value of diffusion constant*) > >v= 0.1; > (*sets value of boundry velocity*) > >a = 0.1; > (*sets the value of constant \[Alpha]*) > >j = 0; > > (*sets the initial value of j. This step shouldn't be necessary*) > >rprevb=rprev = Table[0z, {z,0,100}]; > >r = 0; s = 0; > >(Do[rprev[([q])] = {r, s^2}; (r++); (s++), {q, 1, 101}];) > > (*These three lines create the initial list of ordered pairs*) > >Do[rprevb[[q]]={0,0}, {q,1,101}] > >jmax = 100; > (*initially sets the maximum horizontal range*) > >iter[n_]:= If[ > n==jmax , (rprev[[n+1]][[2]] + v*(g+rprev[[n+1]][[2]]) - > d*(rprev[[n+2]][[2]]-rprev[[n+1]][[2]])), (rprev[[n+1]][[2]]+ > a*(rprev[[n]][[2]]-2rprev[[n+1]][[2]]+rprev[[n+2]][[2]]))] ; > >(*defines transformation function*) > >dim =Dimensions[rprev]; > >dimb = Dimensions[rprevb]; > >Print["Dimensions of initial list = ",dim] > >Print["Dimensions of initial list = ",dimb] > >initialplot = ListPlot[rprev] > >Do[Do[rprevb[[(j+1)]][[2]]=iter[j]; Print[rprevb]; {j,0,jmax,1}]; > rprev=rprevb; Print["time = ",t]; Print["jmax = ",jmax ]; >ListPlot[rprev]; > jmax--,{t,1,10}] > >(*With every iteration of the outside loop, > jmax is decremented by one. As iterations progress to flatten the >curve, > the boundry moves. Should these two processes take place in this >way? > It appears this is not the ideal model, but it will work for >now.*) > > > > -- PGP Key ID: 0x235FDED1 Please avoid sending me Word or PowerPoint attachments. http://www.gnu.org/philosophy/no-word-attachments.html Sycamor at gmail.com wrote: >Hello, > >I am a high school student and am interning/volunteering at a >university over the summer. My ultimate goal is to model the movement >of charge in a Nickel Metal Hydride Sphere using Mathematica. This >goal is beyond my ability as it requires calculus and differential >equations and so forth. But I am to progress as best I can, using >iterative processes to replace calculus where possible. The professor >I am working with has started me with the simpler task of finding the >curvature of a set of data points (in this first easy case, the 'data' >is the values of 101 points of the x^2 function). > >While programming, I have found it necessary to change the value of >certain elements of an array of ordered pairs, but have been unable to >do so. > >In[1]:= array = {{1,1},{2,2},{3,3},{4,4},{5,5}}; >In[2]:= array[[1]][[1]] >Out[2]= 1 >In[2]:= array[[1]][[1]] = 42 >Out[2]= Set::setps: test in assignment of part is not a symbol. > >I suppose the error arises when I attempt to set the numerical contents >of the array equal to a certain value. Is there anyway to simply >overwrite the element at a certain address? Why does the syntax used >above seem to work for lists? > >In[3]:= list = {1,2,3,4,5}; >In[4}:= list[[1]] >Out[4]= 1 >In[5]:= list[[1]] = 42; >In[6]:= list >Out[6]= {42,2,3,4,5} > >I have included what I have written so far at the end of this message >to put my problem in context. As I am new to Mathematica, I do not >know hoe to write elegant, concise programs using the array of powerful >built in functions, so I appologize for my clunky code. There is >probably a much easier way to accomplish my task. > >I would appreciate any help on this matter. > > >Thank you, > >Peter Hedman > > > > > > >Bellow is what I have written so far. The comments are mostly meant >for myself, and probably do not make much sense. > >g = 1; > (*sets value of liberated proton constant*) > >d=0.1; > (*sets value of diffusion constant*) > >v= 0.1; > (*sets value of boundry velocity*) > >a = 0.1; > (*sets the value of constant \[Alpha]*) > >j = 0; > > (*sets the initial value of j. This step shouldn't be necessary*) > >rprevb=rprev = Table[0z, {z,0,100}]; > >r = 0; s = 0; > >(Do[rprev[([q])] = {r, s^2}; (r++); (s++), {q, 1, 101}];) > > (*These three lines create the initial list of ordered pairs*) > >Do[rprevb[[q]]={0,0}, {q,1,101}] > >jmax = 100; > (*initially sets the maximum horizontal range*) > >iter[n_]:= If[ > n==jmax , (rprev[[n+1]][[2]] + v*(g+rprev[[n+1]][[2]]) - > d*(rprev[[n+2]][[2]]-rprev[[n+1]][[2]])), (rprev[[n+1]][[2]]+ > a*(rprev[[n]][[2]]-2rprev[[n+1]][[2]]+rprev[[n+2]][[2]]))] ; > >(*defines transformation function*) > >dim =Dimensions[rprev]; > >dimb = Dimensions[rprevb]; > >Print["Dimensions of initial list = ",dim] > >Print["Dimensions of initial list = ",dimb] > >initialplot = ListPlot[rprev] > >Do[Do[rprevb[[(j+1)]][[2]]=iter[j]; Print[rprevb]; {j,0,jmax,1}]; > rprev=rprevb; Print["time = ",t]; Print["jmax = ",jmax ]; >ListPlot[rprev]; > jmax--,{t,1,10}] > >(*With every iteration of the outside loop, > jmax is decremented by one. As iterations progress to flatten the >curve, > the boundry moves. Should these two processes take place in this >way? > It appears this is not the ideal model, but it will work for >now.*) > > > > -- PGP Key ID: 0x235FDED1 Please avoid sending me Word or PowerPoint attachments. http://www.gnu.org/philosophy/no-word-attachments.html

**References**:**Modeling and Array Problem***From:*Sycamor@gmail.com