- https://www.mathworks.com/help/curvefit/fit.html
- https://www.mathworks.com/help/curvefit/fittype.htm
- https://www.mathworks.com/help/curvefit/feval.html
Trying to create a polynomial formula from xyz chart data where x and y equate to a z value.
17 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Milan Bansal
on 18 Jul 2024
Edited: Milan Bansal
on 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);
% 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)
Please refer to the following documentation links to learn more about fit, fittype and fiteval.
Hope this helps!
3 Comments
Steven Lord
on 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.
More Answers (0)
See Also
Categories
Find more on Linear and Nonlinear Regression in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!