% % Errore e stima dell'errore nell'approssimazione della derivata prima % e seconda di sin(3x) % mrange = 10:10:100; counter = 0; for m = mrange+1 counter = counter + 1; x = linspace(0, 2 * pi, m); h = 2 * pi / (m - 1); u = sin(3*x); u1 = (u(3:m) - u(1:m-2)) / (2 * h); u2 = (u(3:m) - 2 * u(2:m-1) + u(1:m-2)) / h ^ 2; u1exact = 3*cos(3*x); u2exact = -9*sin(3*x); err1inf(counter) = norm(u1 - u1exact(2:m-1), inf); err2inf(counter) = norm(u2 - u2exact(2:m-1), inf); estimate1(counter) = h ^ 2 / 6 * 3 ^ 3; % 3^3 is the maximum of u'''(x) estimate2(counter) = h ^ 2 / 12 * 3 ^ 4; % 3^4 is the maximum of u''''(x) end loglog(mrange, err2inf, 'r*', mrange, estimate2, 'r',... mrange, err1inf, 'b*', mrange, estimate1, 'b') legend('errore derivata seconda', 'stima errore derivata seconda', ... 'errore derivata prima', 'stima errore derivata prima') xlabel('m') ylabel('errore in norma infinito')