MATLAB Examples - Curve Fitting
MATLAB Examples - Curve Fitting
Mathematical Model
Data
Curve Fitting
• MATLAB has built-in curve fitting functions that allows us to
create empiric data model.
• It is important to have in mind that these models are good only
in the region we have collected data.
• Here are some of the functions available in MATLAB used for
curve fitting:
- polyfit()
- polyval()
• These techniques use a polynomial of degree N that fits the data
Y best in a least-squares sense.
Regression Models
Linear Regression:
𝑦(𝑥) = 𝑎𝑥 + 𝑏
Polynomial Regression:
𝑦 𝑥 = 𝑎! 𝑥 " + 𝑎# 𝑥 "$# + ⋯ + 𝑎"$# 𝑥 + 𝑎"
1.order (linear):
𝑦(𝑥) = 𝑎𝑥 + 𝑏
2.order:
𝑦 𝑥 = 𝑎𝑥 % + 𝑏𝑥 + 𝑐
etc.
Linear Regression
Given the following data: Temperature, T [ oC] Energy, u [KJ/kg]
100 2506.7
150 2582.8
200 2658.1
250 2733.7
300 2810.4
400 2967.9
500 3131.6
Plot u versus T.
Find the linear regression model from the data
𝑦 = 𝑎𝑥 + 𝑏
Plot it in the same graph.
T = [100, 150, 200, 250, 300, 400, 500];
u=[2506.7, 2582.8, 2658.1, 2733.7, 2810.4, 2967.9, 3131.6];
a=p(1)
b=p(2)
x=u;
ymodel=a*x+b;
plot(u,T,'o',u,ymodel)
a=
0.6415 𝑦 ≈ 0.64𝑥 − 1.5 / 10,
b=
-1.5057e+003
i.e, we get a polynomial 𝑝 = [0.6, −1.5 + 10! ]