How to predict future position of vehicle (GPS data) by previous data

How to predict future position of vehicle (GPS data) by previous data. I have those values in .csv file. I need to predict future optimal point by using previous data.

 Accepted Answer

With the information given, we are justified in guessing that the vehicles are motorized stilts. That assumption made, we can re-interpret the latitude and longitude as being the centroids of a body, and then apply gait analysis techniques, and use the information so gained to predict future position.
Or we could guess that the vehicles are constrained by mechanical systems and that there is periodic motion. We could then do eigenvalue analysis to try to deduce the modes of the mechanical system in order to predict future behavior.
Or we could guess that the vehicles are part of a weight + springs system and do ODE analysis with a mass matrix.
Or we could do System Identification.
Or we could do neural network timeseries analysis.
Or we could just guess that really all we need is about the last 4 readings, and use those to calculate current velocity and acceleration and assume that it will stay constant for the period for which prediction is to be done. This approach is probably the only one that can be really justified.

22 Comments

Or we could just guess that really all we need is about the last 4 readings, and use those to calculate current velocity and acceleration and assume that it will stay constant for the period for which prediction is to be done. This approach is probably the only one that can be really justified.
How this can be achieved for given problem statement?
https://in.mathworks.com/help/curvefit/polynomial.html I found this link, How to implement for .csv files data.
csvread(). Throw away all but the last few points. Those are position. diff() of position gives velocity. diff() of velocity gives acceleration. Now you can set up the standard equation,
p_projected = p0 + v * t + 1/2 * a * t^2
p0 would be the last point in your data, v would be the diff() between the last and second last, a would be the second difference
But the position has two co-ordinates (x,y) latitude and longitude. How to substitute in this equation.
I need to calculate the p_projected, save onto the csv file and plot the p_projected points in some other form like color to differentiate past and predicted.
latlong1 = csvread('FirstFile.csv');
latlong2 = csvread('SecondFile.csv');
lat1 = latlong1(:,1);
lon1 = latlong1(:,2);
lat2 = latlong2(:,1);
lon2 = latlong2(:,2);
This was shared by you to read .csv file. Can I use this to read data? I need to predict the future points by the same data. I want to run the p_projected in a loop from two .csv files. If two predicted points are equal from both the files(or if they are intersecting) then alert should be given.
beep; msgbox('Danger, Will Robinson!')
"But the position has two co-ordinates"
You can do the projection separately for lat and long.
"Can I use this to read data?"
Yes.
"If two predict points are same then alert should be given."
It will be quite rare that the two predicted points will be equal for any integral timestep. Remember, when you compare positions, you would be comparing down to about 10 angstrom in latitude, twice that in longitude. That is at the boundary between ultraviolet and x-ray frequencies. Your GPS data is not anywhere near accurate enough to make predictions down to that small of a distance.
Given a few readings, you can determine whether they are moving apart from each other, or moving in parallel, or moving toward each other; in the case of moving toward each other, you could calculate how long until intersection. The calculations do get more complicated if you need to take the curvature of the Earth into account.
For the given inputs by me, the output is attached. The plot of both file intersects. If not the equal lat and long values. If there future predicted points coincide(intersect) the alert message should be given
For the given inputs by me, the output is attached. The plot of both file intersects. If not the equal lat and long values. If there future predicted points coincide(intersect) the alert message should be given
In this attached it predicts the 3 points. Similar way can we do the same for two curves? The future points displayed "I I I ". If it coincides with other curve, alert is produced. Mathworks
In this attached it predicts the 3 points. Similar way can we do the same for two curves? The future points displayed "I I I ". If it coincides with other curve, alert is produced. Mathworks
p_projected = p0 + v * t + 1/2 * a * t^2 This is equation of motion?
You cannot use polyfit:
latlong1 = csvread('FirstFile.csv', 1, 0);
latlong2 = csvread('SecondFile.csv', 1, 0);
lat1 = latlong1(:,1);
lon1 = latlong1(:,2);
lat2 = latlong2(:,1);
lon2 = latlong2(:,2);
coeffs1 = polyfit(lat1, lon1, 3); %and ignore the warning
coeffs2 = polyfit(lat2, lon2, 3); %and ignore the warning
intersect_poly = coeffs1 - coeffs2;
intersect_lat = roots(intersect_poly);
intersect_lat(imag(intersect_lat) ~= 0) = []; %remove imaginary intersections
intersect_lon = polyval(coeffs1, intersect_lat);
scatter(lat1, lon1, 'r*');
hold on
scatter(lat2, lon2, 'gs');
scatter(intersect_lat,intersect_lon, 'b^')
How to read .csv file for this code?
%Can I use this?
latlong1 = csvread('FirstFile.csv');
latlong2 = csvread('SecondFile.csv');
No, you cannot use that for files that have header lines. I already posted the small change that you need (it might require R2014a or later)
I am using Matlab 2013. I am getting error Error using dlmread (line 139) Badly formed format string.
Error in csvread (line 48) m=dlmread(filename, ',', r, c);
Error in walterm (line 1) latlong1 = csvread('FirstFile.csv', 1, 0);
For R2013* you will need to switch to using textscan()
fid = fopen('FirstFile.csv', 'rt');
latlong1 = cell2mat( textscan(fid, '%f,%f', 'HeaderLines', 1, 'CollectOutput', 1) );
fclose(fid);
fid = fopen('SecondFile.csv', 'rt');
latlong2 = cell2mat( textscan(fid, '%f,%f', 'HeaderLines', 1, 'CollectOutput', 1) );
fclose(fid);
Warning: Polynomial is not unique; degree >= number of data points. > In polyfit at 71 In code at 12 Warning: Polynomial is not unique; degree >= number of data points. > In polyfit at 71 In code at 13 >>
fid = fopen('FirstFile.csv', 'rt');
latlong1 = cell2mat( textscan(fid, '%f,%f', 'HeaderLines', 1, 'CollectOutput', 1) );
fclose(fid);
if size(latlong1,1) < 3
error('FirstFile is too small to fit, contains only %d samples', size(latlong1,1);
end
fid = fopen('SecondFile.csv', 'rt');
latlong2 = cell2mat( textscan(fid, '%f,%f', 'HeaderLines', 1, 'CollectOutput', 1) );
fclose(fid);
if size(latlong2,1) < 3
error('SecondFile is too small to fit, contains only %d samples', size(latlong2,1);
end
lat1 = latlong1(:,1);
lon1 = latlong1(:,2);
lat2 = latlong2(:,1);
lon2 = latlong2(:,2);
degree = max(3, min(length(lat1,1),length(lat2,1))-1);
if degree < 3
fprintf(2, 'Warning: One of the files has less than 4 samples, dropping down to degree 2 fitting');
end
coeffs1 = polyfit(lat1, lon1, degree); %and ignore the warning
coeffs2 = polyfit(lat2, lon2, degree); %and ignore the warning
intersect_poly = coeffs1 - coeffs2;
intersect_lat = roots(intersect_poly);
intersect_lat(imag(intersect_lat) ~= 0) = []; %remove imaginary intersections
intersect_lon = polyval(coeffs1, intersect_lat);
scatter(lat1, lon1, 'r*');
hold on
scatter(lat2, lon2, 'gs');
scatter(intersect_lat,intersect_lon, 'b^')
For how many inputs(lat, long) does it considers for compilation? Can we also plot on google map?
The following will try to calculate an intersection down to single samples.
fid = fopen('FirstFile.csv', 'rt');
latlong1 = cell2mat( textscan(fid, '%f,%f', 'HeaderLines', 1, 'CollectOutput', 1) );
fclose(fid);
if isempty(latlong1)
error('FirstFile is empty, cannot fit')
end
fid = fopen('SecondFile.csv', 'rt');
latlong2 = cell2mat( textscan(fid, '%f,%f', 'HeaderLines', 1, 'CollectOutput', 1) );
fclose(fid);
if isempty(latlong2)
error('SecondFile empty, cannot fit');
end
lat1 = latlong1(:,1);
lon1 = latlong1(:,2);
lat2 = latlong2(:,1);
lon2 = latlong2(:,2);
degree = max(3, min(length(lat1,1),length(lat2,1))-1);
if degree < 3
fprintf(2, 'Warning: One of the files has less than 4 samples, dropping down to degree %d fitting', degree);
end
coeffs1 = polyfit(lat1, lon1, degree); %and ignore the warning
coeffs2 = polyfit(lat2, lon2, degree); %and ignore the warning
intersect_poly = coeffs1 - coeffs2;
intersect_lat = roots(intersect_poly);
intersect_lat(imag(intersect_lat) ~= 0) = []; %remove imaginary intersections
if isempty(intersect_lat) && degree == 0 && abs(intersect_poly) < 1e-4
intersect_lat = lat1;
intersect_long - lon1;
else
intersect_lon = polyval(coeffs1, intersect_lat);
end
if isempty(intersect_lon)
error('paths do not intersect');
end
scatter(lat1, lon1, 'r*');
hold on
scatter(lat2, lon2, 'gs');
scatter(intersect_lat,intersect_lon, 'b^')
"Can we also plot on google map?"

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!