I have a general question about the "do...until" iteration until a condition is met.
Let's say that I have a variable x
, y
and z
, where:
x0 = function(y0)
I ran the code, and from the function, I got a value x0
from an inputted variable value y0
.
With the value of x0
, now I have to update y0
and get a new value of y1=function(x0)
.
The process is repeated in the next step, and now I should get:
x1 = function(y1)
and again, from x1
, to update y1
and get a new value of y2=function(x1)
.
The iteration should stop when the condition of x(i-1) - x(i) =~ 0
is met,
and after that, it should continue with a new calculation, which will have:
z = function(y(i))
I'm new to these "do...until" iterations, and my main problem is setting up the condition.
Here is a picture of the results of the example that I solved by running the code a few times manually iterating:
I didn't post the code for the functions because they are long. I really appreciate any help you can provide.
CodePudding user response:
This works just like the "do ... until" you described:
while not [condition]:
...
so you can write either something like this:
y = y0
x = function(y)
last_x = -1
while not (-.0001 <= last_x - x <= .0001):
y = function(x)
last_x = x
x = function(y)
print(x, y, last_x)
or (if you want to keep track of all your x's and y's):
y = [y0]
x = []
while len(x) <= 1 or not (-.0001 <= x[-2] - x[-1] <= .0001):
x.append(function(y[-1]))
y.append(function(x[-1]))