
L = 2;

F = inline('x.^3.*sin(n*pi*x/L)','x','n','L');  %this is the integrand in coefficient formula

for n=1:10,
   A(n) = (2/L)*quad(F,0,L,[ ] ,[ ] ,n,L);  %sine series coeff.;  "quad" does the integration!
end;

% make a "grid" for plotting
x = [0:.01:L];

%Now sum first few terms
f = 0*x;  % set f = vector w/ length of x
for n = 1:10,
   f = f + A(n)*sin(n*pi*x/L);
end;

% finally, plot
plot(x,f);
hold on;
plot(x,x.^3,'--');  %plot exact function
hold off;

