Matlab
Matlab
Matlab
zip file preserving folder info using winzip The Bisection Method Suppose that we have a function f of one argument and we want to find a real number x such that f(x) = 0. As you know, there can be zero or more solutions to an equation of this form. We are interested in finding one of them. The bisection method works by assuming that we know of two values h and l such that f(h) > 0 and f(l) < 0. If this is the case (and the function f is continuous), then there must be at least one value v that falls between h and l such that f(v) = 0. This fact is best appreciated graphically. Suppose that we want to find a root of the function
f := x -> cos(x) - x;
It is easy to verify that f(0) is positive whereas f(1) is negative. If we plot f between these two points
plot(f(x), x=0..1);
we see that since the plot crosses the x-axis, there must be a root between 0 and 1. The bisection method works by repeatedly narrowing the gap between h and l until it closes in on the correct answer. It narrows the gap by taking the average v of h and l. If f(v) is positive, then v becomes the new value of h. If f(v) is negative, then v becomes the new value of l. In either event, we still have the root bracketed by h and l. If we repeat this process long enough, we'll eventually converge down to a good approximation to that root. I have given two files for this. bisect.m and fun.m save these two to the same directory open bisect.m (current directory in matlab must be this dir) and fun.m. I have given some functions in fun. You can add new functions and when needed, remove the % sign before the selected function and put the same before other functions. Now run bisect.m http://math.fullerton.edu/mathews/n2003/bisection/BisectionBib/Links/BisectionBib_lnk_1.html resources on this topic. http://einstein.drexel.edu/courses/Comp-Phys/BV/root.html
Regula Falsi is very similar to the bisection method: it assumes that we start with a change of sign interval, and each step of the method tries to make this interval smaller. However, where bisection takes the average of the endpoints: xnew := ( xleft + xright ) / 2 the Regula Falsi method uses the size of the function at the endpoints as weights: xnew := ( f(xright) * xleft - f(xleft) * xright ) / ( f(xright) - f(xleft) ) If the function values at the two end points are of equal size and opposite magnitude, then this gets the same result as bisection. But suppose f(xright) is very negative and f(xleft) is slightly positive. Wouldn't you expect the root to be close to xleft? That's where Regula Falsi will look for it. The given file falsepos.m illustrates this. Save it in the same directory as the fun.m file. Use fun.m file to input functions http://math.fullerton.edu/mathews/n2003/regulafalsi/RegulaFalsiBib/Links/RegulaFalsiBib_lnk_1.html resources on this topic. Mullers method http://math.fullerton.edu/mathews/n2003/mullersmethod/MullersMethodBib/Links/MullersMethodBib_l nk_1.html The Newton-Raphson method The bisection method uses only the sign of the function to improve the approximation to the root at each stage. Regular falsi also uses the value of the function. Both methods require two starting approximations which bracket the root. The Newton-Raphson method (or Newton's method for short) takes things one step further by using the derivative of the function to improve the approximation, pi. Also, Newton's method needs only one starting guess, rather than two. If roots are complex numbers then we need a complex initial guess. An ideal method is Bairstow's method to determine the real and complex roots of an npolyth-order polynomial. It is given in the zipped file.
polyroot.m is used below to find the real and imaginary roots of the 5th-order polynomial:
f ( x) = x 5 3 x 4 10 x 3 + 10 x 2 + 44 x + 48
a = [1 -3 -10 10 44 48]; [real,imag] = polyroot(a) real = 4.0000 imag = 0 -1.0000 1.0000 0 0 -1.0000 -1.0000 -2.0000 3.0000
As usual, a more readable output can be obtained by using the fprintf command as follows. A = [1 -3 -10 10 44 48]; [Re,Im] = polyroot(A); index=1:length(Re); Result=[index;Re;Im]; fprintf(' Root(%1.0f) = %7.4f + %7.4fi\n',Result) Root(1) = 4.0000 + 0.0000i Root(2) = -1.0000 + -1.0000i Root(3) = -1.0000 + 1.0000i Root(4) = -2.0000 + 0.0000i Root(5) = 3.0000 + 0.0000i
A semi-automatic example for Gauss-Jordan elimination is also given in the folder elimination. Gauss-Jordan elimination method http://fourier.dur.ac.uk:80/~dma0wmo/teaching/1h-la/LAnotes/LAnotes.html