Analyzing errors of a numerical Jacobian matrix of partial derivatives, as the finite-differencing step-size gets successively smaller,
Mostrar comentarios más antiguos
Hi there!
Recently, I learned to write a central difference method to approximate a Jacobian matrix of partial derivatives for my equations of interest. I am currently trying to study the method error. To do this, I am using "cell arrays", and I populate these with a numerical Jacobian corresponding to a particular step-size h. As the step-size h successively gets smaller, I compute the difference between two successive Jacobians, and I call this an error matrix. I compute 2-norm of this error matrix, using the norm( ) function. And finally, I make a loglog plot of the matrix norms vs. the step-size h.
Is this a good / correct way to study the method error?
Is there a better way to do it?
Thanks in advance!

%% linear stability analysis
% h = 1e-8; % finite-differencing step-size
e = zeros(6,1); % 6 x 1 zero vector
NumericalJacobian = nan(6); % 6x6 matrix of NaNs, to be filled in with partial derivatives
n = 6;
J = cell(n,1); % use cell arrays to store Jacobian matrices
figure(3)
for K = 1:12 % loop index for the finite-differencing step-size
h = 10^(-K); % finite-difference step-size gets successively smaller
for i = 1:6 % loop over i, from 1 to 6, for computing partial derivatives
e(i) = 1; % temporarily set the ith-component of e = 1
CDM = ( zdot(z_0 + h*e) - zdot(z_0 - h*e) ) / (2*h); % write a central-difference method for computing the Jacobian
% of zdot with respect to the ith partial derivative
NumericalJacobian(:,i) = CDM; % store the ith partial derivatives in the ith column of the Jacobian matrix
e(i) = 0; % reset the ith-component of e = 0
J{K} = NumericalJacobian; % store the K-th Jacobian matrix in a 6x6 cell array
end
if K > 1 % when K > 1, we want to compute the successive differences in matrix entries
norm_error_matrix = norm(J{K} - J{K-1}); % compute the 2-norms of the error matrices
% error_n = sqrt(sum(sum(J_diffn.*J_diffn)/16))
loglog(h,norm_error_matrix,'b.','MarkerSize',10); % use a loglog plot to plot the matrix norms against the step-size h
hold on
end
end
xlabel('step-size $h$', 'interpreter','latex','FontSize',12) % create an x-label
ylabel('$\rm{error}_n$','interpreter','latex','FontSize',12) % create a y-label
title('Norm of Error Matrix .vs step-size $h$','interpreter','latex','FontSize',12) % create a title
grid on % use grid lines
hold off % use hold off, after the figure is finished
5 comentarios
I don't know whether taking the maximum singular value of the matrix J{K} - J{K-1} here
norm_error_matrix = norm(J{K} - J{K-1});
is a good measure for the error you make in approximating the Jacobian.
At least for me,
norm(J{K} - J{K-1},"fro")
would be easier to understand.
Noob
el 6 de Mayo de 2025
Torsten
el 6 de Mayo de 2025
"fro" means "Frobenius norm" of a matrix A=(a_ij):
norm(A,"fro") = sqrt(sum_i sum_j a_ij^2).
Noob
el 6 de Mayo de 2025
Noob
el 6 de Mayo de 2025
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


