MATLAB: Warning: Matrix is singular, close to singular or badly scaled. Results may be inaccurate. RCOND = NaN

curve fittingMATLABmatrixmatrix manipulation

So I'm very new to MATLAB and basically copying code from my instructors to try and generate a periodic function to fit a set of randomly generated points to but I keep getting the error in the title and I need to know how to correct it. To put it into perspective I'm supposed to get a plot that looks like this but all that ends up being plotted is the random points. My code follows, is there some small error I've made that would resolve this?
Thanks for looking 🙂
x = (0:1:6);
f = randn(7,1)
plot(x,f,'ok','MarkerFaceColor','r','MarkerSize',10);
xlabel('x');
ylabel('f');
xlim([-5. 12.]);
ylim([-5. 5.]);
title('Fourier Fit');
G(1:7,1) = ones(7,1);
auxi = sin( k .* x);
G(1:7,2) = auxi';
auxi = cos( k .* x);
G(1:7,3) = auxi';
auxi = sin( 2. .* k .* x);
G(1:7,4) = auxi';
auxi = cos( 2. .* k .* x);
G(1:7,5) = auxi';
auxi = sin( 3. .* k .* x);
G(1:7,6) = auxi';
auxi = cos( 3. .* k .* x);
G(1:7,7) = auxi';
a = G\f;
xp = (-5:0.085:12);
ff = a(1) + a(2) .* sin(k.*xp) + a(3) .* cos(k.*xp);
ff = ff + a(4) .* sin(2.*k.*xp) + a(5) .* cos(2.*k.*xp);
ff = ff + a(6) .* sin(3.*k.*xp) + a(7) .* cos(3.*k.*xp);
plot(xp,ff)

Best Answer

% I have done the minor corrections (mainly . )
% Define a, I have define it a=10,I dont know whta suppose value it would be
% and run the code, it perfectly run, check wheather it OK or not.
x=(0:1:6);
f=randn(7,1)
figure, plot(x,f,'ok','MarkerFaceColor','r','MarkerSize',10);
xlabel('x');
ylabel('f');
xlim([-5. 12.]);
ylim([-5. 5.]);
title('Fourier Fit');
G(1:7,1)=ones(7,1);
%% Example a, ket suppose a
a=10;
%% End a
k=2*pi/a;
auxi=sin(k.* x);
G(1:7,2)=auxi';
auxi=cos(k.* x);
G(1:7,3)=auxi';
auxi=sin(2.*k.*x);
G(1:7,4)=auxi';
auxi=cos(2.*k.*x);
G(1:7,5)=auxi';
auxi=sin(3.*k.*x);
G(1:7,6)=auxi';
auxi=cos(3.*k.*x);
G(1:7,7)=auxi';
a=G\f;
xp=(-5:0.085:12);
ff=a(1)+a(2).*sin(k.*xp)+a(3).*cos(k.*xp);
ff=ff + a(4).*sin(2.*k.*xp)+a(5).*cos(2.*k.*xp);
ff=ff+a(6).*sin(3.*k.*xp)+a(7).*cos(3.*k.*xp);
figure,plot(xp,ff)