



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Numerical Computing Summer ’10 MATH/CSCI 4800- Homework-
Assigned Monday July 12, 2010 Due Tuesday July 20, 2010
(Problem 2b) We have f (x) = sin x − 6 x − 5. A possible bracket in [− 1 , 0] since f (−1) = 1 + sin(−1) > 0 and f (0) = − 5 < 0. Problem 4(b) At the mid-point of the bracket, f (− 1 /2) = −2 + sin(− 1 /2) < 0. Therefore the root lies in [− 1 , − 1 /2]. At the midpoint of the new bracket, f (− 3 /4) = − 0 .5 + sin(− 3 /4) < 0. Therefore the root lies in [− 1 , − 3 /4]. The best approximation to the root at this stage is the midpoint of the bracket, − 7 / 8. The error is bounded by half the width of the bracket, 1/ 8.
(a) By a hand calculation, predict how many steps of bisection will be needed for the desired accuracy. (b) Write an M-file script to implement bisection. The script should be designed to solve a general equation f (x) = 0 where f is specified either in a separate M-file or as an inline or anonymous function. The script should allow you to take N bisection steps, where N is to be specified. It should allow you to print out a table of two columns with appropriate headings: the first column displaying the iteration number and the second the corresponding approximation to the root. Run the script for the problem at hand, using the value of N determined in part (a) above. (c) Find the ‘exact’ root by running the MATLAB command X = fzero(@(x) cos(x)-sin(x),0.5) and see if your answer is indeed correct to the required accuracy.
(a) The number of steps n is given by 1 2 n^
so that n ≥ 6 ln(10/ ln 2 = 19. 936. Therefore, n = 20 will suffice. (b) The MATLAB script and the numerical output are given below. % % Matlab script bisect.m % Solves f(x) = 0 using the bisection method % % Inputs: % a = left endpoint of interval % b = right endpoint of interval % N = number of iterations % % Outputs: % Root by bisection
clc format long e; a=0; b=1; N=20; % The function f(x) is supplied as an inline function f=inline(’cos(x)-sin(x)’); % fa=f(a); fb=f(b); % % check bracketing % if sign(fa)==sign(fb), error(’Root not in bracket’); end % % begin loop % for N=1: c=a+0.5*(b-a); fc=f(c); if sign(fa)~=sign(fc); % root lies in [a,c] b=c; fb=fc; else % root lies in [c,b] a=c; fa=fc; end
% print progress
xb=0.5(a+b); err=0.5abs(b-a); fprintf(’\n N = %i Solution = %20.15e error estimate = %20.15e \n’,N,xb, err); end
% exact solution xexact=fzero(f,0.5); fprintf(’\n Exact Solution = %20.15e \n’,xexact); %%%%%%%%% NUMERICAL OUTPUT N = 1 Solution = 7.500000000000000e-01 error estimate = 2.500000000000000e-
N = 2 Solution = 8.750000000000000e-01 error estimate = 1.250000000000000e-
N = 3 Solution = 8.125000000000000e-01 error estimate = 6.250000000000000e-
N = 4 Solution = 7.812500000000000e-01 error estimate = 3.125000000000000e-
N = 5 Solution = 7.968750000000000e-01 error estimate = 1.562500000000000e-
N = 6 Solution = 7.890625000000000e-01 error estimate = 7.812500000000000e-
N = 7 Solution = 7.851562500000000e-01 error estimate = 3.906250000000000e-
N = 8 Solution = 7.871093750000000e-01 error estimate = 1.953125000000000e-
1 + e−^2 x^
(a) Show that each of the following fixed-point problems is equivalent to the problem f (x) = 0. i. x = arccos
1 + e−^2 x
ii. x = −
ln
cos x
(b) Implement each scheme on MATLAB and attempt to find the fixed point starting with the initial guess x 0 = 3 in each case.
Be sure that your MATLAB code imposes a limit on the number of iterations allowed, so that the program does not enter an infinite loop.
(a) Simple algebraic manipulations show the desired equivalence. (b) i. The MATLAB script and the numerical output are given below. We find that the iteration converges. % MaATLAB script fixed.m % % g = function being iterated supplied inline % % clc maxit=10; % max iterations allowed %g=inline(’acos(-1./(1+exp(-2.x)))’); g=inline(’-(1/2)log(-(1+1./cos(x)))’); r = 3; % initial guess k = 0; % initialize iteration counter R=[k,r];
while k< maxit k=k+1; r1=g(r); R = [R;k,r1]; r=r1; end format short disp(R); %%%%%%%%%% NUMERICAL OUTPUT 0 3. 1.0000 3. 2.0000 3. 3.0000 3. 4.0000 3. 5.0000 3. 6.0000 3. 7.0000 3. 8.0000 3. 9.0000 3. 10.0000 3.
ii. The script is the same as above, with the inline function now being defined as g=inline(’-(1/2)*log(-(1+1./cos(x)))’); The numerical output, given below, now exhibits two new features. First, the iteration leads to complex values, and second, the iteration cycles between two complex conjugate numbers, thereby indicating divergence. 0 3. 1.0000 2. 2.0000 0. 3.0000 -0.3616 - 1.5708i 4.0000 -0.1634 + 1.5231i 5.0000 -0.1732 - 1.5489i 6.0000 -0.1697 + 1.5479i 7.0000 -0.1698 - 1.5483i 8.0000 -0.1698 + 1.5483i 9.0000 -0.1698 - 1.5483i 10.0000 -0.1698 + 1.5483i
a v^2
(v − b) = RT,
relates the pressure p, specific volume v and temperature T of a gas, where R is a universal constant and a and b are constants that depend on the particular gas. In appropriate units, R = 0. 082054 , and for carbon dioxide, a = 3.592 and b = 0. 04267. Use a zero finder of your choice to compute v for T = 300 and p = 1, 10 and 100. Compare your results to those of the ideal gas law, pv = RT. The latter can be used as a starting guess for an iterative method to solve the van der Waals equation. The script and numerical output are given below. MATLAB’s rootfinder fzero has been used.
% Script file HW2P6.m clc % Supply input data a=3.592; b=0.04267; R=0.082054; T=300; % Print headings fprintf(’ p v (ideal) v (van der Walls) \n’) % Set up the for loop for i=1: p=10^(i-1); % Specify the van der Walls function for v f=@(v) (p+a/v^2)(v-b)-RT; % Find starting guess from ideal gas law vi=R*T/p; % Use Matlab rootfinder fzero to find v % according to van der Walls vw=fzero(f,vi); % Print results fprintf(’%4.0f %12.4e %12.4e \n’,p,vi,vw) end %%%%%%%%%% NUMERICAL OUTPUT p v (ideal) v (van der Walls) 1 2.4616e+01 2.4513e+ 10 2.4616e+00 2.3545e+ 100 2.4616e-01 7.9511e-