I have a simple algorithm for computing the residuals of a mathematical model and some experimental data with noise. The objective is to find the phase if the amplitude and frequency are known. The algorithm loops over all phase values (to a precision of 3 decimal places) from 0 to 2*pi. The residuals from each calculation of the model for each phase are then calculated. The program then appends each phase and residual to their respective lists. I know I can use scipy.optimize to attack this problem, but I have reasons for wanting to use this algorithm. My question is, how can I retrieve the phase value associated with the smallest residual value? The program looks like this:
import numpy as np
from numpy import loadtxt
data = loadtxt('foo.txt', float)
x = data[:,0]
y = data[:,1]
a = 1.5
f = 0.01
p = []
phase = 0.000
residuals = []
for i in range(0, 6284):
p.append(phase)
model = a*np.sin(2*np.pi*f*x+phase)
res = sum((y-model)**2)
residuals.append(res)
phase += 0.001
print min(residuals)
Any help on how to retrieve the phase value associated with the minimum residual would be helpful. Thanks.
Use following code
p[residuals.index(min(residuals))]