Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using Matlab You are driving along a North-South road in this town. Specifically

ID: 3680904 • Letter: U

Question

Using Matlab

You are driving along a North-South road in this town. Specifically, the location of your car as you are driving along is (0.2, x), where x varies from 0 to 1. Please plot the distance to the closest gas station as a function of your location x along the road. Again, please use the findclosest function.

this is my findclosest function

function mindistance=findclosest(pointset,testpoint)
a=pointset(:,1)-testpoint(1,1);
b=pointset(:,2)-testpoint(1,2);
distance=sqrt(a.^2+b.^2);
mindistance=min(distance);
end

I don't know how to plot the distance to the closest gas station as a function

Explanation / Answer

Ok. As per problem statement, we are driving along a north-south road in the town. Specific location of our car is (0.2, x) where x varies from 0 to 1.

Now, we need to plot the distance to the closest gas station as function of our location x along the road.

So, that tells us that the x-axis of the plot would contain the location values i.e. x ranging from 0 to 1. Whereas y-axis will have the corresponding distance to the closest gas station.

Now, you are saying, we need to use your function findclosest to find the closest gas station. I see here that the findclosest function take two arguments - pointset and testpoint. testpoint, it seems will be the argument which will hold our current location. But, pointset here, I presume is the set of locations of gas stations.

So, you have not provided the set of locations of gas stations.

I am writing code in matlab where you can change your set of gas stations.

%Script to plot the distance to closest gas stations as a function of x
%along the road

clc
clear
close all

gasStations = [0.1 0.3; 0.4 0.9];   %You can specify your own stations here

p = 0;
for x = 0:0.1:1
    p = p+1;
    cgs(p) = findclosest(gasStations, [0.2, x]);
end

figure
plot((0:0.1:1), cgs, '-o');
title('Distance Plot')
xlabel('x')
ylabel('Distance to closest gas station')

Please upvote the answer if you find it helpful.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote