function [] = approxsinederiv(N) % stepsize h = 2*pi/N; % measurements of sine function x = [0:h:2*pi]; y = sin(x); % approximate derivatives yprime(2:N) = (y(3:N+1)-y(1:N-1))/(2*h); yprime(1) = (y(2)-y(N))/(2*h); yprime(N+1) = yprime(1); % exact derivatives exact = cos(x); subplot(221) plot(x,y,'x') axis([0 2*pi -1 1]) subplot(222) plot(x,exact,'x') axis([0 2*pi -1 1]) subplot(223) plot(x,yprime,'x') axis([0 2*pi -1 1]) subplot(224) plot(x,abs(exact-yprime),'x') v = axis; axis([0 2*pi v(3) v(4)])