Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Cosine between two segment lines

Subject: Cosine between two segment lines

From: Liana

Date: 22 Jun, 2011 07:17:02

Message: 1 of 3

Hi!

My question is the following. There are two segment lines defined by two data sets. I need to know the deviation of the first curve from the second curve. For this purpose I suppose to use cosine angle. I want to go through the segment lines and estimate cosine angle step-by-step in case of any deviation. My code is the following:

data_set1 = [1 1 2; 2 3 4; 2 1 1];
data_set2 = [1 1 2; 2 4 4; 2 2 1];
Cosine = dot(data_set1',data_set2')/(norm(data_set1')*norm(data_set2'));
CosineInDegrees = acos(Cosine)*180/pi;
Avg = sum(CosineInDegrees)/length(CosineInDegrees);

Please give me a feedback if this is the correct and most efficient solution of the problem.

Thanks.

Subject: Cosine between two segment lines

From: Rune Allnor

Date: 22 Jun, 2011 07:37:33

Message: 2 of 3

On Jun 22, 9:17 am, "Liana " <liananapalk...@email.arizona.edu> wrote:
> Hi!
>
> My question is the following. There are two segment lines defined by two data sets. I need to know the deviation of the first curve from the second curve. For this purpose I suppose to use cosine angle. I want to go through the segment lines and estimate cosine angle step-by-step in case of any deviation. My code is the following:
>
> data_set1 = [1 1 2; 2 3 4; 2 1 1];
> data_set2 = [1 1 2; 2 4 4; 2 2 1];
> Cosine = dot(data_set1',data_set2')/(norm(data_set1')*norm(data_set2'));
> CosineInDegrees = acos(Cosine)*180/pi;
> Avg = sum(CosineInDegrees)/length(CosineInDegrees);
>
> Please give me a feedback if this is the correct and most efficient solution of the problem.

To check if this is correct, use a simple example
as starting point (e.g. vectors in the (x,y) plane)
and work out the solution by hand. If the results
from the hand computations and your program don't
match, ther is a problem somewhere. (NB: The converse
is *not* true!)

Rune

Subject: Cosine between two segment lines

From: Roger Stafford

Date: 22 Jun, 2011 18:03:02

Message: 3 of 3

"Liana" wrote in message <its4te$hap$1@newscl01ah.mathworks.com>...
> Hi!
>
> My question is the following. There are two segment lines defined by two data sets. I need to know the deviation of the first curve from the second curve. For this purpose I suppose to use cosine angle. I want to go through the segment lines and estimate cosine angle step-by-step in case of any deviation. My code is the following:
>
> data_set1 = [1 1 2; 2 3 4; 2 1 1];
> data_set2 = [1 1 2; 2 4 4; 2 2 1];
> Cosine = dot(data_set1',data_set2')/(norm(data_set1')*norm(data_set2'));
> CosineInDegrees = acos(Cosine)*180/pi;
> Avg = sum(CosineInDegrees)/length(CosineInDegrees);
>
> Please give me a feedback if this is the correct and most efficient solution of the problem.
>
> Thanks.
- - - - - - - - -
1. It isn't clear what your data sets represent. Do the rows give the coordinates of endpoints of segments or are they incremental differences between segment endpoints? If the former, your examples represent very jagged "curves".

2. Your 'norm' computations act on matrices and do not give what you apparently expect of them. You had better do a careful study of the 'norm' documentation. Also when you make this correction you will have to use the './' operator to normalize the dot products.

3. In your averaging you apparently do not take into consideration the lengths of line segments. That doesn't seem reasonable. A tiny line segment would be given full weighting in the average.

4. There is a more robust way of computing angles than the 'acos' function which uses 'atan2'. Angles very close to zero or pi/2 produce inaccuracies in 'acos'. Just look at a plot of 'acos' to see that.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us