Reputation: 61
For a school assignment, I am coding an algorithm that computes slopes using the Euler method. In part of it, I have a for loop that will repeat a pattern of equations until the end result is met. What I need is a way to add each result to a list and then afterwards plot each item in the list. I also need some help setting up the for-loop if possible.
Here's what I have, keeping in mind I am very inexperienced at coding (please bear with me)
stpsz = input ("Define "h" Value (step size)")
tfinal = input ("Define Final "T" Value (stopping place)")
xnow = input ("Define "X0" as a Starting Value")
t = 0
while t > tfinal
t = t + 1
slopenow = (xnow * (1-xnow))
#Save slopenow (as a number, not variable) to a list of slopes ( I don't understand how to make a list like this)
#Save xnow (as a number, not variable) to a list of xs)
xnext = (slopenow * stpsz)+(xnow)
xnext = x now
#repeat while-loop until tfinal is reached)
I greatly appreciate whatever help you all can give.
Upvotes: 2
Views: 89
Reputation: 1269
Here you have an recursive approach for this equation, to give you an idea what I meant in my comment.
class Slope:
def __init__(self, timestamp, slope):
self.timestamp = timestamp
self.slope = slope
def find_slope(slopes, slope, step_size, until):
if slope.timestamp > until:
return slopes
current_y = slope.slope + step_size * slope.slope
slope = Slope(slope.timestamp + 1, current_y)
slopes.append(slope)
return find_slope(slopes, slope, step_size, until)
if __name__=="__main__":
initial_slope = Slope(0, 1)
for current in find_slope([], initial_slope, 1, 3):
print("slope: {slope}, timestamp: {timestamp}".format(**current.__dict__))
But there multiple ways to solve this problem e.g. with a while or a for loop. I also have to admit you could write a shorter version but I think the verbosity helps you better understand.
Edit
Your eyes should focus on that function...
def find_slope(slopes, slope, step_size, until):
if slope.timestamp > until:
return slopes
current_y = slope.slope + step_size * slope.slope
slope = Slope(slope.timestamp + 1, current_y)
slopes.append(slope)
return find_slope(slopes, slope, step_size, until)
This is a recursive call or simpler a function that calls itself as long as a certain point is reached here it is if slope.timestamp > until
. The first call is with my initial_slope
(step_size
and until
are just constants).
The line current_y = slope.slope + step_size * slope.slope
calculates the new slope value. Then I create a slope instance with the new slope value and updated time and add it to a list.
The slope instance, the slope list and the constants are passed into the next step with a self call of the function return find_slope(slopes, slope, step_size, until)
. The return is needed to not only go down the ladder and collect new slopes but also return back to the start so the caller can receive it.
You can call the recursive function with
slopes = find_slope([], initial_slope, 1, 3)
and get a list of slopes back. I initialized it with an empty list that will be filled with slopes and later be returned from this function.
Upvotes: 1
Reputation: 5195
It sounds what you are looking for is a while
loop. Like this:
t = 0
while t < 10:
t = t + 1
# more code here
Upvotes: 2