Question 4: The figure below shows a Sharp GP2D12 infrared distance sensor and a BasicX-24 microprocessor. The distance sensor uses a beam of infrared light to measure the distance from the sensor to an object. The sensor provides an output voltage that has a fairly complicated relationship to this distance. The BasicX processor converts the voltage from the sensor into a number between zero and one. Let us denote this number as x, and the distance (measured in inches) between the sensor and object as d. The relationship between x and d is as follows:
34.635 * (1 - x) ^ 2.54
Write a MATLAB code/script computing the value of d for the values of x and returning a matrix named Solution A with the number of rows equal to the sum of the digits in your student number and two columns: 15p and a plot with a title (Distance Sensor) and axes labels (Voltage and Distance) like the examples below (10p).
Solution_A:
Inr 0.0227 59.8575
Infed Distance Sensor
BasicX Processor
0.0909 147.9402
0.1136 117.9457
Pseudocode:
1. Define the range of x values.
2. Create an empty matrix named Solution_A.
3. Iterate through each x value.
4. Compute the corresponding d value using the given relationship.
5. Append the x and d values to Solution_A.
6. Plot the x and d values with a title and axes labels.
MATLAB code:
x_values = [0.0909, 0.1136];
Solution_A = zeros(length(x_values), 2);
for i = 1:length(x_values)
x = x_values(i);
d = 34.635 * (1 - x) ^ 2.54;
Solution_A(i, :) = [x, d];
end
plot(Solution_A(:, 1), Solution_A(:, 2));
title('Distance Sensor');
xlabel('Voltage');
ylabel('Distance');