# MATLAB Code Functions

1. Construct your own MATLAB function realizing the insertion sort algorithm.

Your function should have an unsorted array as its input and the sorted array as the output. In particular, you will need:

(i) Draw a flowchart of the function realizing the insertion sort

(ii) Write the corresponding MATLAB code. You should check (just for yourself) how your function works with an arbitrary unsorted array.

2. Write your own function computing cos(x) based on the Taylor expansion of cos(x) in the vicinity of zero. Name this function my_cos(x, N, eps). The input parameters in this function should be: x- the argument; N - the initial number of terms in the expansion; eps -optional parameter, giving the accuracy of approximation. In particular, you will need to do:

(i) Write MATLAB code for a function computing cos(x) based on the Taylor expansion with the fixed number of terms N in the expansion. Name

this function my_cos1(x,N). Draw a flowchart of my_cos1

(ii) (8 marks) Write a MATLAB code for my_cos(x,N,eps) based on the function my_cos1(x,N). 'Eps' is an optional parameter, the giving the accuracy. In other words, your function can used in two modes: my_cos(x,N,eps) and my_cos(x,N). Draw a flowchart of my_cos.

Hint: if we need to compute cos(x) with a given accuracy ?, we can truncate the infinite part of Taylor expansion cos(x)=a0+a1 x+a2 x2+... + an xn+...starting with the term an xn in case we have |an xn|<?.

3. You are computing now the roots of a nonlinear equation f(x)=0 with the accuracy ? based on the bisection method (use Wikipedia for details about the method, section 'The method'). The given accuracy is achieved in case |f(c)|< ? , where c- is an approximate root of the equation.

You will need to construct your own MATLAB function bisect_1(fname, a, b, eps), where fname - is the name of the function; a,b, determine the initial interval for the root search; eps is the accuracy.

(i) Draw a flowchart of bisect_1(fname, a, b, eps).

(ii) Write MATLAB code for bisect_1(fname, a, b, eps).

(iii) Find all roots of the equation exp(- cos(x)) -1=0 in the interval 0<x<3

https://brainmass.com/computer-science/searching/matlab-code-functions-465453

#### Solution Summary

MATLAB code functions are determined. The function computes cos(x) based on the Taylor expansion of cos(x) in the vicinity of zero.