Filter löschen
Filter löschen

Trying to create a polynomial formula from xyz chart data where x and y equate to a z value.

20 Ansichten (letzte 30 Tage)
I'm trying to create a polynomial formula it could be a 3rd degree to a 6th degree it doesn't matter how long the formula is I just need it to be accurate and be able to extrapolate as accurately as possibly if the x or y data inputted is off the ends of the chart. Attached is the example of the data that I'm referencing I would like to get a code that I could edit and run myself as I have about 20 similar charts that I want to extrapolate a formula for. I need to be only a temp(y) and a humidity(x) and have the formula spit out (z) and it be accurate based off the chart.

Akzeptierte Antwort

Milan Bansal
Milan Bansal am 18 Jul. 2024
Bearbeitet: Milan Bansal am 18 Jul. 2024
Hi Graham,
I understand that you wish to fit 2D surface to your data which will accept the temperature and humidity and will predict the extrapolated values.
To achieve this, you can use the "fit" function in MATLAB to fit a surface plot. Define the type of fit of using the "fittype" function. For this case you can use fit type as "poly33" (degree of the for the x terms and degree of three for the y terms).
Please refer to the following code snippet to for step wise solution;
% Data preparation
temp = [-2, 2, 5, 8, 10, 13, 15, 18, 22, 26, 28]';
humidity = [35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85];
values = [
10.7, 11.4, 12.0, 12.7, 13.5, 14.2, 15.0, 15.9, 16.9, 18.0, 19.3;
10.4, 11.1, 11.8, 12.5, 13.3, 14.0, 14.8, 15.7, 16.7, 17.8, 19.1;
10.3, 11.0, 11.7, 12.4, 13.1, 13.9, 14.7, 15.5, 16.5, 17.6, 19.0;
10.1, 10.8, 11.5, 12.2, 13.0, 13.7, 14.5, 15.4, 16.4, 17.5, 18.9;
10.0, 10.7, 11.4, 12.1, 12.9, 13.6, 14.4, 15.3, 16.3, 17.4, 18.8;
9.9, 10.6, 11.3, 12.0, 12.7, 13.5, 14.3, 15.2, 16.2, 17.3, 18.7;
9.8, 10.5, 11.2, 11.9, 12.6, 13.4, 14.2, 15.1, 16.1, 17.2, 18.6;
9.6, 10.3, 11.0, 11.8, 12.5, 13.3, 14.1, 15.0, 16.0, 17.1, 18.5;
9.4, 10.2, 10.9, 11.6, 12.3, 13.1, 13.9, 14.8, 15.8, 16.9, 18.3;
9.3, 10.0, 10.7, 11.4, 12.2, 12.9, 13.8, 14.6, 15.6, 16.8, 18.2;
9.2, 9.9, 10.6, 11.3, 12.1, 12.8, 13.7, 14.6, 15.6, 16.7, 18.1
];
% Flatten the data for fitting
[X, Y] = meshgrid(humidity, temp);
Z = values;
% Fit a 2D polynomial surface of degree 3 (example)
ft = fittype('poly33'); % poly33 degree of the for the x terms and degree of three for the y terms
f = fit([X(:), Y(:)], Z(:), ft);
% Display the fitted model
disp(f);
Linear model Poly33: f(x,y) = p00 + p10*x + p01*y + p20*x^2 + p11*x*y + p02*y^2 + p30*x^3 + p21*x^2*y + p12*x*y^2 + p03*y^3 Coefficients (with 95% confidence bounds): p00 = 2.512 (2.05, 2.975) p10 = 0.3455 (0.3214, 0.3696) p01 = -0.06088 (-0.07545, -0.0463) p20 = -0.004431 (-0.004838, -0.004024) p11 = 5.833e-05 (-0.0003571, 0.0004738) p02 = 0.0005309 (9.877e-06, 0.001052) p30 = 3.148e-05 (2.923e-05, 3.372e-05) p21 = 1.251e-06 (-1.992e-06, 4.494e-06) p12 = -1.737e-06 (-6.865e-06, 3.391e-06) p03 = -5.446e-06 (-1.603e-05, 5.141e-06)
% Plot the fitted surface
figure;
plot(f, [X(:), Y(:)], Z(:));
xlabel('Humidity');
ylabel('Temperature');
zlabel('Values');
To predict the values from this surface, use the feval function as shown in the code snippet below:
newTemp = 34;
newHumidity = 90;
value = feval(f, newHumidity, newTemp)
value = 19.3369
Please refer to the following documentation links to learn more about fit, fittype and fiteval.
Hope this helps!
  3 Kommentare
Graham
Graham am 18 Jul. 2024
Thank you for the answers, this was exactly what I was looking for! I wont need to extrapolate the data very much outside of the input chart maybe a units, so some error is acceptable!
Cheers
Steven Lord
Steven Lord am 18 Jul. 2024
In support of John's comment about extrapolation being potentially dangerous, take a look at these two blog posts written by Cleve Moler: post 1, post 2.
In post 1, the pictures show interpolating census data using four different degrees of polynomials. Degrees 1 and 3 look fairly normal even after the last data point in 2020. Degree 7 looks concerning with its sharp drop after 2020, while degree 12 looks even more concerning with its rapid population explosion.
In post 2 the plot in the Conclusion section for degree 4 also looks concerning, and the error estimates get to be extremely broad.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Interpolation 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