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

Requirements: i) Submit homework solutions in the form a technical document deta

ID: 3405316 • Letter: R

Question

Requirements: i) Submit homework solutions in the form a technical document detailing your procedure for solving the following problems. Include in the same file (for example, in the Appendix) the MATLAB code. (ii) Please include separate MATLAB files that includes your MATLAB scripts and functions. Use comments in your MATLAB script file to describe your procedure and the solution you are presenting. Reminder to make use of the MATLAB documentation (and the internet) for help on specific functions or tasks. ii) Please submit your homework together in one zip file iii) Please submit your homework together in one zip file.

Explanation / Answer

MATLAB function for the problem is as below:

function ind=findNearest(x, desiredVal)

%absolute length of desiredVal is calculated from each element of matrix/vector
for i=1:length(x)
lenx(i)=abs(x(i)-desiredVal);
end

%minimum absolute length
minlen=min(lenx);

%index of minimum absolute length in absolute length matrix
ind=find(lenx==minlen);

end

MATLAB script for getting result from the function is as below:

a) For x as vector

x = [1; 5; 2; 7; 3; 4];

desiredVal = 9;

findNearest(x, desiredVal)

Output of the script is below:

4

b) For x as matrix

x = [1 5 2 7 3 4];

desiredVal = 2.5;

findNearest(x, desiredVal)

Output of the script is below:

3, 5

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