1

I have the following program, it seems that the amp and period at the end print out a list of list(see below). And I am unable to plot them (I want to plot period against amp)

I have tried methods in How to make a flat list out of list of lists? to combine the output of amp and period so that they are plot-table, but nothing worked.

import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp

def derivatives(t,y,q,F):
    return [y[1], -np.sin(y[0])-q*y[1]+F*np.sin((2/3)*t)]

t = np.linspace(0.0, 100, 10000)

#initial conditions
theta0 = np.linspace(0.0,np.pi,100)
q = 0.0       #alpha / (mass*g), resistive term
F = 0.0       #G*np.sin(2*t/3)

for i in range (0,100):
    sol = solve_ivp(derivatives, (0.0,100.0), (theta0[i], 0.0), method = 'RK45', t_eval = t,args = (q,F))

    velocity = sol.y[1]
    time = sol.t

    zero_cross = 0
    value = []
    amp = []
    period = []

    for k in range (len(velocity)-1):
        if (velocity[k+1]*velocity[k]) < 0:
            zero_cross += 1
            value.append(k)
        else:
            zero_cross += 0

    zero_cross = zero_cross - zero_cross % 2         # makes the total number of zero-crossings even

    if zero_cross != 0:
        amp.append(theta0[i])
      # period calculated using the time evolved between the first and last zero-crossing detected
        period.append((2*(time[value[zero_cross - 1]] - time[value[0]])) / (zero_cross -1))

If I print out amp inside the loop, it displays as follows:

[0.03173325912716963]
[0.06346651825433926]
[0.0951997773815089]
[0.12693303650867852]
[0.15866629563584814]
[0.1903995547630178]
[0.2221328138901874]
[0.25386607301735703]
[0.28559933214452665]
[0.3173325912716963]
[0.3490658503988659]
[0.3807991095260356]
[0.4125323686532052]
[0.4442656277803748]
[0.47599888690754444]
[0.5077321460347141]
[0.5394654051618837]
[0.5711986642890533]
[0.6029319234162229]
[0.6346651825433925]
[0.6663984416705622]
[0.6981317007977318]
[0.7298649599249014]
[0.7615982190520711]
[0.7933314781792408]
[0.8250647373064104]
[0.85679799643358]
[0.8885312555607496]
[0.9202645146879193]
[0.9519977738150889]
[0.9837310329422585]
[1.0154642920694281]
[1.0471975511965979]
[1.0789308103237674]
[1.110664069450937]
[1.1423973285781066]
[1.1741305877052763]
[1.2058638468324459]
[1.2375971059596156]
[1.269330365086785]
[1.3010636242139548]
[1.3327968833411243]
[1.364530142468294]
[1.3962634015954636]
[1.4279966607226333]
[1.4597299198498028]
[1.4914631789769726]
[1.5231964381041423]
[1.5549296972313118]
[1.5866629563584815]
[1.618396215485651]
[1.6501294746128208]
[1.6818627337399903]
[1.71359599286716]
[1.7453292519943295]
[1.7770625111214993]
[1.8087957702486688]
[1.8405290293758385]
[1.872262288503008]
[1.9039955476301778]
[1.9357288067573473]
[1.967462065884517]
[1.9991953250116865]
[2.0309285841388562]
[2.0626618432660258]
[2.0943951023931957]
[2.126128361520365]
[2.1578616206475347]
[2.1895948797747042]
[2.221328138901874]
[2.2530613980290437]
[2.284794657156213]
[2.3165279162833827]
[2.3482611754105527]
[2.379994434537722]
[2.4117276936648917]
[2.443460952792061]
[2.475194211919231]
[2.5069274710464007]
[2.53866073017357]
[2.57039398930074]
[2.6021272484279097]
[2.633860507555079]
[2.6655937666822487]
[2.6973270258094186]
[2.729060284936588]
[2.7607935440637577]
[2.792526803190927]
[2.824260062318097]
[2.8559933214452666]
[2.887726580572436]
[2.9194598396996057]
[2.9511930988267756]
[2.982926357953945]
[3.0146596170811146]
[3.141592653589793]
[Finished in 3.822s]

I am not sure what type of output that is and how to handle, any help would be appreciated!

Hiroi
  • 15
  • 2
  • Also, if I plot period against amp inside the loop now it shows a number of plots with one point each; if I plot outside the loop there is only one plot with one point in it, why is that the case? Thank you. – Hiroi Aug 15 '20 at 20:04

1 Answers1

0

You are declaring the lists inside the loop, which means they will be reset to empty at every iteration. Consider declaring amp, period, and any array that should be set to empty only once (as initial state) before the loop, like so:

#initialize arrays, executes only once before the loop
amp = []
period = []


for i in range (0,100):
    #your logic here, plus appending values to `amp` and `period`

#now `amp` and `period` should contain all desired values
Anis R.
  • 6,656
  • 2
  • 15
  • 37