# iterative programming

i have 3 equations with 3 variables and two constants(to be substituted repeatedly). the value of the variables we got by applying nsolve on the system of 3 equations assuming some initial value for contants. after getting these unknown we evaluate the value of constants with the help of two equations. Then substituting these recent value of constants in the equations and calculate the new set of values for the unknown.i have to do this repeteadly . Equations are like

 x[i + 1] - x[i] - Cos[z[i]] - Cos[z[i + 1]] - ax[i] == 0 &&    (*1*)
y[i + 1] - y[i] - Sin[z[i]] - Sin[z[i + 1]] - ay[i] == 0 &&    (*2*)
x[i + 1]^2 + y[i + 1]^2 - 1 == 0                               (*3*)


We solve for {x[i + 1], y[i + 1], z[i + 1]} if know the initial values for x,y,z,ax and ay then we have another set of equations to calculate ax and ay i.e

 ax[i + 1] = Cos[z[i + 1]] - Cos[z[i]] + ax[i]                  (*4*)
ay[i + 1] = Sin[z[i + 1]] - Sin[z[i]] + ay[i]                  (*5*)


and resubstitute these new value of ax and ay in equation 1,2,3 and solve it again for x,y,z then again solve 4,5 and substitute new ax and ay like wise i have to do repeatedly. How to do this in mathematica. eqn 1,2,3 are solved by NSolve .

• Hi and welcome to Mathematica.SE! Your question is likely to be better received if you format your code and show what you have already tried. Apr 13 '14 at 12:57

For this task, you can use the function NestList. It takes a function as its first argument, and a starting list of parameters as the second argument. Here I define the function that corresponds to the iteration in your question:

step[{x_, y_, z_, ax_, ay_}] := Module[
{xNew, yNew, zNew, axNew, ayNew},
{xNew, yNew, zNew} = {xNew, yNew, zNew} /. First@Quiet@NSolve[
xNew - x - Cos[z] - Cos[zNew] - ax == 0 &&
yNew - y - Sin[z] - Sin[zNew] - ay == 0 &&
xNew^2 + yNew^2 - 1 == 0, {xNew, yNew, zNew}];
axNew = Cos[zNew] - Cos[z] + ax; ayNew = Sin[zNew] - Sin[z] + ay;
{xNew, yNew, zNew, axNew, ayNew}]


The arguments are passed to the function step as one list. I don't need any index i in this calculation, so I'm naming the new values of the parameters by appending New to their symbol. The function step returns a list of the same form as it received in the argument. But the return values are the new values of the parameters. Since the NSolve in your equation set produces non-unique solutions with corresponding warnings, I use Quiet and First to get only one of the possible numerical solutions without printing the warning.

What NestList does is to apply this function to the list of starting values, which I choose randomly here. Then it repeats this step a number of times, determined by the third argument (in this case, 4):

NestList[step, {0.1, 0.2, 0.1, 0.2, 0.1}, 4]

(*
==> {{0.1, 0.2, 0.1, 0.2,
0.1}, {0.864444, -0.502729, -2.01591, -1.22556, -0.902395}, \
{-0.39584 + 0.660797 I, -1.15384 - 0.226694 I,
1.24032 - 0.65152 I, -0.399164 + 0.660797 I,
1.15401 - 0.226694 I}, {-0.79509 + 0.328052 I, -0.768528 -
0.33939 I, -1.7739 - 1.29526 I, -1.19093 - 1.65434 I, -1.92237 +
0.340693 I}, {-1.22282 - 3.61537 I, -3.73821 + 1.18264 I,
0.64674 + 0.918611 I, 0.364119 - 0.634746 I, 0.875389 + 0.840642 I}}
*)


The output is the list of parameter sets, starting with the initial one, and continuing over the four iterations.

• NestList is generating the result correctly now i just wanted to Plot the x Vs y graph....so how to extract the values of x and y from the above and plot the graph . I wrote this in matrix form. and able to store x value in one list and y value in another list but this does not solve the purpose as i have to plot the y value corresponding to that x. listplot will be used it is clear to me. Apr 15 '14 at 11:40
• @user13674That's exactly how I'd do it (had I remembered to reply earlier...)
– Jens
Apr 16 '14 at 15:22