How to find the area between two lines of different size matrices and fill that area?

2 views (last 30 days)
I have two lines that im plotting for a vehicles location. One is the intended path I wanted the vehicle to travel, its a perfectly straight line, the other is the path the vehicle actually took. Its close but not perfect. My end goal is to find how much the vehicle actual path deviated from the intended path as a proxy for navigational accuracy. My idea is that I could calculate the area between the two lines and use that as an indicator for how accuratly the vehicle followed its intended path. When I do this calculation I also want to highlight the difference between the two lines in the graph. This is all part of a much larger program intended for use by the people operating these vehicles. Below is how im plotting the position of both the vehicle and the planned path, I also included a jpg of what the path looks like compared to the intened path.
Ive looked into this and the error I keep getting is:
Specify the coordinates as matrices of the same size or as vectors with the same number of elements.
The problem is, there is only 71 points for the track path and 508486 points for vehicle position, I tried interpolating the track path to make the matrices the same size but I get NaN values for everything after the 71 points so im not sure why thats not working.
I tried patch and fill and got the same error with both. Below is my code:
P = uigetdir('C:\');
S1 = dir(fullfile(P,'AHR2.csv'));
S3 = dir(fullfile(P,'CMD.csv'));
F = fullfile(S3.folder,S3.name);
M = readmatrix(F);
F1 = fullfile(S1(k).folder,S1(k).name);
M1 = readmatrix(F1);
TrackLat1 = M(:,10);
TrackLong1 = M(:,11);
Lat2 = M1(:,7);
Long2 = M1(:,8);
TrackLatLong = [TrackLat1, TrackLong1];
[MM, ~, ~] = rmoutliers(TrackLatLong, 1);
TrackLat = MM(:,1);
TrackLong = MM(:,2);
LatLong = [Lat2, Long2];
[MM1, ~, ~] = rmoutliers(LatLong, 1);
Lat1 = MM1(:,1);
Long1 = MM1(:,2);
plot(Long1, Lat1)
hold on
plot(TrackLong, TrackLat)
hold on
X = [Long1;Lat1];
Y = [TrackLong; TrackLat];
patch([X fliplr(X)], [Y fliplr(Y)], 'red')
title 'FAT Position'
legend('USV Position', 'Mission Plan')
xlim tight
ylim tight
ylabel 'Latitude'
xlabel 'Longitude'
attached is a screen shot of the plot plus a zoomed in section because you might not be able to tell. Thanks in advance for any help I might recieve, if any one has any ideas on a simpler way to do this please let me know!
  1 Comment
Stephen23
Stephen23 on 8 Jan 2025
Edited: Stephen23 on 8 Jan 2025
"I tried interpolating the track path to make the matrices the same size but I get NaN values for everything after the 71 points so im not sure why thats not working."
It sounds as if the x-query points were not defined over the entire range of the input x-sample points.
Please show the code you used in your attempt to interpolate the data.

Sign in to comment.

Answers (2)

Jacob Mathew
Jacob Mathew on 8 Jan 2025
Hey Bradley,
Could you share some sample data as well as how you tried to extrapolate the data? The following example demos one line having much less coordinates than the other but then using the interp1 function to generate the necessary interpolated data to calculate the area using the trapz function:
% Line with 100 points
x1 = linspace(0, 10, 100);
y1 = 2 * x1 + 1;
% Line with 10 points
x2 = linspace(0, 10, 10);
y2 = 2 * x2 + 3 + randn(size(x2)); % Creates gaps to measure area
% Interpolate y2 to have the same number of points as y1 for integration
y2_interp = interp1(x2, y2, x1, 'linear');
% Plot the lines
figure;
plot(x1, y1, 'b-', 'LineWidth', 2); % First line in blue
hold on;
plot(x2, y2, 'ro', 'MarkerSize', 8, 'MarkerFaceColor', 'r'); % Second line points in red
plot(x1, y2_interp, 'r--', 'LineWidth', 2); % Interpolated second line
legend('Line 1', 'Line 2 (random)', 'Interpolated Line 2');
xlabel('X');
ylabel('Y');
title('Area between 2 lines');
grid on;
hold off;
% Calculate the area between the lines using numerical integration
area_between_lines = trapz(x1, abs(y1 - y2_interp));
fprintf('The area between the lines with randomness is: %.2f\n', area_between_lines);
The area between the lines with randomness is: 24.12
You can refer to the documentation of interp1 and trapz at the link below:

Star Strider
Star Strider on 8 Jan 2025
It would be helpful to have your data.
That aside, the problem with your patch calls is that your data are column vectors and your patch calls are designed to work with row vectors:
patch([X fliplr(X)], [Y fliplr(Y)], 'red')
Change the patch call to:
patch([X; flip(X)], [Y; flip(Y)], 'red')
using vertical concatenation rather than horizontal conncatenatiion, and noting that fliplr with a column vector returns the same column vector unflipped, so use either flip or flipud to flip column vectors, and it should work.
.

Categories

Find more on Spline Postprocessing in Help Center and File Exchange

Tags

Products


Release

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!