当前位置:网站首页>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 :

边栏推荐
- Exercises under insect STL string
- Bucket of P (segment tree + linear basis)
- Linear basis count (k large XOR sum)
- STM32F1和GD32F1有什么区别?
- 9項規定6個嚴禁!教育部、應急管理部聯合印發《校外培訓機構消防安全管理九項規定》
- 秒懂JSONArray和JSONObject的区别和使用
- A must for programmers, an artifact utools that can improve your work efficiency n times
- RISC-V 芯片架构新规范
- 服务器创建虚拟环境跑代码
- ArcGIS cannot be opened and displays' because afcore cannot be found ' DLL, solution to 'unable to execute code'
猜你喜欢

From Celsius to the three arrows: encrypting the domino of the ten billion giants, and drying up the epic liquidity

STM32F1和GD32F1有什么区别?

2022年最新贵州建筑八大员(机械员)模拟考试题库及答案

Usage of unique function

A must for programmers, an artifact utools that can improve your work efficiency n times

ThreadLocal giant pit! Memory leaks are just Pediatrics

Hard (magnetic) disk (II)

Leaflet load day map

Eigen(3):error: ‘Eigen’ has not been declared

Build your own PE manually from winpe of ADK
随机推荐
Difference between classification and regression
常用控件及自定义控件
Eigen(3):error: ‘Eigen’ has not been declared
On insect classes and objects
数学建模经验分享:国赛美赛对比/选题参考/常用技巧
Leaflet loading ArcGIS for server map layers
Pychar remotely connects to the server to run code
Memory considerations under bug memory management
Common evaluation indexes of classification model -- confusion matrix and ROC curve
C language ---getchar() and putchar()
A标签去掉下划线
In insect classes and objects
Eigen(3):error: ‘Eigen’ has not been declared
Niuke challenge 53:c. strange magic array
RISC-V 芯片架构新规范
[hcsd application development training camp] one line of code second cloud evaluation article - experience from the experiment process
fileinput.js php,fileinput
H5关闭当前页面,包括微信浏览器(附源码)
K gold Chef (two conditions, two points and difference)
从Celsius到三箭:加密百亿巨头们的多米诺,史诗级流动性的枯竭



