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

Write a function in Matlab that takes as input s and a number x and outputs the

ID: 3846849 • Letter: W

Question

Write a function in Matlab that takes as input s and a number x and outputs the floating point number of x using s-digit rounding. You may use Matlab’s “round” command to help you. Use only basic programming.

(a) Write out or print out your function

(b) Choose 2 random vectors of length 10, using the “rand” command, and compute the result of the dot product using your function under 4-digit rounding and compute the exact Matlab result and compute the absolute error. Do the same thing 4 more times and turn in the results.

Explanation / Answer

Matlab function sDigitRound.m Part (a)

function x = sDigitRound(s,x)
   x = round(x,s,'significant'); % Performing s-Digit rounding using Matlabs's round command
end

Matlab code for calling the function Part (b)

NumerOfTimes = 5; % Specify the number of times the following procedure have to repeat
s = 4; % for 4-digit rounding
fprintf('loop A.B without_rounding A.B with_rounding Absolute_Error ');
for k = 1: NumerOfTimes % loop to perform the procedure NumerOfTimes
    x1 = rand(1,10); % Creating the first random vector of length 10
    x2 = rand(1,10); % Creating the Second random vector of length 10
    y1 = sDigitRound(s,x1); % Calling the function to perform 4-digit rounding of x1
    y2 = sDigitRound(s,x2); % Calling the function to perform 4-digit rounding of x2
    z1 = x1*x2'; % Computing the dot product of vectors without using rounding
    z2 = y1*y2'; % Computing the dot product of vectors with using rounding
    Err = abs(z1-z2); % Computing the absolute error
    fprintf('%d %f %f %f ',k,z1,z2,Err);
end

OUTPUT

loop   A.B without_rounding   A.B with_rounding    Absolute_Error
1         2.985727                          2.985745          0.000019
2         2.305577                          2.305715                    0.000137
3         2.534370                          2.534231                    0.000139
4         2.071503                          2.071473                    0.000030
5   2.039450                          2.039481                    0.000031

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