Azzera filtri
Azzera filtri

Find matrix covariance by fitting data as a parabola

13 visualizzazioni (ultimi 30 giorni)
Hi! I need to fit my data into a parabola and I need also the covariance matrix of the coefficients... anyone knows how to do that?
  1 Commento
Mathieu NOE
Mathieu NOE il 15 Dic 2021
hi
I can help for the first part of the question :
% dummy data
X = [-5 -3 -1 1 2 4].';
Y = [7 3 1 0 1 5].'+0.5*randn(6,1);
m = length(X);
%Set up the appropriate matrix A to find the best-fit parabola of the form y=C+Dx+Ex^2. The
%first column of A will contain all 1's, using the ones() command. The second column of A
%contains x values that are stored in X. The third column of A contains the squared x values
%that are stored in X. Elementwise multiplication of X by itself, using .* operator, will
%produce the desired values for the third column.
A = [ones(m,1) X X.*X];
A_transposeA = A.' * A;
A_transposeY = A.' * Y;
%backslash operation to solve the overdetermined system.
Soln2 = A_transposeA\A_transposeY;
%x values to use for plotting the best-fit parabola.
x=-5: 0.1 :5;
yfit = Soln2(1) + Soln2(2)*x + Soln2(3)*x.*x; %
plot(x, yfit, X, Y, 'r*');

Accedi per commentare.

Risposte (1)

Shubham
Shubham il 26 Feb 2024
Hi Pietro,
In MATLAB, you can fit a parabola to your data using the polyfit function, which allows you to fit your data with a polynomial of a specified degree (2 for a parabola). You can also obtain the covariance matrix of the coefficients using the same function by requesting two output arguments.
Here's a step-by-step guide on how to do it in MATLAB:
  1. Organize your data into two vectors, x and y, where x contains the independent variable data and y contains the dependent variable data.
  2. Use the polyfit function to fit a second-degree polynomial to your data and to get the covariance matrix. The general syntax is [p, S] = polyfit(x, y, n), where n is the degree of the polynomial (2 for a quadratic fit), p contains the coefficients, and S is a structure containing information for use by polyval to generate error estimates for predictions.
  3. To get the actual covariance matrix of the polynomial coefficients, you will need to use the expression, where S is the second output from polyfit.
Here's a MATLAB code snippet that demonstrates this process:
% Sample data
x = [1, 2, 3, 4, 5];
y = [2.3, 4.1, 6.3, 7.9, 10.2];
% Fit a second-degree polynomial to the data
[p, S] = polyfit(x, y, 2);
% Calculate the covariance matrix of the polynomial coefficients
% Note: This calculation is valid only if the errors in the data are independent
% and normally distributed with constant variance.
cov_p = (inv(S.R)*inv(S.R')) * (S.normr^2) / (length(y) - length(p));
% Display the results
disp('Coefficients (a, b, c):');
disp(p);
disp('Covariance matrix:');
disp(cov_p);
In this code snippet, p will be a vector containing the coefficients [a, b, c] of the fitted parabola y = ax^2 + bx + c, and cov_p will be the covariance matrix of these coefficients. Remember to replace the sample x and y values with your actual data.

Prodotti


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by