Yeah Pylab python code translates pretty directly to MATLAB code. That's why they called it PyLAB
But you made two mistakes (highlight in red):
% % % %%% Day_n = 20120419 t_Start = 1223
% data points
da=[2, 0, 1, 2, 1, 2, 2, 1]; l=length(da); x=1:l;
% lines + green o's
% set axis limits
axis([-5, 9, -5, 5])
% dotted lines
lookfor polyfit %% POLYFIT Polynomial curve fitting.
help polyfit %%% finds the coefficients of a polynomial
%%%%% See also POLY, POLYVAL, ROOTS.
% polynomial fit, blue curve
p7=polyfit(da, x, 7)
%% % Warning: Matrix is singular to working precision.
p3=polyfit(da, x, 3) % %%% -0.6944 0 4.0278 2.0000
%%% % Warning: Rank deficient, rank = 3 tol = 2.8588e-014
% polynomial fix, RED curve's lookfor frange NOT FOUND
As far as I remember from MATLAB, the code x = 1:l
generates a sequence [1,2,3,4,5,6,7,8]? But Python's range(
makes a sequence [0,1,2,3,4,5,6,7].
But the reason why you get the rank error is because you switched the order of your parameters for polyfit. It should work if you do this:p = polyfit([0,1,2,3,4,5,6,7], da, 7)
See the first param is the x points and the second is the y (data) points. Otherwise it tries to fit a polynomial for [0,1,2,3,4,5,6,7] on the y axis for the points [2,0,1,2,2,1,1,2] on the x-axis and you get multiple values for a single x position and you can't fit a polynomial to that.
(also try putting [tt]
tags around your code, it looks prettier