1

I am trying to calculate g(x_(i+2)) from the value g(x_(i+1)) and g(x_i), i is an integer, assuming I(x) and s(x) are Gaussian function. If we know x_i = 100, then the summation from 0 to 100, I don't know how to handle g(x_i) with the subscript in python, knowing the first and second value, we can find the third value, after n cycle, we can find the nth value.

Equation: enter image description here

code:

import numpy as np
from matplotlib import pyplot as p
from math import pi

def f_s(x, mu_s, sig_s):
    ss = -np.power(x - mu_s, 2) / (2 * np.power(sig_s, 2))
    return np.exp(ss) / (np.power(2 * pi, 2) * sig_s)

def f_i(x, mu_i, sig_i):
    ii = -np.power(x - mu_i, 2) / (2 * np.power(sig_i, 2))
    return np.exp(ii) / (np.power(2 * pi, 2) * sig_i)

# problems occur in this part
def g(x, m, mu_s, sig_s, mu_i, sig_i):
    for i in range(1, m):                 # specify the number x, x_1, x_2, x_3 ......X_m
        h = (x[i + 1] - x[i]) / e
        for n in range(0, x[i]):          # calculate summation
            sum_f = (f_i(x[i], mu_i, sig_i) - f_s(x[i] - n, mu_s, sig_s) * g_x[n]) * np.conj(f_s(n + 
x[i], mu_s, sig_s))
        g_x[1] = 1       # initial value
        g_x[2] = 5
        g_x[i + 2] = h * sum_f + 2 * g_x[i + 1] - g_x[i]

    return g_x[i + 2]


x = np.linspace(-10, 10, 10000)
e = 1
d = 0.01
m = 1000
mu_s = 2
sig_s = 1
mu_i = 1
sig_i = 1

p.plot(x, g(x, m, mu_s, sig_s, mu_i, sig_i))
p.legend()
p.show()

result: I(x) and s(x) enter image description here

  • I don't think this is a duplicate, and writing your own implementation is a rewarding experience. But in case you want to try a library sometime: https://stackoverflow.com/questions/18991408/python-finite-difference-functions – bad_coder Feb 02 '20 at 00:37

0 Answers0