
How to find a figure of polynomial curve fitting for 3 matrices
8 views (last 30 days)
Show older comments
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');
0 Comments
Answers (1)
Dev
on 26 Aug 2025 at 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.
0 Comments
See Also
Categories
Find more on Get Started with Curve Fitting Toolbox 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!