naughty_waves
naughty_waves

Reputation: 275

How to sum while iterating through an exponential equation in Python?

Real and imaginary parts of any complex function are related by the Kramer-Kronig relations (KKR). I am trying to use KKR modified for a logarithmic scale

enter image description here

where X_R is the real part of the modulus, X_{R_{0}} is the real modulus at some reference frequency f_0, e is Euler's number, and L is the number of frequencies f_i corresponding to the number of phase angles \phi_i.

However, I am unable since I am not quite sure how to sum an exponential equation in Python. I tried the following

import math
import numpy as np

f_exp  = np.array([.1, .2, .3, .4, .5, .6, .7, .8, 1, 2, 3, 4, 5, 6, 7, 8, 10, 20, 30, 40, 100])

x_exp  = np.array([2.69672131, 2.703278689, 2.71311475, 2.718032787, 2.721311475, 2.732240437, 2.743715847, 2.749726776, 2.773224044, 2.800546448,
                   2.81147541, 2.813114754, 2.81420765, 2.810928962, 2.810382514, 2.808196721, 2.808743169, 2.804371585, 2.807650273, 2.809289617, 
                   2.81256830])

qx_exp = np.array([0.004198813, 0.010331355, 0.017057369, 0.021656775, 0.023832839, 0.026849654, 0.027492582, 0.027987141, 0.025365974, 0.01418892, 
                   0.008204748, 0.006918892, 0.004990109, 0.003803165, 0.004050445, 0.003654797, 0.002912957, 0.002616222, 0.002319486, 0.00202275, 
                   0.001577646])

phases = [np.rad2deg(np.arctan(qx)) for qx in qx_exp]

def KKR(X_R0, frequency, phis):
    X_R = []
    for idx, i in enumerate(frequency):
        X_R[idx] = X_R0 * np.exp((1/(math.pi*math.log10(math.e))*np.sum(phis[idx+1]+phis[idx]*np.log10(frequency[idx+1]/frequency[idx]))))
    return X_R

kkr = KKR(x_exp[0], f_exp, phases)

where f_exp is frequency, x_exp is the real modulus, qx_exp is inverse Quality-factor, and phases are the phase angles but it obviously did not work.

What am I doing wrong? How do I sum while iterating through an exponential equation?

Upvotes: 1

Views: 169

Answers (2)

naughty_waves
naughty_waves

Reputation: 275

After trial and error, and with the help of @pythonic833, who helped me understand how to sum a series, I came up with the solution to my problem.

def KKR(modulus, frequency, phase):
    X_R0      = modulus[0]
    f         = frequency
    phi       = phase
    phi_sum   = [phi[i+1]+phi[i] for i in range(len(phi)-1)]
    f_div     = [f[i+1]/f[i] for i in range(len(f)-1)]
    log_f_div = np.log10(f_div)
    arg       = phi_sum*log_f_div
    series    = np.array([sum(arg[:i+1]) for i in range(len(arg))])
    exponent  = 1/(math.pi*math.log10(math.e)) * series
    X_R       = X_R0 * np.exp(exponent)
    X_R       = np.insert(X_R, 0, X_R0)
    return X_R

Thank you again, @pythonic833!

Upvotes: 0

pythonic833
pythonic833

Reputation: 3224

You are summing X_R0**exp1 + X_R0**exp2 and so on instead of summing the exponents first and the exponenanting:

import math
import numpy as np

f_exp  = np.array([.1, .2, .3, .4, .5, .6, .7, .8, 1, 2, 3, 4, 5, 6, 7, 8, 10, 20, 30, 40, 100])

x_exp  = np.array([2.69672131, 2.703278689, 2.71311475, 2.718032787, 2.721311475, 2.732240437, 2.743715847, 2.749726776, 2.773224044, 2.800546448,
                   2.81147541, 2.813114754, 2.81420765, 2.810928962, 2.810382514, 2.808196721, 2.808743169, 2.804371585, 2.807650273, 2.809289617, 
                   2.81256830])

qx_exp = np.array([0.004198813, 0.010331355, 0.017057369, 0.021656775, 0.023832839, 0.026849654, 0.027492582, 0.027987141, 0.025365974, 0.01418892, 
                   0.008204748, 0.006918892, 0.004990109, 0.003803165, 0.004050445, 0.003654797, 0.002912957, 0.002616222, 0.002319486, 0.00202275, 
                   0.001577646])

phases = [np.rad2deg(np.arctan(qx)) for qx in qx_exp]

def KKR(X_R0, frequency, phis):
    X_R = []
    exponent = 0
    prefactor =  1/(math.pi*math.log(math.e))
    for idx in range(len(frequency)-1): # calculate sum of exponents
        exponent += prefactor*np.sum(phis[idx+1]+phis[idx]*np.log(frequency[idx+1]/frequency[idx]))
    X_R = X_R0 ** exponent # apply the resulting exponent
    return X_R 

kkr = KKR(x_exp[0], f_exp, phases)

Upvotes: 1

Related Questions