MAE 107 Homework 7 Solutions 1. Tridiagonal system for u xx (x) + u(x) = 1 + sin(πx/4), for x [0, ], where u(0) =, u() = 4, with step size h = 1/. The total number of segments are: n = 0 1/ =. The nodes will be x 0 through x, with x 0 = 0, x =, and x k+1 x k = 1/. Use difference approximation for u xx (x) at each x k, k = 1,,, 4, 5: The differential equation becomes D h (x k) = u(x k1) u(x k ) + u(x k+1 ) h u(x k1) u(x k ) + u(x k+1 ) h + u(x k ) = 1 + sin( πx k for k = 1,,, 4, 5. Substituting in u(0) = u(x 0 ) =, u() = u(x ) = 4), we get u(x 1) + u(x ) h + u(x 1 ) = 1 + sin( πx 1 u(x 1) u(x ) + u(x ) h + u(x ) = 1 + sin( πx u(x ) u(x ) + u(x h + u(x ) = 1 + sin( πx u(x ) u(x + u(x 5 ) h + u(x = 1 + sin( πx 4 u(x 4) u(x 5 ) + 4 h + u(x 5 ) = 1 + sin( πx 5 Putting all the constants on the right hand side and collect terms, we get ( ) ( ) h + 1 u(x 1 ) h u(x ) = 1 + sin( πx 1 + h ( ) ( ) ( ) h u(x 1 ) + h + 1 u(x ) h u(x ) = 1 + sin( πx ( ) ( ) ( ) h u(x ) + h + 1 u(x ) h u(x = 1 + sin( πx ( ) ( ) ( ) h u(x ) + h + 1 u(x h u(x 5 ) = 1 + sin( πx 4 ( ) ( ) h u(x + h + 1 u(x 5 ) = 1 + sin( πx 5 + 1 h 1
We can also multiply by h on both sides: ( + h )u(x 1 ) u(x ) = h + h sin( πx 1 + u(x 1 ) + ( + h )u(x ) u(x ) = h + h sin( πx u(x ) + ( + h )u(x ) u(x = h + h sin( πx u(x ) + ( + h )u(x u(x 5 ) = h + h sin( πx 4 u(x + ( + h )u(x 5 ) = h + h sin( πx 5 + 1 Write in matrix form + 1 0 0 0 u(x h h 1 ) + 1 0 0 h h h u(x ) 0 + 1 0 h h h u(x 0 0 + 1 ) = u(x h h h 0 0 0 + 1 u(x h h 5 ) or 1 + sin( πx 1 + h 1 + sin( πx 1 + sin( πx 1 + sin( πx 4 1 + sin( πx 5 + 1 h + h 0 0 0 u(x 1 ) h (1 + sin( πx 1 + h 4 0 0 u(x ) )) + h (1 + sin( πx 0 + h ) 0 u(x 0 0 + h ) = h (1 + sin( πx ) u(x h (1 + sin( πx 4 0 0 0 + h ) u(x 5 ) h (1 + sin( πx 5 4 )) + 1. Matlab function that generates the tridiagonal system with input n: function [A,b] = tridiagsys(n) % Generate the tridiagonal system for given differential equation % Input variables % - n: The number of steps % Output variables % - A: The tridiagonal matrix % - b: The vector of right hand side (rhs) % Functions Called % None % Given differential equation % -u (x) + u(x) = 1 + sin(pi*x/4), 0 <= x <= % u(0)=, u()=4 % Calculate h and x(interior points only, not points) h = (-0)/n; x = h:h:-h;
% Allocate vectors A = zeros(n-1,n-1); b = zeros(n-1,1); % Fill in the matrix for i = 1:n-1 % Diagonal A(i,i) = /h^ + 1; if i >1 % Lower diagonals A(i,i-1) = -/h^; % Upper diagonals A(i-1,i) = -/h^; % Calculate rhs vector b(1) = 1 + sin(pi*x(1)/4) + /h^; for i=:n- b(i) = 1 + sin(pi*x(i)/4); b(n-1) = 1 + sin(pi*x(n-1)/4) + 1/h^; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Code that calls the function with inputs looping over an input vector of n values: % Generate the tridiagonal system of the differential equation % -u (x) + u(x) = 1 + sin(pi*x/4), 0 <= x <= % u(0)=, u()=4 % for a vector of n values % vector of n n = [,8,10]; % loop over n for i = 1:length(n); [A,b] = tridiagsys(n(i)); display(a) display(b). There is only one root between [a k1, b k1 ]. The midpoint of the interval is a k1+b k1. Let the root be r. If our guess ˆx k is in the left half of the interval, i.e. ˆx k [a k1, a k1+b k1 ), the worst case would be when r = b k1, and the error in this case would be e = b k1 ˆx k > b k1a k1. Similarly, if our guess ˆx k is in the right half of the interval, i.e. or ˆx k ( a k1+b k1, b k1 ], the
worst case would be when r = a k1, and the error in this case would be e = a k1 ˆx k > b k1 a k1. If our guess ˆx k is at the midpoint, i.e. ˆx k = a k1+b k1, then the worst case is when r = a k1 or r = b k1, and in either case the error e = b k1a k1 is less than that of other guesses. 4. Perform steps of bisection method to find the root of f(x) = exp[x sin(x)], starting with a 0 = and b 0 =. f(a 0 ) =.9545 f(b 0 ) = 18.995 f(a 0 ) f(b 0 ) < 0. There is a root between a 0 and b 0. ˆx 1 = c 1 = a 0 + b 0 f(c 1 ) = = + = 0 f(c 1 ) f(b 0 ) < 0. There is a root between c 1 and b 0. a 1 = c 1 = 0 b 1 = b 0 = ˆx = c = a 1 + b 1 f(a 1 ) = f(b 1 ) = 18.995 f(c ) = 0.185 = 0 + = 1 f(c ) f(a 1 ) < 0. There is a root between c and a 1. a = a 1 = 0 b = c = 1 ˆx = c = a + b f(a ) = f(b ) = 0.185 f(c ) = 1.170 = 0 + 1 = 0.5 f(c ) f(b ) < 0. There is a root between c and b. a = c = 0.5 b = b = 1 ˆx 4 = c 4 = a + b = 0.5 + 1 5. Matlab code that performs bisection method. = 0.75 function [f] = functioneval(x) % Compute the function value % Input variables 4
% - x: the point where function value is calculated % Output variables % - f: function value at point x % Functions Called % - None f = - exp(*x - sin(x)); A function that performs bisection method: function [root, f, n] = bisection_mthd(a,b,eps) % The objective of this code is to compute the root of a % function using the bisection method. % Input variables % - a,b: the starting points of the bisection algorithm % - eps: the desired error % Output variables % - root: The approximation of the root of the function after n iterations (xhat_n) % - f: The value of the funtion at xhat_n % - n: The number of function evaluations % Functions Called % - functioneval: This function returns the evaluation of f at x % Initial function evaluations fa = functioneval(a); fb = functioneval(b); % Check if starting point is compatible if (fa*fb > 0) error( Initial points are incompatible ); % Determine number of steps corresponding to desired error n = ceil((log(b-a) - log(eps))/log()); % midpoint of [a_0,b_0] c = a + 0.5*(b-a); % Algorithm. of Textbook for i = 1:n-1 fc = functioneval(c); if (fa*fc < 0) b = c; fb = fc; elseif (fa*fc > 0) a = c; fa = fc; else %fa*fc = 0 root = c; 5
root = (a+b)/; f = functioneval(root); % there is one function evaluation in each iteration from step to step n; % and there are two function evaluations in the beginning % number of function evaluations = n+1 n= n+1; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Results: >> [xhat_n,fxhat_n,n]= bisection_mthd(-,,10^(-5)) xhat_n = 0.9585 fxhat_n = n = -8.51e-0 0