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

Write and test a function that computes the inner product of two vectors (1-dime

ID: 3816050 • Letter: W

Question

Write and test a function that computes the inner product of two vectors (1-dimensional arrays of numbers). The inner product is defined to be That is, the inner product is the sum of the element-wise product of the two vectors x and y. For example, if x = [7 4 6] and y= [4 1 8], then the inner product x of and y is Your function should be able to handle input vectors of any size (both must be the same length). To test your function, write a script that calls the function with 2 vectors for which you have hand-calculated the inner product. Then, compare the result of the function to your hand calculation. If the output is correct (matches your hand calculation), then output a message indicating that the test passed). If the output is incorrect (does not match your hand calculation), then output a message indicating that the test failed. For bonus cool-points, also indicate what the inputs were, what the expected output was, and what the actual output was.

Explanation / Answer

function [result err] = compute_inner_prod(X, Y)
    err = false;
    result = 0.0;  
    sizeX = length(X);
    sizeY = length(Y);
  
    if (sizeX != sizeY)
        fprintf("Error : Matirces are of different size ");
        err = true;
        return;
    end
  
    for i = 1:sizeX
        result = result + (X(i) * Y(i));  
    end
end


[result err] = compute_inner_prod([1 2 3], [1 2 3]);
if (err == false)
    fprintf("Inner product is %.3f ",result);
end   

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