Search code examples
pythonfor-loopdifferential-equationscalculus

Adding Results to a List and Setting up a for-loop


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.


Solution

  • It sounds what you are looking for is a while loop. Like this:

    t = 0
    while t < 10:
        t = t + 1
        # more code here