How to increase the number of decimal places included in MATLAB computation?
8 views (last 30 days)
Show older comments
I'm trying to obtain the values and their errors of the components of a best fit function that has underwent chi-squared minimization through the use of the following code. The error values are obtained by analyzing the diagonal of the covariance matrix, however, 3 of the 6 values where returning complex numbers. By conducting the chi-square minimization throught Excel it was determined that not enough decimals had been retained during the calculation of the covariance matrix and retaining 5 or 6 decimals would cause the covariance matrix to return all real numbers.
Is there a way to ensure MATLAB uses more decimal places during the computation process?
I have already tried including "format long" in the code and nothing has changed.
I'm NOT looking for more decimals to be shown in the display window, just more decimal places used in the actual calculations. Thanks in advance.
% define the fittype for the model function
ft = fittype('a1 + b1*exp(-((x-c1)/d1)) + e1*exp(-((x-c1)/f1))');
% perform the chi-squared minimization
[f, gof, fit_output] = fit(time_data, count_data, ft,...
'Weights', count_data_error.^(-2) ,'StartPoint', [17.31282, 1633.025, 0.56563, 0.192456, 142.6486, 2.342308]);
% extract the weighted Jacobian matrix of the model fit
J = fit_output.Jacobian;
% compute the curvature and covariance matrices
curvature_matrix = J' * J;
covariance_matrix = inv(curvature_matrix);
2 Comments
Chunru
on 4 Oct 2022
matlab use double by default which has an accuracy of about 16 decimal digits. Do you think 16 decimal digits accuray is not enough for your problem at hand?
Stephen23
on 4 Oct 2022
"By conducting the chi-square minimization throught Excel it was determined that not enough decimals had been retained during the calculation of the covariance matrix... "
Both MATLAB and Eccel use exactly the same IEEE 754 double class for numeric calculations:
"...and retaining 5 or 6 decimals would cause the covariance matrix to return all real numbers."
Excel does include an option "precision as displayed" (which reduces the calculation precision), but MATLAB always uses the native double/single precision.
Answers (0)
See Also
Categories
Find more on Hypothesis Tests in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!