# How to make simple iteration in Mathematica

How to make simple iteration in Mathematica for this three equations and save $q$ for each step. If we define $q'=dq/dx$, $q''=dq'/dx$ we have two equations

(1) $q''[j+1]=a_1(q[j+1]-q[j])-a_3q'[j]-a_5q''[j]$,

(2) $q'[j+1]=a_2(q[j+1]-q[j])+a_4q'[j]+a_6q''[j]$,

And we should substitute in third

(3) $q[j+1]=Q_2q[j]+Q_3q'[j]+Q_4q''[j]$

$a_i$ and $Q_k$ ($i=1,\ldots,6$; $k=1,2,3$) are functions of $\delta(x)$. Where $\delta(x)=0.01$ for example. For each $\delta(x)$ I need $q$.

Regards,

• Should this really be here, and not a programming forum? – GEdgar Aug 29 '11 at 20:56
• As it stands, it's unanswerable since you didn't say what the ai and Qk are. Are you trying to do Runge-Kutta by any chance? – J. M. is a poor mathematician Aug 30 '11 at 2:13
• Could you write the original differential equations? – tlehman Aug 30 '11 at 14:13
• GEdgar, yes it should be here, because it is a numerical method for solving differential equation – derdack Aug 30 '11 at 16:00
• J.M. I wrote that ai and Qk are the function of the x I mean (delta x). This is not a Runge Kutta. habitmelon original differential equation is R1*q''(x)+R2*q'[x]+R3*q[x]=F(x), Ri contants, but I dont want another method for solving. Just help about programing in mathematica for simple iteration which I wrote. – derdack Aug 30 '11 at 16:17

You can just use a For loop, following the manual. Equation 3 has no [j+1] on the right, so should be calculated first. Then equations 1 and 2 can be calculated as you already have q[j+1].

For[j=0,j<end,j++,
calculate Q's
qold=q
q=
qp=  (using qold for q[j])
qpp= (using qold for q[j])
Print[j,q,qp,qpp]

• Mathematica also supports so-called "dynamic programming": e.g. f = 1; f[n] := f[n] = n f[n-1] for the factorial, which I believe the OP wanted to do for his particular system. Still, the question is less than clear to me... – J. M. is a poor mathematician Aug 30 '11 at 13:22
• @J.M.: That should read f = 1; f[n_Integer?Positive] := f[n] = n f[n-1] – Simon Aug 30 '11 at 22:18
• @Simon: Yes, I forgot the _` (and the head-checking for guaranteed correctness); thanks! – J. M. is a poor mathematician Aug 30 '11 at 22:20
• Dear Ross Millikan how can I set the step of delta{x}, because for example I have a2=1/delta{x}. I need from [0,1] for each delta{x} with step 0.01 value of q. Regards! – derdack Aug 31 '11 at 14:33
• Usually your steps are in x, not q. You choose delta x, say as .01, or choose 1000 steps in [0,1] and find delta x is .001. Then the steps in q are calculated. You start with initial conditions q(0), q'(0). Usually your iteration starts with q'' as a function of q, q' and x. Then you step up to q'(delta x) and q(delta x). Then you use your equation to get q''(2 delta x), q'(2 delta x), q(2 delta x), etc. – Ross Millikan Aug 31 '11 at 15:34