% buggy codes - now debugged! X-hr of Feb 1st, Math 126
% #1
% supposed to check periodic trapezoid rule works well for a known integral
N = 10;
f = @(x) exp(sin(x));
Qf = besseli(0, 1); % exact is 2pi*I_0(1) modified bessel func (note math bug)
xj = (1:N)*(2*pi/N);
wj = 2*pi/N * ones(size(xj));
Qnf = sum(wj .* f(xj));
(1/(2*pi))*Qnf-Qf
% ans = -5.50589351888675e-10
% #2
% Supposed to plot the 20-term Taylor series for 1/(1-z) and show it
% converges to the function near the origin
x = -1.5:0.05:1.5;
[xx yy] = meshgrid(x,x);
zz = xx + 1i*yy;
f = @(z) 1./(1-z); % the func to compare to
tt = 0*xx; % array for the taylor series, add in each term in a loop:
for n=0:20
tt = tt + zz.^n;
end
figure; surf(x,x, abs(tt - f(zz))); axis([-1.5 1.5 -1.5 1.5 0 1]);
% once debugged: in what domain does it converge?
% In the unit disc. Need to increase from N=20 to check convergence.
% #3
% Supposed to solve for equi-spaced Newton-Cotes weights on [-1 1]
n = 20;
xj = (-1:2/n:1)';
A = vander(xj);
A = A(:,end:-1:1); A = A';
b = zeros(n+1,1); %cond(A)
for j = 1:n
b(j) = (1-(-1)^j)/j;
end
wj = A\b;
% Is A correct?
% How are you going to test the weights are correct?
% Like this: integral of cos from [-1 1] is 2*sin(1):
sum(wj.*cos(xj)) - 2*sin(1)
% ans = -2.74340550276975e-11