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

Secant Method using Mathlab 1. Write a MATLAB progrm that implements the Secant

ID: 3603224 • Letter: S

Question

Secant Method using Mathlab

1. Write a MATLAB progrm that implements the Secant method for root-finding problem.

The input parameter should be: the function f, the previous guess xn-1, the next question xn+1, an error tolerance, and the maximum number of iterations.

The program should stop when the approximate percent error is less than or equal the tolerance.

Your output should be a table where each row contains the number of iteration, the value of the root, and the percent approximate error.

2. In order to test your program to compute the root of e^-x=cos(x) +1 with tolerance 10^-6, create a main M-file where you can call you program, the necessary function(s), and that should the output of roots (to 5 significant figures)

3. Everything should work by simply running the main M-file. The output table should be printed on the screen, and your program shoud create a plot of the used function and a point representing the root

4. Your final submission should include:

a) Your M-file for question 1

b) The main M-file for question 2

c) When you run the main M-file, the output table should be printed on the screen, and a plot of the function with the root point should be created.

Explanation / Answer

While Newton's method is fast, it has a big downside: you need to know the derivative of f in order to use it. In many "real-life" applications, this can be a show-stopper as the functional form of the derivative is not known. A natural way to resolve this would be to estimate the derivative using

(1)f(x)f(x+)f(x)

for 1. The secant method uses the previous iteration to do something similar. It approximates the derivative using the previous approximation. As a result it converges a little slower (than Newton's method) to the solution:

(2)xn+1=xnf(xn)xnxn1f(xn)f(xn1).

Since we need to remember both the current approximation and the previous one, we can no longer have such a simple code as that for Newton's method. At this point you are probably asking yourself why we are not saving our code into a file, and it is exactly what we will now learn how to do.

Coding in a File

Instead of writing all your commands at the command prompt, you can type a list of commands in a file, save it and then have MATLAB® "execute" all of the commands as if you had typed them into the command prompt. This is useful when you have more than very few lines to write because inevitably you are bound to make a small mistake every time you write more than 5 lines of code. By putting the commands in a file you can correct your mistakes without introducing new ones (hopefully). It also makes it possible to "debug" your code, something we will learn later.

onvergence

Different root-finding algorithms are compared by the speed at which the approximate solution converges (i.e., gets closer) to the true solution. An iterative method xn+1=g(xn) is defined as having pth order convergence if for a sequence xn where limnxn= exists then

(3)limn|xn+1||xn|p=L0.

Newton's method has (generally) second-order convergence, so in Eq. (3) we would have p=2, but it converges so quickly that it can be difficult to see the convergence (there are not enough terms in the sequence). The secant method has a order of convergence between 1 and 2. To discover it we need to modify the code so that it remembers all the approximations.

The following code, is Newton's method but it remembers all the iterations in the list x. We use x(1) for x1 and similarly x(n) for xn:

The semicolon (;) at the end of line 4 tells MATLAB not to display the value of x after the assignment (also in line 1. Without the lonely x on line 9 the code would calculate x, but not show us anything.

After running this code, x holds the 6 approximations (including our initial guess) with the last one being the most accurate approximation we have:

Notice that there is a small but non-zero distance between x(5) and x(6):

This distance is as small as we can hope it to be in this case.

We can try to verify that we have second order convergence by calculating the sequence defined in Eq. (3). To do that we need to learn more about different options for accessing the elements of a list like x. We have already seen how to access a specific element; for example to access the 3rd element we write x(3). MATLAB can access a sublist by giving it a list of indexes instead of a single number:

We can use the colon notation here too:

Another thing we can do is perform element-wise operations on all the items in the list at once. In the lines of code below, the commands preceding the plot command are executed to help you understand how the plot is generated:

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