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

(A taste of vectorization) Loops can be written in MATLAB, but they are NOT the

ID: 2247058 • Letter: #

Question

(A taste of vectorization) Loops can be written in MATLAB, but they are NOT the most efficient way to get things done. It's better to avoid loops and use the colon notation instead. The following code has a loop that computes values of the cosine function. The index of yy () must start at 1.) Rewrite this computation without using the loop (follow the style in the previous part). yy = []: % leftarrow initialize the yy vector to be empty for k = -40: 40 yy(k + 41) = exp (-(k/10)*(k/20)): end plot(-40: 40, yy) Explain why it is necessary to write yy(k + 41). What happens if you use yy(k) instead? Also, explain the labels on the x-axis. This functional form is called a Gaussian form. Record your explanations on the Verification Sheet.

Explanation / Answer

We can avoid foor loop here. The below given code does exacty th same as above

k = -40:40
yy = exp(-(k/10).*(k/20));
plot(-40:40, yy)

In MATLAB array indexing starts at 1. So index less than 1 are invalid. So yy(k) will give error when yy is an array and index is less than zero.

if we donot explicitly indicate the label, the label will be empty. And x-axis will contain only numbers to mark the grid lines