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

Create an m-file function that takes two input parameters, which are vectors of

ID: 1858381 • Letter: C

Question

Create an m-file function that takes two input parameters, which are vectors of the same length. The

output of the function is the inner product (dot product) of the two vectors. Use a loop structure

instead of the array multiplication (.*) and the function sum. Test the function.

A=[1 2 3];,B=[4 5 6]';

m= size(A,1); % gets the row size of matrix A

p= size(A,2); % gets the column size of matrix A

n= size(B,2); % gets the column size of matrix B

f= size(B,1); % gets the row size of matrix B

if f==p

C=zeros(m,n); % initializes the variable C

fori=1:m % starts the loop

forj=1:n % starts the loop

C(i,j)=C(i,j) + A(i,:) * B(:,j); % updates C

end % ends loop

end % ends loop

ans = C; % prints the final answer C

else

error('The dimensions of the matrices must agree.')

end

D=A+B';

fprintf('dot product ans =M ,sum of vector = M ',C,D);

Explanation / Answer

THIS WILL HELP YOU


Since many of the built-in functions take vectors as arguments, it should come as no surprise that you can write functions that take vectors. Here’s a simple (silly) example:

There’s nothing special about this function at all. The only difference from the scalar functions we’ve seen is that I used a capital letter to remind me that X is a vector.

This is another example of a function that doesn’t actually have a return value; it just displays the value of the input variable:

Here’s a more interesting example that encapsulates the code from Section 4.12 that adds up the elements of a vector:

I called it mysum to avoid a collision with the built-in function sum, which does pretty much the same thing.

Here’s how you call it from the Command Window:

Because this function has a return value, I made a point of assigning it to a variable.

There’s also nothing wrong with assigning a vector to an output variable. Here’s an example that encapsulates the code from Section 4.13:

Ideally I would have changed the name of the output variable to Res, as a reminder that it is supposed to get a vector value, but I didn’t.

Here’s how myapply works:

Functions that work on vectors will almost always work on scalars as well, because MATLAB considers a scalar to be a vector with length 1.

Unfortunately, the converse is not always true. If you write a function with scalar inputs in mind, it might not work on vectors.

But it might! If the operators and functions you use in the body of your function work on vectors, then your function will probably work on vectors.

For example, here is the very first function we wrote:

And lo! It turns out to work on vectors:

At this point, I want to take a minute to acknowledge that I have been a little harsh in my presentation of MATLAB, because there are a number of features that I think make life harder than it needs to be for beginners. But here, finally, we are seeing features that show MATLAB’s strengths.

Some of the other functions we wrote don’t work on vectors, but they can be patched up with just a little effort. For example, here’s hypotenuse from Section 5.5:

This doesn’t work on vectors because the ^ operator tries to do matrix exponentiation, which only works on square matrices.

But if you replace ^ with the elementwise operator .^, it works!

In this case, it matches up corresponding elements from the two input vectors, so the elements of Care the hypotenuses of the pairs (3,4), (5,12) and (8,15), respectively.

In general, if you write a function using only elementwise operators and functions that work on vectors, then the new function will also work on vectors.

Another common vector operation is cumulative sum, which takes a vector as an input and computes a new vector that contains all of the partial sums of the original. In math notation, if V is the original vector, then the elements of the cumulative sum, C, are:

In other words, the ith element of C is the sum of the first i elements from V. MATLAB provides a function named cumsum that computes cumulative sums:

The inverse operation of cumsum is diff, which computes the difference between successive elements of the input vector.

Notice that the output vector is shorter by one than the input vector. As a result, MATLAB’s version of diff is not exactly the inverse of cumsum. If it were, then we would expectcumsum(diff(X)) to be X:

But it isn’t.

The multiplicative version of cumsum is cumprod, which computes the cumulative product. In math notation, that’s:

In MATLAB, that looks like:

MATLAB doesn’t provide the multiplicative version of diff, which would be called ratio, and which would compute the ratio of successive elements of the input vector.

You can use a loop, or if you want to be clever, you can take advantage of the fact that elna + lnb = a b.

If you apply myratio to a vector that contains Fibonacci numbers, you can confirm that the ratio of successive elements converges on the golden ratio, (1+√5)/2 (see Exercise 4.6).

It is often useful to check the elements of a vector to see if there are any that satisfy a condition. For example, you might want to know if there are any positive elements. In logic, this condition is calledexistential quantification, and it is denoted with the symbol ∃, which is pronounced “there exists.†For example, this expression

means, “there exists some element x in the set S such that x>0.†In MATLAB it is natural to express this idea with a logical function, like exists, that returns 1 if there is such an element and 0 if there is not.

Ci = i ∑ j=1 Vj
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