title: Python code to find minimum distance between points and a curve
body: <p>I have some data and I have plotted magnitude against wavelength (the blue points). I then have some code that reads a model stellar population from a file, and plots this on the same graph (the pink line). In this code, there is a scale that can be adjusted that moves this line up or down on the graph. So far I have been changing the scale so that the line is as close as I can tell by eye to my points, but I would like to write some code that would calculate the value of the scale for which the total distance between my points and the line is a minimum. This is my code so far:</p>
  
  <pre><code>#Import modules
  
  from math import *
  import numpy as np
  import matplotlib.pyplot as plt
  
  # Specify data
  
  wavelength = 
  np.array([357.389,445.832,472.355,547.783,620.246,752.243,891.252,2164.089])
  magnitude = 
  np.array([24.0394,23.1925,23.1642,22.4794,21.7496,20.9047,20.4671,19.427])
  
  # Create Graph
  
  #plt.scatter(wavelength, magnitude)
  #plt.ylim([25,18])
  #plt.xlim([300,2200])
  #plt.xlabel('wavelength (nm)')
  #plt.ylabel('magnitude')
  #plt.title('object 1')
  #plt.show()
  #plt.close()
  
  #now - here is some code that reads a model stellar population model from a 
  file
  
  lines = open('fig7b.dat').readlines()
  
  wavelengths, luminosities = [],[]
  
  for l in lines:
      s = l.split()
      wl = s[0]
      old = s[-1]
      if '#' not in wl:
          wavelengths.append(float(wl)) #wavelength in angstroms 
          luminosities.append(float(old)) #luminosities are in log units!
  
  
  scale = 3.5
  c=3.e8 
  wavelengths = np.array(wavelengths)
  nus = c/(wavelengths*1.e-10) 
  luminosities = np.array(luminosities) + scale 
  
  luminosity_density = np.log10(((10**luminosities)*wavelengths)/nus)
  
  #plt.plot(wavelengths,luminosity_density)
  #z = 1.0
  #plt.plot(wavelengths*(1+z),luminosity_density,color='r')
  
  #plt.axis([900, 10000, 25,31])
  #plt.savefig('sed.png')
  #plt.show()
  #plt.close()
  
  Mpc_to_cm = 3.086e24 #convert Mpc to cm 
  z  = 0.3448 #our chosen redshift
  D_L = 1841.7 * Mpc_to_cm 
  
  #remember luminosity_density is logged at the moment
  flux_density = (10**luminosity_density) * (1+z) / (4*pi*D_L**2) #units will 
  be erg/s/cm^2/Hz
  
  #now turn that into an AB magnitude - goes back to log
  AB_mag = -2.5*np.log10(flux_density) - 48.6
  
  #try plotting your photometry on here and play with z and D_L 
  plt.plot(wavelengths*(1+z),AB_mag,color='pink')
  plt.scatter(wavelength*10., magnitude,color='cornflowerblue')
  plt.axis([900, 25000, 30,18])
  plt.xlabel('wavelength')
  plt.ylabel('magnitude')
  plt.title('object 1')
  plt.savefig('sed_ab.png')
  plt.show()
  </code></pre>
  
  <p>which gives a graph that looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/3Hu6w.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3Hu6w.jpg" alt="enter image description here"></a></p>
  
  <p>Also it would be helpful to print the best scale value.
  I'm very new to python and programming in general and the pink line isn't a simple equation (in the file I was given it is made up of a lot of data points) so I have been getting a bit stuck. Apologies if I am not using the correct language to describe my problem, and for the long code - a lot of the comments were previous plots my supervisor has kept from before when I had separate plots. (I am using python 2.7)</p>
  
  <p>A link to fig7b.dat: <a href="https://drive.google.com/open?id=0B_tOncLLEAYsbG8wcHJMYVowOXc" rel="nofollow noreferrer">https://drive.google.com/open?id=0B_tOncLLEAYsbG8wcHJMYVowOXc</a></p>