% 2nd order accurate first derivative of a function % u(x) data found in the file: data_MoreReal.dat load data_MoreReal.dat x = data_MoreReal(:,1); u = data_MoreReal(:,2); dx=(x(end)-x(1))/(length(x)-1); n=length(x); % number of points % Use second order forward difference ux(1)=(-3*u(1)+4*u(2)-u(3))/(2*dx); % Use second order center difference for j=2:n-1 ux(j)=(u(j+1)-u(j-1))/(2*dx); end % Use second order backward difference ux(n)=(3*u(n)-4*u(n-1)+u(n-2))/(2*dx); % plot the numerical integral figure(1), plot(x,ux,'r*'); % plot the exact integral (this is known because I obtained % the data in data_MoreReal using: -tanh(x).*sech(x) figure(1), hold on, plot(x,-sech(x).*tanh(x),'b');