0

I've a system that i need to use a graph to solve this function.

RLC Series - ODE equation

I'm trying to use tf.contrib.integrate.odeint(), however, this function can only get first order ODE, so I divided in two Differential equations. Here is what i did:

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import math as mat

graph = tf.Graph() 
with graph.as_default(): 
    R = tf.constant(100.)
    L = tf.constant(0.002)
    C = tf.constant(0.000005)
    E = tf.constant(10.)
    '''
    Inital EDO:
    dˆ2(vc)/dtˆ2 + R/L * dvc/dt + vc/LC = E/LC

    dvc/dt=z

    dvz/dt = (E-vc)/LC - R*z/L
    '''
    #dvz/dt = (E-vc)/LC - R*z/L
    EDO0 =  lambda z, t: (E-vc)/(L*C) - R/L * z

    #dvc/dt=z
    EDO1 = lambda vc, t: z

    #initial value
    EDO1_init = constant_op.constant(1.0, dtype=dtypes.float64)
    t = np.linspace(0.0, 1.0, 11)
    EDO1_solved = tf.contrib.integrate.odeint(EDO0, 0.5, t)

    with tf.Session() as sess:
      y_solved = sess.run(EDO1_solved)
      print(y_solved)
      tf.summary.FileWriter('/tmp/logs', tf.get_default_graph()).close()

But i'm having some problems with the equations

The main problem that i couldn't find the solution is that i must use tensorflow package.

brunoto
  • 91
  • 9
  • You need to solve coupled ODE as system, see the many similar questions on applying numerical integrators on second or higher order ODE. Why do you need the tensorflow package, why is scipy.integrate.odeint not sufficient? – Lutz Lehmann Sep 16 '18 at 16:31
  • Possible duplicate of [Need help solving a second order non-linear ODE in python](https://stackoverflow.com/questions/19779217/need-help-solving-a-second-order-non-linear-ode-in-python) – Lutz Lehmann Sep 16 '18 at 16:42

1 Answers1

0

@LutzL I'd to use tensorflow package because was a must of the professor.

So i found the answer with some friends help:

'''
Inital DOE:
vc'' + R/L * vc' + vc/LC = E/LC

Using state variables:
x1  = vc
x1' = vc'
x2  = vc' = x1'
x2' = vc''

       a       b       c
x2' = E/LC - R/L*x2 - 1/LC*x1
x1' = x2
y   = x1 

'''
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt

graph = tf.Graph() 
with graph.as_default():  
  E = 10.0   #Source Voltage
  R = 2.5    #Resistor
  L = 0.01   #inductor
  C = 0.001  #capacitor

  a = E / (L * C)
  b = R / L
  c = 1.0 / (L * C)

  x1 = tf.constant(0.0)
  x2 = tf.constant(0.0)

  t = np.linspace(0, 1.0, num=1000)

  def SecondOrderDev(state, t):
    x1, x2 = tf.unstack(state)    
    dx1 = x2
    dx2 = -c*x1 - b*x2 + f
    return tf.stack([dx1, dx2])

  tensor_state, tensor_info = tf.contrib.integrate.odeint(SecondOrderDev, [x1, x2], t, full_output=True)

  with tf.Session() as sess:
    state, info = sess.run([tensor_state, tensor_info])
    y, _ = state.T
    tf.summary.FileWriter('/tmp/logs', tf.get_default_graph()).close()

  plt.plot(t, y)

This is just if u are using colab and want to see the graph:

!wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
!unzip ngrok-stable-linux-amd64.zip

LOG_DIR = '/tmp/logs'
get_ipython().system_raw(
    'tensorboard --logdir {} --host 0.0.0.0 --port 6006 &'
    .format(LOG_DIR)
)
get_ipython().system_raw('./ngrok http 6006 &')

! curl -s http://localhost:4040/api/tunnels | python3 -c \
    "import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])" 
brunoto
  • 91
  • 9