# Tutorial: Linear Regression

## Part 1: Linear Regression (line)

### Problem

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

<figure><img src="/files/KpIoy8pVYHNJqTm5tRVr" alt="" width="237"><figcaption></figcaption></figure>

<figure><img src="/files/HGpvQUxqNR94iJljaAIR" 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\_LinearRegression\_student.cpp](https://github.com/ykkimhgu/NumericalProg-student/blob/main/src/Assignment_LinearRegression_student.cpp)

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

`void linearRegression (double z_opt[], double xdata[], double ydata[], int dataN)`

***

## Part 2: Higher-order polynomial curve fitting (Optional)

### Problem

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

<figure><img src="/files/9GaVUM69PTMXswRvFBIE" 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 (optional)

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

Download the tutorial source code

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

`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);`

<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>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ykkim.gitbook.io/ec/numerical-programming/ta-tutorial/tutorial-curve-fitting.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
