---
description: Least Squares Regression
---

# Tutorial: Curve Fitting

## Part 1-1: Line Curve Fitting&#x20;

### Problem

#### Predict the pressure if the temperature is increased to 150C based on Charles's law for ideal gasP=kT, where k is a constant.

<figure><img src="https://3453146497-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgmrEstOHxu62gXxq1t%2Fuploads%2Fd5OIQ3qQrEJHPRIZP22g%2Fimage.png?alt=media&#x26;token=8df867ae-9b04-46cd-aa7d-92961dcaafed" alt="" width="237"><figcaption></figcaption></figure>

<figure><img src="https://3453146497-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgmrEstOHxu62gXxq1t%2Fuploads%2FWwHuwctYPpyxIX59nOF7%2Fimage.png?alt=media&#x26;token=ccf0fe66-cc27-43a7-8dd8-f478a7afafa4" alt="" width="375"><figcaption></figcaption></figure>

### Tutorial: Matlab

<pre class="language-matlab"><code class="lang-matlab">T=[30	40	50	60	70	80];
P=[1.05	1.07	1.09	1.14	1.17	1.21];

<strong>% MATLAB function of curvefitting
</strong>% z=[a1, a0]; 
Z=polyfit(T,P,1)

% yopt=Z(1).*x+Z(2);
x=30:10:150;
yopt=polyval(Z,x);  
</code></pre>

### Exercise: MATLAB

Download the tutorial source file

* [TU\_Curvefitting\_student\_2024.mlx](https://github.com/ykkimhgu/NumericalProg-student/blob/main/tutorial/TU_Curvefitting/TU_Curvefitting_student_2024.mlx)

Fill-in the blanks to create `function [a0,a1] = linearFit(X, Y)`

### Exercise: C

Download the tutorial source code

* [Assignment\_Curvefit\_student.cpp](https://github.com/ykkimhgu/NumericalProg-student/blob/main/src/Assignment_Curvefit_student.cpp)

Fill-in the blanks to create functions that  calculates coefficients of least squares regression (line)

`void linearFit(double vecZ[], double vecX[], double vecY[]);`&#x20;

If you choose to use  Matrix structure

`linearFit_mat(Matrix _X, Matrix _Y);`&#x20;



<pre class="language-c"><code class="lang-c"><strong>	double T[] = { 30, 40, 50, 60, 70, 80 };
</strong>	double P[] = { 1.05, 1.07, 1.09, 1.14, 1.17, 1.21 };
	double Z_Q1[2] = { 0 };
	int n = 1;	// nth order
	int m_Q1 = 6;	// length of dataset

	// Option 1: using 1D array
	n = 1;
	polyFit(Z_Q1, T, P, n);
	
	// Option 2	
	// Delete the below if you selected Option 1
	Matrix matT = arr2Mat(T, m_Q1, 1);
	Matrix matP = arr2Mat(P, m_Q1, 1);
	Matrix vecZ_Q1 = polyFit_mat(matT, matP, n);
	printMat(vecZ_Q1, "Z_Q1");

</code></pre>







## Part 1-2: Higher order polynomial curve fitting&#x20;

### Problem

Find the optimal higher-order polynomial to fit the given dataset. assume the model has n=4 order polynomial form

<figure><img src="https://3453146497-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgmrEstOHxu62gXxq1t%2Fuploads%2FAWvk3lhofal3AAoYtmPo%2Fimage.png?alt=media&#x26;token=b6417d18-b25b-4356-8a82-6c60d94d6b4f" alt="" width="375"><figcaption></figcaption></figure>

### Exercise: MATLAB

```matlab

Xdata = 0:0.4:6;
Ydata = [0, 3, 4.5, 5.8, 5.9, 5.8, 6.2, 7.4, 9.6, 15.6, 20.7, 26.7, 31.1, 35.6, 39.3, 41.5];

% MATLAB function of curvefitting
% z=[an,..., a1, a0]; 

% polynomial order n=4
n=4;
Z=polyfit(Xdata,Ydata,n)

% yhat on the fitted curve:
% yopt=Z(5).*(x.^4) +...+ +Z(0);
Yopt=polyval(Z,Xdata);

figure
plot(Xdata,Ydata, '*r')
hold on
plot(Xdata,Yopt, '-b')
xlabel('strain','fontsize',15)
ylabel('stress','fontsize',15)
title('Polyfit')
```

### Exercise: C

Fill-in the blanks to create functions that  calculates coefficients of least squares regression of  Nth order polynomial&#x20;

`void polyFit(double vecZ[], double vecX[], double vecY[], int n);`

If you choose to use  Matrix structure

`Matrix polyFit_mat(Matrix _vecX, Matrix _vecY, int n);`



