💾
EC
  • Introduction
  • EC Course
    • Syllabus
    • Preparation for EC
    • Tutorial
      • Tutorial: arduino-stm32
        • Tutorial: arduino-stm32 Installation
        • Tutorial: arduino-stm32 Part1
        • Tutorial: arduino-stm32 Part2
      • Tutorial: MDK uVision
        • Tutorial: Installing MDK uVision
        • Tutorial: Create a Project with uVision
        • Tutorial: Adding library header in uVision
        • Tutorial: Re-using Project Configuration
        • Debugging in uVision
      • Tutorial: PlatformIO in CLion
      • Tutorial: PlatformIO in VSCode
      • Tutorial: Repository Management
      • Tutorial: Managing library header files
      • Tutorial: PinName Configuration
      • Tutorial: Bitwise Macro
      • Tutorial: Custom initialization
      • Tutorial: Documentation
      • Tutorial: Creating Application API
      • Tutorial: 7-Segment Display
      • Tutorial: DC motor driver connection
      • Tutorial: USART with TeraTerm
      • Tutorial: Finite State Machine programming
      • Tutorial: Bluetooth
      • Tutorial: Zigbee with Nucleo board
    • LAB
      • LAB Report Template
      • LAB: Smart mini-fan with STM32-duino
      • LAB: Portable Fan with mbed
      • LAB: GPIO Digital InOut
      • LAB: GPIO Digital InOut 7-segment
      • LAB: EXTI & SysTick
      • LAB: Timer & PWM
      • LAB: Stepper Motor
      • LAB: Input Capture - Ultrasonic
      • LAB: USART - LED, Bluetooth
      • LAB: ADC - IR reflective sensor
      • LAB: Line Tracing RC Car
    • Sample code
      • Code Templates
    • Hardware
      • Nucleo-F411RE
      • LAB Hardware
        • Electronic Chips
        • HUINS Embedded Kit
    • Projects
      • Line Tracing Car Project
      • Design Project
        • Past Projects
      • Project Grading Criteria
    • Study Resource for MCU
      • Hexa-Decimal Table
      • Bitwise Op for Register
      • System Clock
      • Timer
      • USART
      • ADC
  • STM32 M4 Programming
    • Documentation
      • C++ API Docs
    • Firmware Library
      • PinName Configuration
      • GPIO Digital
      • RCC
      • EXTI_SysTick
      • TIMER
      • USART
    • Troubleshooting
    • mbed for STM32
      • Tutorial: mbed-Part 1
      • Tutorial: mbed - Part 2
      • Tutorial: mbed - Part 3
      • Using mbed API on uVision
    • mbed OS
  • Other Programming
    • Arduino
    • Socket Programming
      • Window Socket Programming
      • Arduino WiFi
    • Cube-MX
    • Github
    • Markdown
      • Example: API documentation
    • MATLAB
  • C Programming
    • C-Programming Lessons
      • Installing Visual Studio Community
        • Visual Studio Community 2022
      • Installing VS Code(Mac/Linux)
      • Creating Header Lib
      • Pointer
      • Array
      • 2D Array
      • Structure
      • Dynamic Alloc
      • Bitwise Operation
  • Numerical Programming
    • Syllabus
    • Preparation for NP
    • Math Review
    • Tutorial
      • TA Session Video
      • Tutorial: NP Library Header Files
      • Tutorial - Sine Taylor
      • Tutorial: Passing a Function, Function callback
      • Tutorial: Nonlinear solver
      • Tutorial: Differentiation
      • Tutorial: Integration
      • Tutorial: Matrix Structure
      • Tutorial: Eigenvalue problem
      • Tutorial: ODE-IVP
      • Tutorial: Curve Fitting
      • Tutorial: Create Github Repos of NP lib
      • Tutorial: Version Control in Github
      • Tutorial: Documentation with Markdown
      • Exercise: Version Control and Documentation
    • Example: MATLAB
    • Example: NP Library
    • Assignment
      • Assignment Factorial and Power
      • Assignment: Version Control and Documentation
    • Problem Bank
Powered by GitBook
On this page
  • Part 1-1: Line Curve Fitting
  • Problem
  • Tutorial: Matlab
  • Exercise: MATLAB
  • Exercise: C
  • Part 1-2: Higher order polynomial curve fitting
  • Problem
  • Exercise: MATLAB
  • Exercise: C

Was this helpful?

  1. Numerical Programming
  2. Tutorial

Tutorial: Curve Fitting

Least Squares Regression

PreviousTutorial: ODE-IVPNextTutorial: Create Github Repos of NP lib

Last updated 6 months ago

Was this helpful?

Part 1-1: Line Curve Fitting

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.

Tutorial: Matlab

T=[30	40	50	60	70	80];
P=[1.05	1.07	1.09	1.14	1.17	1.21];

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

% yopt=Z(1).*x+Z(2);
x=30:10:150;
yopt=polyval(Z,x);  

Exercise: MATLAB

Download the tutorial source file

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

Exercise: C

Download the tutorial source code

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

void linearFit(double vecZ[], double vecX[], double vecY[]);

If you choose to use Matrix structure

linearFit_mat(Matrix _X, Matrix _Y);

	double T[] = { 30, 40, 50, 60, 70, 80 };
	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");

Part 1-2: Higher order polynomial curve fitting

Problem

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

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

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

TU_Curvefitting_student_2024.mlx
Assignment_Curvefit_student.cpp