Python Forum
Differential equations with initial condition in Python (change a working code)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Differential equations with initial condition in Python (change a working code)
#1
I am pretty new in Python. I have a working code which looks like

import numpy as np
import scipy.integrate as sp
import sys
g, l, k = 9.81, 10, float(input())
PTS = 6
def rhs( y, t, g, l, k ): return [ k*t/l * np.cos(y[1]) - g/l * np.sin(y[1]),y[0] ]
y0 = [ 0, 0 ]
t = np.linspace( 0, 6, PTS+1 )
y = sp.odeint( rhs, y0, t, args = (g, l, k) )
for i in range(PTS): print( t[i], np.arctan(k*t[i]/g) *180/np.pi )
where you have to write a value 0.5 (it is value of "k"). The output of this code is:

time value of y
0.0 0.0
1.0 2.91774937823
2.0 5.82044363971
3.0 8.69348861202
4.0 11.5231772897
5.0 14.297051396

Now I want to use a value in time t = 5 (y = 14.297051396) as my initial condition and find values of y in time t = 6, ..., 16 for k=1.1. How should I change my code?

I tried this code

import numpy as np
   import scipy.integrate as sp
   import sys
   g, l, k = 9.81, 10, float(input())
   PTS = 12
   def rhs( y, t, g, l, k ): return [ k*t/l * np.cos(y[1]) - g/l * np.sin(y[1]), y[0] ]
   y0 = [ 14.297051396, 5 ]
   t = np.linspace( 5, 17, PTS+1 )
   y = sp.odeint( rhs, y0, t, args = (g, l, k) )
   for i in range(PTS): print( t[i], np.arctan(k*t[i]/g) *180/np.pi )
and write 1.1, but the output is the same as in the case that I write y0 = [ 0, 0 ].

What did I do wrong? Could somebody explain to me, how to change it? (I am self-learner in Python and I do not study programming).
Reply
#2
I tried to write it as a code for t=0:1:12 (and hence my condition is in time t=0), but it also did not work....

import numpy as np
import scipy.integrate as sp
import sys
g, l, k = 9.81, 10, float(input())
PTS = 12
def rhs( y, t, g, l, k ): return [ k*t/l * np.cos(y[1]) - g/l * np.sin(y[1]),
y[0] ]
y0 = [14.297051396, 0 ]
t = np.linspace( 0, 12, PTS+1 )
y = sp.odeint( rhs, y0, t, args = (g, l, k) )
for i in range(PTS): print( t[i], np.arctan(k*t[i]/g) *180/np.pi )
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 690 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  New to Python - Not sure why this code isn't working - Any help appreciated TheGreatNinx 4 909 Jul-22-2023, 10:21 PM
Last Post: Pedroski55
  code not working when executed from flask app ThomasDC 1 836 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  New to python/coding Need help on Understanding why this code isn't working. Thanks! mat3372 8 1,664 May-09-2023, 08:47 AM
Last Post: buran
  I am new to python and Could someone please explain how this below code is working? kartheekdas 2 977 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
  ANSI not working for change of text colors BliepMonster 10 3,249 Nov-10-2022, 09:28 AM
Last Post: BliepMonster
Exclamation My code is not working as I expected and I don't know why! Marinho 4 1,031 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  Changing the initial worksheet name in an MS Excel file azizrasul 3 908 Oct-02-2022, 07:56 PM
Last Post: azizrasul
  How to move multiple columns to initial position SriRajesh 4 1,375 Jul-02-2022, 10:34 AM
Last Post: deanhystad
  My Code isn't working... End3r 4 1,865 Mar-21-2022, 10:12 AM
Last Post: End3r

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020