# [Numpy-discussion] solving matrix

Sudheer Joseph sudheer.joseph@yahoo....
Sat Aug 10 10:26:55 CDT 2013

```I thought I should add the error I get too.
From: Sudheer Joseph <sudheer.joseph@yahoo.com>

To: "numpy-discussion@scipy.org" <numpy-discussion@scipy.org>
>Sent: Saturday, 10 August 2013 8:40 PM
>Subject: [Numpy-discussion] solving matrix
>
>
>
>Dear Experts,
>I am trying to write the below piece of matlab code to a python script. The objective of the code is to find a fit of annual harmonic to a time series and then remove it from the time series. It uses matlabs E\ts to find the coefficients of harmonic fit, I used linlag.lstsq equivalent in python but feel the result is not correct. Can any one advice on this please.
>
>
>##########Matlab CODE################
>
>function[an_fit,sam_fit,fit_both]=calcharm(t,ts,dt)
>subplot 211
>plot(t,ts)
>legend('Orig Tser')
>xlabel('TIME')
>title('ORIGINAL TIMESERIES')
>figure(gcf)
>disp('Paused...');pause
>%% Prepare Annual signal
>fann = 2*pi/(365.25/dt);
>E = [ones(size(t)) cos(fann*t) sin(fann*t)];
>a = E\ts;
>%dfit = a(1) + a(2)*cos(fann*t) + a(3)*sin(fann*t);
>% % or more easily:
>an_fit = E*a;
>
>#################PYTHON CODE ##################
>import numpy as np
>from numpy import pi,c_,ones,size,cos,sin
>t=np.arange(1,365.25*5)
>dt=np.average(np.diff(t))
>acyc = 2*pi/(365.25/dt)
>
>
>365.25/4)/dt)
>E1=c_[ones(size(t)),cos(acyc*t),sin(acyc*t)]
>linalg.lstsq(E1,ts)
>an_fit = E*a
>
>###############ERROR##################
>ts.shape
>Out[243]: (1828, 1)
>E1.shape
>Out[244]: (1826, 3)
>linalg.lstsq(E1,ss)
>---------------------------------------------------------------------------
>LinAlgError                               Traceback (most recent call last)
>/home/sjo/work/PY_WORK/PY_TSER/<ipython-input-245-2eb3822d2564> in <module>()
>----> 1 linalg.lstsq(E1,ss)
>
>/usr/local/lib/python2.7/dist-packages/numpy-1.7.0-py2.7-linux-x86_64.egg/numpy/linalg/linalg.pyc in lstsq(a, b, rcond)
>   1801     ldb = max(n, m)
>   1802     if m != b.shape[0]:
>-> 1803         raise LinAlgError('Incompatible dimensions')
>   1804     t, result_t = _commonType(a, b)
>   1805     result_real_t = _realType(result_t)
>
>LinAlgError: Incompatible dimensions
>
>
>with best regards,
>Sudheer
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20130810/33504034/attachment.html
```