How To Calculate Max Deviation On A Line

Asked by Kent on 29 Jun 2012
Latest activity Commented on by Kent on 29 Jun 2012

Hi there! I have a question regarding how to calculate maximum deviation on a line.

Given that I have one line (call it Line A), I'm interested in finding the MAX value at 1<POSITION<10 and MIN value at 50<POSITION<60, and then formed a line by this two points(call it Line B).

Next I would like to do is to calculate the max deviation (or distance) from Line A to the lowest point of Line B and output the value.

I've tried but kinda stuck at how to proceed with forming the lines from 2 points then calculating the max deviation from this lines to another line. Hope to get some input from you guys.

```Position,deltaP
1,0.79672217
2,0.7967096689
3,0.7967104044
4,0.7967030204
5,0.79669891
6,0.79670164
7,0.7966935311
8,0.7966885096
9,0.7966867596
10,0.7966908044
11,0.7966915659
12,0.7967002459
13,0.7966929348
14,0.7966808326
15,0.79668317
16,0.7966720107
17,0.7966774959
18,0.7966798385
19,0.7966586178
20,0.7966574996
21,0.7966417311
22,0.7966176296
23,0.7966008181
24,0.79657083
25,0.79653591
26,0.79651816
27,0.796498
28,0.79648868
29,0.796482303
30,0.79648864
31,0.7964957001
32,0.7965104026
33,0.79653118
34,0.79655322
35,0.79656086
36,0.7965753686
37,0.79657933
38,0.7965895335
39,0.796610771
40,0.7966209815
41,0.7966409801
42,0.7966490117
43,0.7966384365
44,0.7966236063
45,0.7966218648
46,0.7966473956
47,0.7966660522
48,0.7966582037
49,0.7966641863
50,0.7966611456
51,0.79665338
52,0.7966786663
53,0.7966801211
54,0.7966836322
55,0.7966905467
56,0.7966988863
57,0.79669877
58,0.7967126519
59,0.79669836
60,0.7967066396
```

P/S: I can send over the .ppt file for better illustrates about LineA and LineB. Please let me know your email.

~Kent

Products

No products are associated with this question.

Hi Kent, here's a quick solution I worked out. Assume you have your first column of data saved as 'x' and your second as 'y'.

```% find max/min points and their location
y1 = max(y(1:10));
idx1 = find(y==y1);
y2 = min(y(50:60));
idx2 = find(y==y2);
% plot new line
hold on
yp = (y2-y1)/(x(idx2)-x(idx1))*(x-x(idx1))+y1;
plot(x,yp,'r')
% max deviation and location
maxdiff = max(abs(yp(idx1:idx2)-y(idx1:idx2)))
idx_maxdiff = find((yp-y)==maxdiff)
```

The maxdiff is the maximum difference between the line and your data, and the idx_maxdiff is the index location in the vector where the maxdiff was found.

Kent on 29 Jun 2012

Hi Adam, thank you very much! I tried the code and it actually works pretty good. However, when trying to find the location of maximum difference (idx_maxdiff), it outputs

idx_deltamax =

`   Empty matrix: 0-by-1`

I think it is because (yp-y) returns much smaller values and maxdiff has much larger value which doesn't coincide on the line. I'm not sure if this is true and would like to seek your opinion to work around this.

I've another question related to this which is to level or pivot the yp line and the Line A so that it becomes flat/parallel with the x-axis, before calculating for the maxdiff. Is there a line pivoting function in MATLAB that allows this kind of line pivoting?

~Kent

Adam Filion on 29 Jun 2012

Hi Kent, for your first question, this could be a mistake on my part. If you find the absolute difference in maxdiff, then you need to use abs again in finding idx_maxdiff otherwise the signs might not match. However when I was working with the data you provided, the code worked on my end. To fix that oversight change the following line

```idx_maxdiff = find(abs(yp-y)==maxdiff)
```

Secondly, there is no built-in function in MATLAB to ‘pivot’ a line. However if you know the point you want to pivot around, then it is relatively simple. Let’s say you want to make Line B parallel to the x-axis at the max point you found in the first range of values. Then you can try:

```yp=ones(size(y))*y(idx1);
plot(x,yp,'r')
```
Kent on 29 Jun 2012

Hi Adam, regarding my first question, it was actually a mistake I made in the code that causes outputs "Empty Matrix". I rechecked my code and found that I had a typo in the maxdiff equation, y(idx1:idx2) became x(idx1:idx2), need to get my eyes check! But thanks for your help!

Regarding my 2nd question about pivoting, I will try that and let you know. Thanks!

~Kent