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

Start with the seriesCos example (below) and modify it so that it works in the s

ID: 3562635 • Letter: S

Question

Start with the seriesCos example (below) and modify it so that it works in the same way as MATLAB's cos function. Name the function seriesCos2.

seriesCos example

function c = seriesCos(x)

n = 0;

term = 1;

c = term;

while abs(term) > 1.e-6

n = n+1;

term = (-1)^n*x^(2*n)/factorial(2*n);

c = c + term;

end

Using MATLAB vector/array manipulation syntax, this can be done with very small changes to the code. The main loop will continue until the series associated with each element of the input array has converged. You will want to use "." to change at least one operator to element-by-element mode. Other useful functons include size(), any(), and all(). For example, zeros(size(x)) returns an array or vector the same size as x filled with zeros.

Make sure your function gets the same answers as MATLAB's cos function. Use cputime to see how fast your function is relative to MATLAB's cos when you call it with a large input array (e.g., 100X100X100).

Explanation / Answer

function c = seriesCos2(x)
m=size(x);

n=0;
term=ones(m);

c = term;

while max(max(abs(term))) > 1.e-6 % max(abs(term)) will return the largest element of each column % of the array,and it's max will return the largest element of the array

n = n+1;

term = (-1)^n*x.^(2*n)/factorial(2*n); %x.^(2*n) raises x to the power of 2*n element wise

c = c + term;

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