Re: [Q] Programming & "Not Floating Point Number" Error

*Subject*: [mg3142] Re: [Q] Programming & "Not Floating Point Number" Error*From*: rknapp (Robert Knapp)*Date*: 7 Feb 1996 16:50:38 -0600*Approved*: usenet@wri.com*Distribution*: local*Newsgroups*: wri.mathgroup*Organization*: Wolfram Research, Inc.*Sender*: daemon at wri.com

Noel T. Doromal wrote: > > Hello out there! This is my first time on this group, so I apologize for > breaking any netiquette rules. > > I have a Mma question that I hope someone out there can help me with. I > recently started using Mma for a class and have had a hard time > "programming" with it. Here is the script/program that i wrote: > > -- > epsilon = .01 > gamma = 7 > dt = .125 > stim = 0 > > y0 = -.5 > w0 = 0.0 > > For[i=0, i<50, i++, > f = y0 * (1 - y0 *y0); > v = y0 + (f - w0) * dt; > w = w0 +(epsilon*(gamma*y0 - w0)) * dt; > vee[i] = v; > doubleu[i] = w; > y0 = v; > w0 = w; > ] > > ListPlot[{vee,doubleu}] > --- > > First question: Are there examples of Mma programs out there that are > longer than a line or two? (Or was mathematica not really made for > programming?) I bought the book and can't find any in there. (It > describes the control structures: do, if, for... but then gives one line > examples only) > > Second: In the script above I'm essentially calculating a bunch of points > that I want to plot (V vs. W - I had to use "vee" and "doubleu" to get it > to work). The only way I could figure to do it was to store the numbers > in a list and then use ListPlot[] but I can't get it right. I keep > getting: > > -- > Graphics::gptn: > Coordinate vee in {1, vee} is not a floating-point number. > Graphics::gptn: > Coordinate doubleu in {2, doubleu} is not a floating-point number. > -- > > What am I doing wrong? > The basic thing you are doing wrong is that you are not making a list--you are making vee and doubleu as subscripted variables, which can alternatively be viewed as functions with values at 0, 1, 2, etc. The for loop you set up is fine--all you need to do to see your result is to make a List. The easiest way is with Table. If you replace your ListPlot command with ListPlot[Table[{vee[i],doubleu[i]},{i,0,49}]] you'll get the plot you desire. However, there are much easier ways to achieve what you desire with Mathematica. For example, assuming that your constants epsilon, gamma, and dt are defined the same, y0 = -.5; w0 = 0.0; ListPlot[ Table[ ytmp = y; f = y0 * (1 - y0 *y0); v = y0 + (f - w0) * dt; w = w0 +(epsilon*(gamma*y0 - w0)) * dt; {y0,w0} = {v,w}, {i,0,49}]]; gives exactly the same thing. If your goal is simply to get a numerical approximation to the solution of the differential equation, the easiest way (and far more accurate and fast that Euler's method) is Clear[v,w]; NDSolve[{ v'[t] == v[t]*(1-v[t]^2) - w[t], v[0] == -.5, w'[t] == epsilon*(gamma*v[t] -w[t]), w[0] == 0.}, {v[t],w[t]},{t,0,50*.125}] the solution can tehn be plotted by: ParametricPlot[Evaluate[{v[t],w[t]} /. %],{t,0,50*.125}]; My guess is this might not help you much because your assignement may have been to write a program to compute the solution using Euler's method? Well, here is where the power of Mathematica comes in--you can easily write a program which will compute the solution of any equation using Euler's method: Eulers[f_,init_,vars_,{t_,tmin_,tmax_,dt_}] := Module[{vals = init, time}, Table[vals = N[vals + dt* (f /. {t->time,vars[[1]]->vals[[1]],vars[[2]]->vals[[2]]})], {time,tmin,tmax,dt}]] f is the lhs of the ODE, init are the initial values, vars are the dependent variables, t is the time variables which is to go between tmin and tmax in steps of dt. With this definition, Clear[v,w]; ListPlot[ Eulers[{v*(1-v^2)-w,epsilon*(gamma*v-w)}, {-.5,0.},{v,w},{t,0,6.25,.125}]]; Gives the same plot as you could get via your For loop. You can try any system, however. (This is a crude definition because the is no error checking) To make it even more general, you don't need to restrict the number of dependent variables to two: the following definition will take a system of any numebr of equations: Eulers[f_,init_,vars_,{t_,tmin_,tmax_,dt_}] := Module[{vals = init, time}, Table[vals = N[vals + dt*(f /. Join[{t->time},Thread[vars->vals]])], {time,tmin,tmax,dt}]] then, for example, Eulers[{x},{1},{x},{t,0,1,.1}] Produces a list of values for the scalar equation x' = x. {{1.1}, {1.21}, {1.331}, {1.4641}, {1.61051}, {1.77156}, {1.94872}, {2.14359}, {2.35795}, {2.59374}, {2.85312}} (Note that this list can be made a simple list of numbers using Flatten) -- Rob Knapp Wolfram Research, Inc. http://www.wri.com/~rknapp