# Pdf Foile

**Topics:**Gaussian elimination, Numerical linear algebra, Iterative method

**Pages:**6 (2228 words)

**Published:**March 3, 2013

1. Chapter No. Two

Program 2.1

MATLAB m-ﬁle for the Bisection Method function sol=bisect(fn,a,b,tol) f a = f eval(f n, a); f b = f eval(f n, b); if f a∗f b > 0; fprintf(’Endpoints have same sign’) return end while abs (b − a) > tol c = (a + b)/2; f c = f eval(f n, c); if f a ∗ f c < 0; b = c; else a = c; end end; sol=(a + b)/2;

Program 2.2

MATLAB m-ﬁle for the Fixed-Point Method function sol=ﬁxpt(fn,x0,tol) old= x0+1; while abs(x0-old) > tol; old=x0; x0 = f eval(f n, old); end; sol=x0;

Program 2.3

MATLAB m-ﬁle for the Newton’s Method function sol=newton(fn,dfn,x0,tol) old = x0+1; while abs (x0 − old) > tol; old = x0; x0 = old − f eval(f n, old)/f eval(df n, old); end; sol=x0;

Program 2.4

MATLAB m-ﬁle for the Secant Method function sol=secant(fn,a,b,tol) x0 = a; x1 = b; f a = f eval(f n, x0); f b = f eval(f n, x1); while abs(x1-old)> tol new = x1 − f b ∗ (x1 − x0)/(f b − f a); x0 = x1; f a = f b; x1 == new; f b = f eval(f n, new); end; sol=new;

1

Program 2.5

MATLAB m-ﬁle for ﬁrst Modiﬁed Newton’s Method function sol=mnewton1(fn1,dfn1,x0,m,tol) old = x0+1; while abs (x0 − old) > tol; old = x0; fa=feval(fn,old); fb=feval(dfn,old); x0 = old − (m ∗ f a)/f b; end; sol=x0;

Program 2.6

MATLAB m-ﬁle for second Modiﬁed Newton’s Method function sol=mnewton2(fn1,dfn1,ddfn1,x0,tol) old = x0+1; while abs (x0 − old) > tol; old = x0; fa=feval(fn,old); fb=feval(dfn,old); fc=feval(ddfn,old); x0 = old − (f a ∗ f b)/((f b). ˆ 2 - (f a ∗ f c)); end; sol=x0;

Program 2.7

MATLAB m-ﬁle for Newton’s Method for a Nonlinear System function sol=newton2(fn2,dfn2,x0,tol) old=x0+1; while max(abs(x0-old))>tol; old=x0; f = f eval(f n2, old); f 1 = f (1); f 2 = f (2); J=feval(dfn2,old); f 1x = J(1, 1); f 1y = J(1, 2); f 2x = J(2, 1); f 2y = J(2, 2); D = f 1x ∗ f 2y − f 1y ∗ f 2x; h = (f 2 ∗ f 1y − f 1 ∗ f 2y)/D; k = (f 1 ∗ f 2x − f 2 ∗ f 1x)/D; x0 = old + [h, k]; end; sol=x0; 2. Chapter No. Three

Program 3.1

MATLAB m-ﬁle for ﬁnding inverse of a matrix function [Ainv]=INVMAT(A) [n,n]=size(A); I=zeros(n,n); for i=1:n; I(i,i)=1; end m(1:n,1:n)=A; m(1 : n, n + 1 : 2 ∗ n) = I; for i=1:n; m(i, 1 : 2 ∗ n) = m(i, 1 : 2 ∗ n)/m(i, i); for k=1:n; if i˜ =k m(k, 1 : 2∗n) = m(k, 1 : 2∗n)−m(k, i)∗m(i, 1 : 2∗n); end; end; end invrs = m(1 : n, n + 1 : 2 ∗ n); 2

Program 3.2

MATLAB m-ﬁle for Gaussian Elimination Without Pivoting function x=WP(B) [n,t]=size(B); U=B; for k=1:n-1; for i=k:n-1; m=U(i+1,k)/U(k,k); for j=1:t; U(i+1,j)=U(i+1,j)-m*U(k,j);end;end end i=n; x(i,1)=U(i,t)/U(i,i); for i=n-1:-1:1; s=0; for k=n:-1:i+1; s = s + U(i, k) ∗ x(k, 1); end x(i,1)=(U(i,t)-s)/U(i,i); end; B; U; x; end

Program 3.3

MATLAB m-ﬁle for Gaussian Elimination by Partial Pivoting function x=PP(B) % B = input(′ input matrix in f orm[A : b]′ ); [n, t] = size(B); U = B; for M = 1:n-1 mx(M) = abs(U(M, M)); r = M; for i = M+1:n if mx(M) < abs(U(i, M)) mx(M)=abs(U(i,M)); r = i; end; end rw1(1,1:t)=U(r,1:t); rw2(1,1:t)=U(M,1:t); U(M,1:t)=rw1 ; U(r,1:t)=rw2 ; for k=M+1:n; m=U(k,M)/U(M,M); for j=M:t; U(k,j)=U(k,j)-m*U(M,j); end;end i=n; x(i)=U(i,t)/U(i,i); for i=n-1:-1:1; s=0; for k=n:-1:i+1; s = s + U(i, k) ∗ x(k); end x(i)=(U(i,t)-s)/U(i,i); end; B; U; x; end

Program 3.4

MATLAB m-ﬁle for the Gauss-Jordan Method function sol=GaussJ(Ab) [m,n]=size(Ab); for i=1:m Ab(i, :) = Ab(i, :)/Ab(i, i); for j=1:m if j == i; continue; end Ab(j, :) = Ab(j, :) − Ab(j, i) ∗ Ab(i, :); end; end; sol=Ab;

3

Program 3.5

MATLAB m-ﬁle for the LU decomposition Method function A = lu − gauss(A) % LU factorization using Gauss Elimination (without pivoting) [n,n] = size(A); for i=1:n-1; pivot = A(i,i); for k=i+1:n; A(k,i)=A(k,i)/pivot; for j=i+1:n; A(k,j)= A(k,j) - A(k,i)*A(i,j); end; end; end

Program 3.6

MATLAB m-ﬁle for using the Doolittle’s Method function sol = Doll(A,b) [n,n]=size(A); u=A;l=zeros(n,n); for i=1:n-1; if abs(u(i,i))> 0 for i1=i+1:n; m(i1,i)=u(i1,i)/u(i,i); for j=1:n u(i1, j) = u(i1, j) − m(i1, i) ∗ u(i, j);end;end;end;end for i=1:n;...

Please join StudyMode to read the full document