Tutorial: Differentiation

Tutorial: Differentiation

Ploblem

Estimate the velocity and acceleration from datasets of the position of an object.

t = 0:0.2:4;
y = [-5.87 -4.23 -2.55 -0.89 0.67 2.09 3.31 4.31 5.06 5.55 5.78 5.77 5.52 5.08 4.46 3.72 2.88 2.00 1.10 0.23 -0.59];

Tutorial: MATLAB

Example code

% asusume evenly spaced h
h=0.2;
vel = diff(y)./h;
tv=t(1:length(vel));
acc = diff(y,2)./h^2;
ta=t(1:length(acc));

Exercise (25 min)

Download the tutorial source file and fill in the blanks.

Run the code and validate your answer


Tutorial: C-Programming

Create a new folder under \tutorial Directory and name it as differentiation

  • e.g ) C:\Users\yourID\source\repos\NP\tutorial\TU_differentiation

Create a new empty project in Visual Studio Community. Name the project as TU_Differentiation

Create a new C/C++ source file for main()

  • Name the source file as TU_Differentiation_main.cpp

Paste from the following code

TU_differentiation_student.cpp

Exercise 1 : Differentiation from Discrete Points (25 min)

Create a C-program function for numerical differentiation from a set of discrete data. Read the instruction in the source code.

For m dataset, x[0] to x[m-1]

  • 2-Point forward difference: for the first point x[0] to [m-2]

  • 2-Point backward difference: for the last point x[m-1]

The gradient1D function should be defined in your library header files, located in \include folder

  • function definitions: myNP_yourID.h

  • function declaration: myNP_yourID.cpp

Review how to use 1D arrays in C-Programming. See tutorial here

Exercise 2 : Differentiation from a function (25 min)

Define a function named as myFunc() that defines the user's equation F(x).

For this tutorial, use y=x^3 , for x=0:0.2:4

Create a function named as gradientFunc() that returns the gradient of the user's equation.

  • It should receive myFunc() as the input argument.

  • It generates a discrete dataset of the user's F(x)

  • This function should return a 1D array of dydx[] .

This function should be defined in your library header files, located in \include folder

  • function definitions: myNP_yourID.h

  • function declaration: myNP_yourID.cpp

Validate the result

Last updated

Was this helpful?