Discussion of the Secant Method to Solve an Equation. The following Secant formula is implemented to approximate the two roots lie in the intervals 2 to 3 and 0.1 to 1. The results are shown after the program. Modified Newton Raphson Approach (1) Modified Thomas Method (2) Multi Degree of Freedom (3).
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
begin{equation} label{eq:dervative:estimate} f'(x)approxfrac{f(x+epsilon)-f(x)}{epsilon} end{equation}
- Modified Secant Method-idea using secant method but instead using a fractional perturbation of the independent variable to exstimate deriv(f(x)) Modified Secant Method-pros.
- The secant method In the first glance, the secant method may be seemed similar to linear interpolation method, but there is a major difference between these two methods. In the secant method, it is not necessary that two starting points to be in opposite sign. Therefore, the secant method is not a kind of bracketing method but an open method.
- THE SECANT METHOD Newton’s method was based on using the line tangent to the curve of y = f(x), with the point of tangency (x 0;f(x 0)).
- Show this behavior by finding the secant of x degrees and 2 radians. U = symunit; syms x f = x.u.degree 2.u.radian; secf = sec (f) secf = 1/cos ( (pi.x)/180), 1/cos (2) You can calculate secf by substituting for x using subs and then using double or vpa.
for (epsilonll1). 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:
begin{equation} label{eq:3} x_{n+1}=x_n-f(x_n) frac{x_n-x_{n-1}}{f(x_n)-f(x_{n-1})}. end{equation}
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.
For guided practice and further exploration of how to debug, watch Video Lecture 6: Debugging.
MATLAB files have names that end with
.m
, and the name itself must comprise only letters and numbers with no spaces. The first character must be a letter, not a number. Open a new file by clicking on the white new-file icon in the top left of the window, or select from the menu File(rightarrow)New(rightarrow)Script. Copy the Newton method code for (tanh(x)=x/3) into it. Save it and give it a name (NewtonTanh.m for example). Now on the command prompt you 'run' the file by typing the name (without the .m
) and pressing Enter .A few points to look out for:
- You can store your files wherever you want, but they have to be in MATLAB's 'search path' (or in the current directory). To add the directory you want to the path select File(rightarrow)Set path… select 'Add Folder', select the folder you want, click 'OK' then 'Save'. To check if your file is in the path you can type
which NewtonTanh
and the result should be the path to your file. - If you choose a file-name that is already the name of a MATLAB command, you will effectively 'hide' that command as MATLAB will use your file instead. Thus, before using a nice name like
sum
, orfind
, orexp
, check, usewhich
to see if it already defined. - The same warning (as the previous item) applies to variable names, a variable will 'hide' any file or command with the same name.
- If you get strange errors when you try to run your file, make sure that there are no spaces or other non-letters in your filename, and that the file is in the path.
- Remember that after you make changes to your file, you need to save it so that MATLAB will be aware of the changes you made.
For guided practice and further exploration of how to use MATLAB files, watch Video Lecture 3: Using Files.
Exercise 7.Save the file as SecantTanh.m and modify the code so that it implements the Secant Method. You should increase the number of iterations because the Secant Method doesn't converge as quickly as Newton's method.
Notice that here it is not enough to use
x
like in the Newton's method, since you also need to remember the previous approximation (x_{n-1}). Hint: Use another variable (perhaps calledPrevX
).Convergence
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 (x_{n+1}=g(x_n)) is defined as having (p-)th order convergence if for a sequence (x_n) where (lim_{nrightarrowinfty}x_n=alpha) exists then
begin{equation} label{eq:convergence:order} lim_{nrightarrowinfty}frac{|{x_{n+1}-alpha}|}{|{x_n-alpha}|^p} = L ne 0. end{equation}
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 (x_1) and similarly x(n)
for (x_n):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:
The last line makes the following plot (except for the green line, which is (y=2x)):
MATLAB can calculate roots through Newton's method, and verification of convergence is graphed.
The main point here is that the points are more or less on the line y=2x, which makes sense:
Taking the logarithm of the sequence in (3) leads to
begin{equation} label{eq:convergence:plots} log|{x_{n+1}-alpha}| approx log L + plog|{x_{n}-alpha}| end{equation}
for (ngg1), which means that the points ((log|{x_{n}-alpha}|, log|{x_{n+1}-alpha}|)) will converge to a line with slope (p).
The periods in front of
*
, /
, and ^
are needed (as in the code above) when the operation can have a linear algebra connotation, but what is requested is an element-by-element operation. Since matrices can be multiplied and divided by each other in a way that is not element-by-element, we use the point-wise version of them when we are not interested in the linear algebra operation.Exercise 8.Internalize the differences between the point-wise and regular versions of the operators by examining the results of the following expressions that use the variables
A=[1 2; 3 4]
, B=[1 0; 0 2]
, and C=[3;4]
. Note: some commands may result in an error message. Understand what the error is and why it was given.A*B
vs.A.*B
vs.B.*A
vs.B*A
2*A
vs.2.*A
A^2
vs.A*A
vs.A.*A
vs.A.^2
vs.2.^A
vs.A^A
vs.2^A
. The last one here might be difficult to understand…it is matrix exponentiation.A/B
vs.AB
vs.A./B
vs.A.B
A*C
vs.A*C'
vs.C*A
vs.C'*A
AC
vs.AC'
vs.C/A
vs.C'/A
Homework 2.Modify your secant method code so that it remembers the iterations (perhaps save it in a new file?). Now plot the points that, according to (4) should be on a line with slope (p). What is (p)?
Welcome!
This is one of over 2,200 courses on OCW. Find materials for this course in the pages linked along the left.
MIT OpenCourseWare is a free & open publication of material from thousands of MIT courses, covering the entire MIT curriculum.
No enrollment or registration. Freely browse and use OCW materials at your own pace. There's no signup, and no start or end dates.
Knowledge is your reward. Use OCW to guide your own life-long learning, or to teach others. We don't offer credit or certification for using OCW.
Made for sharing. Download files for later. Send to friends and colleagues. Modify, remix, and reuse (just remember to cite OCW as the source.)
Learn more at Get Started with MIT OpenCourseWare
Modified Euler'sMethod :The Euler forward scheme may be very easy to implement but it can'tgive accurate solutions. A very small step sizeis required for any meaningful result. In this scheme, since, thestarting point of each sub-interval is used to find the slope of the solutioncurve, the solution would be correct only if the function is linear.So an improvement over this is to take the arithmetic average of the slopesat xi and xi+1(that is, at theend points of each sub-interval). The scheme so obtained is called modifiedEuler's method. It works first by approximating a value to yi+1and then improving it by making use of average slope.
yi+1 | = yi+ h/2 (y'i + y'i+1) |
= yi + h/2(f(xi, yi) + f(xi+1,yi+1)) |
If Euler's method is used to find the first approximationof yi+1 then
Secant Method Function Matlab
Truncation error:
yi+1 = yi + h y'i + h2yi'/2 + h3yi'' /3! + h4yiiv/4! + . . .
fi+1 = y'i+1 = y'i+ h y'i + h2yi''' /2 + h3yiiv/3! + h4yiv /4! + . . .
fi+1 = y'i+1 = y'i+ h y'i + h2yi''' /2 + h3yiiv/3! + h4yiv /4! + . . .
By substituting these expansions in the Modified Euler formula gives
yi + h y'i + h2yi'/2 + h3yi'' /3! + h4yiiv/4! + . . . = yi+ h/2 (y'i +y'i + h y'i +
h2yi''' /2 + h3yiiv/3! + h4yiv /4! + . . . )
h2yi''' /2 + h3yiiv/3! + h4yiv /4! + . . . )
So the truncation error is: - h3yi'' /12 -h4yiiv /24 + . . . that is,Modified Euler's method is of order two.
Example 1 | Find y(1.0) accurate upto fourdecimal places using Modified Euler's method by solving the IVP y'= -2xy2, y(0) = 1 with step length 0.2. | Solution |
Example 2 | Find y in [0,3] by solving the initial value problemy' = (x - y)/2, y(0) = 1. Compare solutions for h = 1/2, 1/4and 1/8. | Solution |
Example 3 | Find y(0.1) for y' = x - y2, y(0) = 1 correct uptofour decimal places. | Solution |
Example 4 | Find y at x = 1.1 and 1.2 by solving y' = x2 + y2, y(1) = 2.3 | Solution |