How to find a figure of polynomial curve fitting for 3 matrices

6 Ansichten (letzte 30 Tage)
Elinbesti
Elinbesti am 14 Jan. 2021
Beantwortet: Dev am 26 Aug. 2025 um 10:08
Hello,
I have 3 matrices(mat1, mat2, mat3) and i found their averages one by one, and now i want to find their best polynomial curve fitting for each matrix(for each matrix should have 1 figrue of its polynomial curve fitting) by using X, average of (mat1, mat2 and mat3) and X2(X2 can be changable to suit for the best curve) as written in the codes bellow.
I'm able to do for 1 matrix but need help to do for 3 matrices
Many thanks in advance
clc
clear
X = [1 2 3];
mat1= [7 12 177;
0 10 7;
12 0 55];
mat2= [3 6 21;
0 4 3;
6 0 8];
mat3= [2 4 61;
0 3 2;
4 0 19];
Y = mean(mat1)
Y1 = mean(mat2)
Y2 = mean(mat3)
coefs = polyfit(X,Y,4);
X2= 1:0.1:3;
curve = polyval(coefs,X2);
plot(X2, curve, X, Y);
xlabel('x axis');
ylabel('y axis');
title('x and y');

Antworten (1)

Dev
Dev am 26 Aug. 2025 um 10:08
To plot the best polynomial curve for each matrix, we can modify the code provided in the question to use 2nd degree polynomials instead of 4th degree. Since we only have 3 data points, making 2nd degree more appropriate. Please find the updated code snippet below-
coefs = polyfit(X, Y1, 2); % Using 2nd degree polynomial
We can then use the “subplot” function along with the “figure” function to split our plots accordingly and plot all the three curves simultaneously.
I have attached an example figure after plotting all the three functions for your reference-
For more information on the usage of the functions mentioned above, please refer to the following links-
I hope the above explanation solves the question.

Kategorien

Mehr zu Get Started with Curve Fitting Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by