As per integral calculus, the length of a continuous and differentiable curve f(x) from x=a to x=b is given by
Now how do we find the length of a curve in MATLAB.
Let us do this via an example. Assume one asked you to find the length of from Π to 2Π. In the book, How People Learn, the authors mention that learning a concept in multiple contexts prolongs retention. Although it may not be the context that the authors of the book are talking about, let us find the length of the curve multiple ways within MATLAB. Try the program for functions and limits of your own choice to evaluate the difference.
METHOD 1: Use the formula S= by using the diff and int function of MATLAB
METHOD 2: Generate several points between a and b, and join straight lines between consecutive data points. Add the length of these straight lines to find the length of the curve.
METHOD 3. Find the derivative dy/dx numerically using forward divided difference scheme, and then use trapezoidal rule (trapz command in MATLAB) for discrete data with unequal segments to find the length of the curve.
QUESTIONS TO BE ANSWERED:
- Why does METHOD 3 giving inaccurate results? Can you make them better by using better approximations of derivative like central divided difference scheme?
- Redo the problem with f(x)= with a=1 and b=4 as the exact length can be found for such a function.
% Simulation : Find length of a given Curve
% Language : Matlab 2007a
% Authors : Autar Kaw
% Last Revised : June 14 2008
% Abstract: We are finding the length of the curve by three different ways
% 1. Using the formula from calculus
% 2. Breaking the curve into bunch of small straight lines
% 3. Finding dy/dx of the formula numerically to use discrete function
disp(‘We are finding the length of the curve by three different ways’)
disp(‘1. Using the formula from calculus’)
disp(‘2. Breaking the curve into bunch of small straight lines’)
disp(‘3. Finding dy/dx of the formula numerically to use discrete function integration’)
%INPUTS – this is where you will change input data if you are doing
% a different problem
% Define the function
% lower limit
% b=upper limit
% n = number of straight lines used to approximate f(x) for METHOD 2
%p = number of discrete data points where dy/dx is calculated for METHOD 3
% METHOD 1. Using the calculus formula
% finding dy/dx
% applying the formula
fprintf (‘\nExact length =%g’,leng_exact)
% METHOD 2. Breaking the curve as if it is made of small length
% straight lines
% Generating n x-points from a to b
% generating the y-values of the function
% assuming that between consecutive data points, the
% curve can be approximated by linear splines.
% there are m-1 splines for m points
fprintf (‘\n\nBreaking the line into short lengths =%g’,leng_straight)
% METHOD 3. Same as METHOD1, but calculating dy/dx
% numerically and integrating using trapz
% generating the dy/dx-values
% derivative at last point using Backward divided difference formula
% is same as Forward divided difference formula
disp (‘Using numerical value of dy/dx coupled’)
disp (‘with discrete integration’)
fprintf (‘ =%g’,length_fdd)
This post is brought to you by Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at http://numericalmethods.eng.usf.edu