51 views (last 30 days)
Show older comments
Graham about 7 hours ago
Edited: Milan Bansal about 5 hours ago
- Screenshot_20240717_215107_Drive~2.jpg
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 Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Answers (1)
Milan Bansal about 3 hours ago
Edited: Milan Bansal about 3 hours ago
Open in MATLAB Online
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.
- https://www.mathworks.com/help/curvefit/fit.html
- https://www.mathworks.com/help/curvefit/fittype.htm
- https://www.mathworks.com/help/curvefit/feval.html
Hope this helps!
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
See Also
Tags
- interpolation
- curve fitting
Products
- Curve Fitting Toolbox
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office