# 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.

Fixed Point of Function & Matlab Program for the Newton-Raphson

1. Let g: R→R+ be such a function that g∈ C^1(R) and for all x ∈ R, -1 <g'(x) < 0.

Show that the sequence Xn+1 : = g(Xn) converges to the unique fixed point of the function g, regardless of chioce Xo ∈ R.

[ Note : Observe that the domain of function g is not a compact interval.]

2. Write a matlab program (Newton - Raphson ) for finding the root of the function f(x) = x^5 - 2x^3 + x + 2. Be as much accurate as you as you can. To proceed, store this function in an m-file, say f.m, and its derivative in df.m. Label the whole procedure newt.m.

View Full Posting Details