# [SciPy-user] lstsq

Gregor Hagelueken gh50@st-andrews.ac...
Thu May 8 09:00:51 CDT 2008

```Hi,

I would like to use lstsq to fit a curve y using a simple linear
combination of three spectra (spectrum1-3 see below). Since the program
below always crashes, I played around with it and found that it does not
crash if I copy the contents of the array "spectrum2" to the array
"spectrum3" (using spectrum3[0:36]=spectrum2[0:36]). But as soon as I
change one of the numbers in array "spectrum3" it crashes again.

What is the mistake?

Thanks,
Gregor

from numpy import *
from numpy.random import normal
from pylab import *

t = arange(205, 241, 1.0)

#spectrum1
spectrum1=array([-25.35,-30.85,-34.6,-36.5,-36.75,-35.95,-35.2,-34.2,-33.6,-33.55,-33.8,-34.2,-34.2,-34.8,-35.4,-36.25,-36.6,-36.85,-36.55,-35.85,-35.05,-33.55,-31.7,-29.6,-27.15,-24.1,-21.5,-18.95,-16.4,-13.85,-11.45,-9.43,-7.76,-6.24,-4.81,-3.88])
#spectrum2
spectrum2=array([8.08,5.37,1.24,-3.07,-6.02,-8.84,-11.2,-13.05,-14.85,-17.75,-17.5,-18,-18.35,-18.65,-18.15,-17.45,-17.15,-16.3,-15,-13.15,-11.57,-9.76,-7.98,-5.79,-3.73,-2.37,-0.61,0.08,0.15,1.17,0.73,0.77,0.38,0.56,0.28,0.64])
#spectrum3
spectrum3=array([-25.8,-21.45,-18.0,-15.59,-12.82,-10.41,-8.41,-6.58,-4.99,-3.85,-2.62,-1.74,-1.02,-0.5,-0.13,0.17,0.03,-0.08,-0.32,-0.35,-0.48,-0.75,-1.12,-1.31,-1.5,-1.55,-1.17,-1.04,-0.72,-0.46,-0.14,0.21,0.01,0.23,0.03,-0.22])

#make curve y
y=0.8*spectrum1+0.1*spectrum2+0.2*spectrum3+ normal(0.0, 1.0, len(t))

from numpy.linalg import lstsq
Nparam = 3
A=zeros((len(t), Nparam),float)

#helix
A[:,0]=spectrum1
#beta
A[:,1]=spectrum2
#coil
A[:,2]=spectrum3

(p, residuals, rank, s) = lstsq(A,y)

#A=p*A
#fit = A[:,0]+A[:,1]#+A[:,2]
#plot (t, y)
#plot (t, fit)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/scipy-user/attachments/20080508/eb03eea5/attachment.html
```