As per integral calculus, the length of a continuous and differentiable curve f(x) from x=a to x=b is given by

S=\int_a^b \sqrt{(1+(dy/dx)^2} dx

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 $latex x^2*sin(x) $ 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= \int_a^b \sqrt{(1+(dy/dx)^2} dx 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)= x^{\frac{3}{2}} 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

% integration

clc

clear all

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

syms x;

% Define the function

curve=x^2*sin(x)

% lower limit

a=pi

% b=upper limit

b=2*pi

% n = number of straight lines used to approximate f(x) for METHOD 2

n=100

%p = number of discrete data points where dy/dx is calculated for METHOD 3

p=100

% OUTPUTS

% METHOD 1. Using the calculus formula

% S=int(sqrt(1+dy/dx^2),a,b)

% finding dy/dx

poly_dif=diff(curve,x,1);

% applying the formula

integrand=sqrt(1+poly_dif^2);

leng_exact=double(int(integrand,x,a,b));

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

xi= a:(b-a)/n:b;

% generating the y-values of the function

yi=subs(curve,x,xi);

% assuming that between consecutive data points, the

% curve can be approximated by linear splines.

leng_straight=0;

m=length(xi);

% there are m-1 splines for m points

for i=1:1:m-1

dx=xi(i+1)-xi(i);

dy= yi(i+1)-yi(i);

leneach=sqrt(dx^2+dy^2);

leng_straight=leng_straight+leneach;

end

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

xi=a:(b-a)/p:b;

% generating the dy/dx-values

m=length(xi);

for i=1:1:m-1

numer=yi(i+1)-yi(i);

den=xi(i+1)-xi(i);

dydxv(i)=numer/den;

end

% derivative at last point using Backward divided difference formula

% is same as Forward divided difference formula

dydxv(m)=dydxv(m-1);

integrandi=sqrt(1+dydxv.*dydxv);

length_fdd=trapz(xi,integrandi);

disp(‘ ‘)

disp(‘ ‘)

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://nm.mathforcollege.com

Hi,

I am a mechanical undergrad that is introduced to matlab only very recently. I have a desperate question in need of assistance, and please treat me as a total beginner…

How do I find the length of a curve, when I have the arrays of both the x and y axis. That means, I already have all the (x,y) coordinates of the curve, and I could generate the curve in a matlab graph easily. However, it is crucial for me to achieve its length.

It seems to me method 2 as shown above is the best method for my situation, but “??? Undefined function or method ‘Calculating’ for input arguments of type ‘char’.” error keep surfacing… It should be error of my part…

Any chance you could be helping me out. And hope you have a great Christmas…

Regards,

Orlando

Hi,

I am a mechanical undergrad that is introduced to matlab only very recently. I have a desperate question in need of assistance, and please treat me as a total beginner…

How do I find the length of a curve, when I have the arrays of both the x and y axis. That means, I already have all the (x,y) coordinates of the curve, and I could generate the curve in a matlab graph easily. However, it is crucial for me to achieve its length.

It seems to me method 2 as shown above is the best method for my situation, but “??? Undefined function or method ‘Calculating’ for input arguments of type ‘char’.” error keep surfacing… It should be error of my part…

Any chance you could be helping me out. And hope you have a great Christmas…

Regards,

Orlando

Hi,

I am a mechanical undergrad that is introduced to matlab only very recently. I have a desperate question in need of assistance, and please treat me as a total beginner…

How do I find the length of a curve, when I have the arrays of both the x and y axis. That means, I already have all the (x,y) coordinates of the curve, and I could generate the curve in a matlab graph easily. However, it is crucial for me to achieve its length.

It seems to me method 2 as shown above is the best method for my situation, but “??? Undefined function or method ‘Calculating’ for input arguments of type ‘char’.” error keep surfacing… It should be error of my part

hey orlando..

just copy the program from the editor and paste in the comnmand window that should work..

hey orlando..

just copy the program from the editor and paste in the comnmand window that should work..

Hi.

Can I use trapezodial integration for double integration?

Thanks for your answer

Hi.

Can I use trapezodial integration for double integration?

Thanks for your answer

Yes, you can use Trapezoidal rule for double integration.

Hi everyone,

Just wanted to know if there is a way to do this…

I have a binary image, an I would like to find the min corresponding y coordinates for a given range , say x=120:200;

[y1]=find(image(:,x1));

y1=min(y1);

I can do it manually for up to 10 coordinates but is there any way i can do it for a range of x values.

I tried the following:

x = 120:380′;

[y1]=find(image(:,x));

y=max(y);

But I am getting an error every time…

Any suggestions would be greatly appreciacted.

Many thanks,

This will give you a rough value. However, you can always use splines on xi and yi to generate points closer to each other. See this link, otherwise see code below for the rough value

http://autarkaw.wordpress.com/2009/06/20/how-do-i-do-spline-interpolation-in-matlab/

clc

clear all

% Put x array values below

xi=[2 5 9]

% Put y array values below

yi=[ 3 45 78]

leng_straight=0;

m=length(xi);

% there are m-1 splines for m points

for i=1:1:m-1

dx=xi(i+1)-xi(i);

dy= yi(i+1)-yi(i);

leneach=sqrt(dx^2+dy^2);

leng_straight=leng_straight+leneach;

end

fprintf (‘\n\nBreaking the line into short lengths =%g’,leng_straight)

Hi everyone,

Just wanted to know if there is a way to do this…

I have a binary image, an I would like to find the min corresponding y coordinates for a given range , say x=120:200;

[y1]=find(image(:,x1));

y1=min(y1);

I can do it manually for up to 10 coordinates but is there any way i can do it for a range of x values.

I tried the following:

x = 120:380′;

[y1]=find(image(:,x));

y=max(y);

But I am getting an error every time…

Any suggestions would be greatly appreciacted.

Many thanks,

This will give you a rough value. However, you can always use splines on xi and yi to generate points closer to each other. See this link, otherwise see code below for the rough value

http://autarkaw.wordpress.com/2009/06/20/how-do-i-do-spline-interpolation-in-matlab/

clc

clear all

% Put x array values below

xi=[2 5 9]

% Put y array values below

yi=[ 3 45 78]

leng_straight=0;

m=length(xi);

% there are m-1 splines for m points

for i=1:1:m-1

dx=xi(i+1)-xi(i);

dy= yi(i+1)-yi(i);

leneach=sqrt(dx^2+dy^2);

leng_straight=leng_straight+leneach;

end

fprintf (‘\n\nBreaking the line into short lengths =%g’,leng_straight)

Remember if you cut and paste, the single quotes need to be deleted and entered again such as in the fprintf statement.

hallo,i am mechanical engineer.I want a program to determine thedifferent sizes of circular holes in the rectangular plate.It is required for digital image processing to verify the results and error determination by dimensional metrology.

hallo,i am mechanical engineer.I want a program to determine thedifferent sizes of circular holes in the rectangular plate.It is required for digital image processing to verify the results and error determination by dimensional metrology.

Dear sir,

I have to plot a graph of arc length vs curvature,where I have to define a level set function pi(x,y)=2-sqrt(x.^2+y.^2) and define an indicator function where 0 at fluid 1 and 1 at fluid2 and 0.5 at the interface.I have define them all. However,now İ have to plot arc length of the above defined interface vs curvature of the same.

please can you tell me how to do this in matlab.

Thank you.

Kumudu

Dear sir,

I have to plot a graph of arc length vs curvature,where I have to define a level set function pi(x,y)=2-sqrt(x.^2+y.^2) and define an indicator function where 0 at fluid 1 and 1 at fluid2 and 0.5 at the interface.I have define them all. However,now İ have to plot arc length of the above defined interface vs curvature of the same.

please can you tell me how to do this in matlab.

Thank you.

Kumudu

your work is much appreciated! Thanks..

your work is much appreciated! Thanks..