Skip to content

Instantly share code, notes, and snippets.

@shane5ul
Created June 9, 2014 20:08
Show Gist options
  • Save shane5ul/1f6f1beefa8cdc1d3d6e to your computer and use it in GitHub Desktop.
Save shane5ul/1f6f1beefa8cdc1d3d6e to your computer and use it in GitHub Desktop.
Discrte Cosine Series Tutorial 2
n = 15; % number of collocation points is (n+1)
nmodes = 5; % number of modes of the DCS
xi = [0:n]'/(n+1); % collocation points for discrete cosine series
V = zeros(n+1, nmodes);
%
% V: stack the nmodes DCS basis functions columnwise
%
for j = 1:nmodes
V(:,j) = cos(2*pi*(j-1)*xi); % evaluation of phi_j(x_i) at all the n+1 nodes
end
%
% Sample Function
%
fi = (1/2-xi).^2 + cos(2*pi*xi).^2;
%
% if nmodes = n+1: interpolation
% nmodes < n+1: approximation
nmodes = 5;
a = zeros(nmodes,1);
a=2/(n+1)*V'*fi;
a(1) = a(1)/2; % a(0) needs a factor of 1/2
%
% Plot data, and the approximating function
%
x = linspace(0,1)';
V = zeros(numel(x), nmodes);
for j = 1:nmodes
V(:,j) = cos(2*pi*(j-1)*x);
endfor
g = V*a;
plot(xi,fi,'o',x,g,'LineWidth',3)
xlabel('x')
ylabel('f(x)')
a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment