% buggy codes - please fix! 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 (1/2pi)*I_0(1) modified bessel func
xj = (0:N)*(2*pi/N);
wj = 2*pi/N * ones(size(xj));
Qnf = sum(wj .* f(xj));
(1/2*pi)*Qnf-Qf
% #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=1: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?
% #3
% Supposed to solve for equi-spaced Newton-Cotes weights on [-1 1]
n = 20;
xj = (-1:2/n:1)';
A = vander(xj);
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?