当前位置:网站首页>Combat readiness mathematical modeling 31 data interpolation and curve fitting 3
Combat readiness mathematical modeling 31 data interpolation and curve fitting 3
2022-06-26 14:28:00 【nuist__ NJUPT】
Catalog
One 、 Classic cases of data interpolation
Two 、 Curve fitting classic case
One 、 Classic cases of data interpolation
The following is pool 1 1,3,5...15 Various indicators of the week , Interpolate the indicators in the pool , And all the images drawn after interpolation are displayed on one graph .
More data , It is troublesome to select each row for interpolation , Circular interpolation possible , then subplot Function to draw multiple graphs , Of course , For one-dimensional interpolation , We prefer cubic spline interpolation .
Concrete MATLAB The code is as follows :
clear; clc
load('Z.mat')
x = Z(1,:) ; % Get a row vector , Weeks
[n,m] = size(Z) ; % Get the number of rows and columns of the original data
% Each picture y The label of the shaft
ylab={' Weeks ',' rotifer ',' Dissolved oxygen ','COD',' The water temperature ','PH value ',' Salinity ',' transparency ',' Total alkalinity ',' Chloride ion ',' transparency ',' Biomass '};
P = zeros(11,15) ;
for i = 2 : n % From 2 Row start interpolation
y = Z(i,:) ;
new_x = 1:15 ;
p1 = interp1(x,y,new_x,'spline') ; % Cubic spline interpolation
subplot(4,3,i-1) ;
plot(x,y,'ro',new_x,p1,'-');
axis([0 15,-inf,inf]) ;
xlabel(' Weeks ') ;
ylabel(ylab(i)) ;
P(i-1,:) = p1 ;
end
legend(' Raw data ',' Cubic spline interpolation data ','Location','SouthEast'); % legend
disp(P) ;
The drawing is as follows :
Two 、 Curve fitting classic case
1- Polynomial fitting
Let's look at a simple example , The following data are fitted by polynomial , Of course, it can be directly fitted and solved , That is to solve the linear regression equation of one variable , It can also be solved by the least square method , In addition to drawing the fitting curve , The goodness of fit is also calculated .
MATLAB The code is as follows :
Method 1, Direct polynomial fitting
clear; clc
load('nihe.mat');
%plot(x,y,'o') ;
xlabel('x Value ') ;
ylabel('y Value ') ;
n = size(x,1) ; % Number of sample points
x = x' ;
y = y' ;
xp = 2.7 : 0.1 : 6.9 ;
p = polyfit(x,y,1) ;
yp = polyval(p,xp);
plot(x,y,'o',xp,yp) ;
legend(' Sample data ',' Fit function ','location','SouthEast') ;
Method 2, Least squares parameter estimation , Then draw according to the expression , The goodness of fit is calculated here .
clear; clc
load('nihe.mat');
plot(x,y,'o') ;
xlabel('x Value ') ;
ylabel('y Value ') ;
n = size(x,1) ; % Number of sample points
% Least square method for parameter estimation
k = (n*sum(x.*y)-sum(x)*sum(y))/(n*sum(x.*x)-sum(x)*sum(x)) ;
b = (sum(x.*x)*sum(y)-sum(x)*sum(x.*y))/(n*sum(x.*x)-sum(x)*sum(x)) ;
hold on ;
grid on ;
f = @(x) k*x + b ; % Anonymous functions
fplot(f,[1,7.5]) ;
legend(' Sample data ',' Fit function ','location','SouthEast') ;
y_hat = k * x + b ;
SSR = sum((y_hat-mean(y)).^2) ; % Sum of regression squares
SSE = sum((y_hat-y).^2) ; % The sum of the squares of the errors
SST = sum((y-mean(y)).^2) ; % Total sum of squares
disp(' Goodness of fit values are as follows :') ;
R_2= SSR / SST ;
disp(R_2) ;
The explanation of goodness of fit is as follows : The goodness of fit is closer to 1, The smaller the sum of squared errors , The better the fitting effect is .
The drawing is as follows :
2- Nonlinear fitting
Let's take a look at the envious population forecast , The nonlinear fitting function can be used directly , You can also use the fitting toolbox cftool
MATLAB The code is as follows , Here, nonlinear fitting is performed directly , Anonymous functions are required .
clear; clc
x = 1790:10:2000;
y = [3.9,5.3,7.2,9.6,12.9,17.1,23.2,31.4,38.6,50.2,62.9,76.0,92.0,106.5,123.2,131.7,150.7,179.3,204.0,226.5,251.4,281.4];
plot(x,y,'o')
y1 = @(b,t) b(1) ./ (1 + (b(1) / 3.9 - 1)*exp(-b(2)*(t-1790))); % Anonymous functions
b0 = [100 0.1] ; % The selection of initial value is very important
a = nlinfit(x,y,y1,b0) ; % Coefficient after nonlinear fitting
xp = 1790 : 10 : 2030 ;
yp = y1(a,xp) ; % Substitute coefficients and values into the expression for evaluation
hold on ;
plot(xp, yp, '-') ;
legend(' Original scatter ',' Fit the curve ') ;
The drawing is as follows :
边栏推荐
- 1075 pat judge (25 points)
- ArcGIS secondary development method - layer related operations (add, modify)
- Correlation analysis related knowledge
- ThreadLocal giant pit! Memory leaks are just Pediatrics
- D - Face Produces Unhappiness
- [hnoi2010] flying sheep
- GDAL grid data types and their type codes
- 近期比较重要消息
- D check type is pointer
- [jsoi2015] string tree
猜你喜欢
New specification of risc-v chip architecture
常用控件及自定义控件
量化框架backtrader之一文读懂observer观测器
ArcGIS cannot be opened and displays' because afcore cannot be found ' DLL, solution to 'unable to execute code'
Eigen(3):error: ‘Eigen’ has not been declared
PostGIS create spatial database
Experience sharing of mathematical modeling: comparison between China and USA / reference for topic selection / common skills
STM32F1和GD32F1有什么区别?
Self created notes (unique in the whole network, continuously updated)
Relevant knowledge of information entropy
随机推荐
量化框架backtrader之一文读懂observer观测器
First k large XOR value problem
Flex & Bison 开始
K gold Chef (two conditions, two points and difference)
Knowledge about the determination coefficient R2 and the relationship with the correlation coefficient
从Celsius到三箭:加密百亿巨头们的多米诺,史诗级流动性的枯竭
[cqoi2015] task query system
Caelus - full scene offline mixed Department solution
Self created notes (unique in the whole network, continuously updated)
2021-10-29 atcoder ABC157——B - Bingo
Eigen(3):error: ‘Eigen’ has not been declared
Never use redis expired monitoring to implement scheduled tasks!
Question bank and answers of the latest Guizhou construction eight (Mechanics) simulated examination in 2022
9项规定6个严禁!教育部、应急管理部联合印发《校外培训机构消防安全管理九项规定》
Installation tutorial about origin2019
启动Redis报错:Could not create Server TCP listening socket *:6379: bind: Address already in use–解决办法
PostGIS create spatial database
How to personalize VIM editor format (DIY)
Is it safe to open a securities account? Is there any danger
Usage of unique function