Zero found Residual Actual iterations used5. Apply the secant method to compute the negative zero with the error tolerance tol = 1.0e− 12 forf(x) = cosh(x) + cos(x)− 3.Explain how the initial...

1 answer below »
just problem 6,7,8,9. problem 5 is not included.



Zero found Residual Actual iterations used 5. Apply the secant method to compute the negative zero with the error tolerance tol = 1.0e− 12 for f(x) = cosh(x) + cos(x)− 3. Explain how the initial guesses x0 and x1 are chosen and present computational results in a table as follows: x(0), x(1) Zero found Residual Iterations used 6. Consider the following system of nonlinear equations about x1 and x2: sin(a1x1x2)− 2x2 − x1 = 0, a2(e 2x1 − e) + a3(4x22 − 2x1) = 0. (a) Put this system of nonlinear equations in vector form f(x) = 0 where f(x) = (f1(x), f2(x)) T and identify the formulas for f1(x) and f2(x). (b) Implement user-defined Matlab functions for f(x) and its Jacobian, respectively, that take a1, a2, a3 as parameter inputs in addition to the main variable input x. (c) Compute x(1) and x(2) in Newton’s method for this nonlinear system with the following param- eters: a1 = 12.5, a2 = 0.91, a3 = 2.7. Start the iteration with x(0) = [−0.36; 0.06]T . Present numerical results in the following table: k x (k) 1 x (k) 2 1 2 (d) Use Newton’s method with tol = 10−8 and x(0) = [−0.4, 0.06]T to solve the nonlinear system described in (d) above and present numerical results in a table as follows: x1 x2 Residual Actual iterations Present your Maltab script used to generate data in this table. Make sure that your script uses the “varargin”functionality of “newtonsys.m”. (e) Use Newton’s method with tol = 10−8 and x(0) = [−0.46, 0.09]T to solve the nonlinear system in this problem with a1 = 6.28, a2 = 0.8, a3 = 2.75 present numerical results in a table as follows: x1 x2 Residual Actual iterations 13 Present your Maltab script used to generate data in this table. Make sure that your script uses the “varargin”functionality of “newtonsys.m”. 7. This problem is for implementing and testing the modified Newton’s method with the approximate Jacobian constructed by the one-point finite difference. (a) Use the Matlab function approxJ_fdh.m given in the Lecture Slides to compute an approximate Jacobian J̃f for the system of nonlinear equations in Problem 6 at x (0) = [−0.3, 0.05]T with the following parameter: h = 0.01, a1 = 12.58, a2 = 0.9, a3 = 2.73. Present numerical results in the following table J̃(1, 2) J̃(2, 1) (b) Implement the modified Newton’s method that uses the approximate Jacobian generated by approxJ_fdh.m. Make sure that the implemented Matlab function has the following interface: function [x, res, niter] = newtonsys_approxJ_fdh(Ffun, ... x0, h, tol, nmax, varargin) (c) Use the Matlab function newtonsys_approxJ_fdh.m implemented above to solve the system of nonlinear equations specified in 7a with h = 0.01; tol = 10^(-8); nmax = 100; Present the numerical results in the following table x1 x2 res niter 8. This problem is for implementing and testing the modified Newton’s method with the approximate Jacobian by the complex variable. (a) Implement the complex variable method for computing an approximate Jacobian J̃f at a point x. Make sure that the implemented Matlab function has the following interface: function J = approxJ_compl(fun, x, EPS, varargin) (b) Use the Matlab function approxJ_compl.m implemented above to compute an approximate Jacobian J̃f for the system of nonlinear equations in Problem 6 at x (0) = [−0.3, 0.05]T with the following parameter: EPS = 10−1, a1 = 12.576, a2 = 0.918, a3 = 2.72. Present numerical results in the following table J̃(1, 2) J̃(2, 1) (c) Implement the modified Newton’s method that uses approxJ_compl.m to generate the approx- imate Jacobian. Make sure that the implemented Matlab function has the following interface: 14 function [x, res, niter] = newtonsys_approxJ_compl(Ffun, ... x0, EPS, tol, nmax, varargin) (d) Use the Matlab function newtonsys_approxJ_compl.m implemented above to solve the system of nonlinear equations specified in 8b with EPS = 10^(-5); tol = 10^(-8); nmax = 100; Present the numerical results in the following table x1 x2 res niter 9. This problem is for implementing and testing Broyden’s method. (a) Implement Broyden’s method described in the Lecture Slides. Make sure that the implemented Matlab function has the following interface: function [x, res, niter, err, B]=broyden(Ffun, B0, ... x0, tol, nmax, varargin) (b) Use Broyden’s method implement above and suitable choices of nmax to compute x(1) and x(2) for the system of nonlinear equations in Problem 6 with the following parameters: a1 = 12.6, a2 = 0.91, a3 = 2.71. Use x(0) = [−0.3, 0.05]T , B0 = I2×2, tol = 10−8 Present numerical results in the following tables k x (k) 1 x (k) 2 1 2 and B2(1, 1) = B2(1, 2) = B2(2, 1) = B2(2, 2) = (c) Use Broyden’s method to solve the system of nonlinear equations specified in 9b. Use B0 as the approximate Jacobian at x(0) = [−0.3, 0.05]T generated by the complex variable method with EPS = 10−7 and use tol = 10^{-8}; nmax = 100; Present the numerical results in the following tables x1 x2 res err niter 15 and B0(1, 1) = B0(1, 2) = Bn(1, 1) = Bn(1, 2) = where Bn is the matrix produced by the Broyden’s method. 16 1 3 2 Alfi o Quarteroni Fausto Saleri · Paola Gervasio Scientifi c Computing with MATLAB and Octave Fourth Edition Editorial BoardT. J. Barth M. Griebel D. E. Keyes R. M. Nieminen D. Roose T. Schlick Texts in Computational Science and Engineering 2 Editors Timothy J. Barth Michael Griebel David E. Keyes Risto M. Nieminen Dirk Roose Tamar Schlick For further volumes: http://www.springer.com/series/5151 Alfio Quarteroni • Fausto Saleri • Paola Gervasio Scientific Computing with MATLAB and Octave Fourth Edition 123 Alfio Quarteroni MATHICSE-CMCS Ecole Polytechnique Fédérale de Lausanne Lausanne Switzerland Paola Gervasio DICATAM Università degli Studi di Brescia Brescia Italy Fausto Saleri (1965-2007) MOX - Politecnico di Milano Milano Italy ISSN 1611-0994 ISBN 978-3-642-45366-3 ISBN 978-3-642-45367-0 (eBook) DOI 10.1007/978-3-642-45367-0 Springer Heidelberg New York Dordrecht London Library of Congress Control Number: 2014932425 Mathematics Subject Classification (2010): 65-01, 68U01, 68N15 c© Springer-Verlag Berlin Heidelberg 2003, 2006, 2010, 2014 This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illus- trations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law. The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or im- plied, with respect to the material contained herein. The picture on the cover shows a re-entrant electrical wave on a slab of homogeneous excitable medium, exhibiting spiral turbulence and spatiotemporal chaos. Computation by Ricardo Ruiz- Baier, IST, University of Lausanne, CH. Printed on acid-free paper Springer is part of Springer Science+Business Media (www.springer.com) www.springer.com To the memory of Fausto Saleri Preface Preface to the First Edition This textbook is an introduction to Scientific Computing. We will illustrate several numerical methods for the computer solution of cer- tain classes of mathematical problems that cannot be faced by paper and pencil. We will show how to compute the zeros or the integrals of continuous functions, solve linear systems, approximate functions by polynomials and construct accurate approximations for the solution of differential equations. With this aim, in Chapter 1 we will illustrate the rules of the game that computers adopt when storing and operating with real and complex numbers, vectors and matrices. In order to make our presentation concrete and appealing we will adopt the programming environment MATLAB � 1 as a faithful com- panion. We will gradually discover its principal commands, statements and constructs. We will show how to execute all the algorithms that we introduce throughout the book. This will enable us to furnish an im- mediate quantitative assessment of their theoretical properties such as stability, accuracy and complexity. We will solve several problems that will be raised through exercises and examples, often stemming from spe- cific applications. Several graphical devices will be adopted in order to render the read- ing more pleasant. We will report in the margin theMATLAB command along side the line where that command is being introduced for the first time. The symbol will be used to indicate the presence of exercises, the symbol to indicate the presence of a MATLAB program, while 1 MATLAB is a trademark of TheMathWorks Inc., 24 Prime Park Way, Natick, MA 01760, Tel: 001+508-647-7000, Fax: 001+508-647-7001. VII VIII Preface the symbol will be used when we want to attract the attention of the reader on a critical or surprising behavior of an algorithm or a pro- cedure. The mathematical formulae of special relevance are put within a frame. Finally, the symbol indicates the presence of a display panel summarizing concepts and conclusions which have just been reported and drawn. At the end of each chapter a specific section is devoted to mentioning those subjects which have not been addressed and indicate the biblio- graphical references for a more comprehensive treatment of the material that we have carried out. Quite often we will refer to the textbook [QSS07] where many issues faced in this book are treated at a deeper level, and where theoretical re- sults are proven. For a more thorough description of MATLAB we refer to [HH05]. All the programs introduced in this text can be downloaded from the web address mox.polimi.it/qs No special prerequisite is demanded of
Answered Same DayOct 07, 2021

Answer To: Zero found Residual Actual iterations used5. Apply the secant method to compute the negative zero...

Kshitij answered on Oct 11 2021
149 Votes
MATLAB/Code_5.m
%% Problem No.5
% clear screen
clc
clear all
maxiter = 100; % max number of iteration before get the answer
f = input('Enter Function in terms of x: ');
xn_2 = input('Ener Lower Limit: ');
xn_1 = input('Ener Upper Limit: ');
maxerr = input('Enter Maximum Error: ');
xn = (xn_2*f(xn_1) - xn_1*f(xn_2))/(f(xn_1) - f(xn_2));
disp('xn-2 f(xn-2) xn-1 f(xn-1) xn f(xn)');
disp(num2str([xn_2 f(xn_2) xn_1 f(xn_1) xn f(xn)],'%20.7f'));
flag = 1;
while abs(f(xn)) > maxerr
xn_2 = xn_1;
xn_1 = xn;
xn = (xn_2*f(xn_1) - xn_1*f(xn_2))/(f(xn_1) - f(xn_2));

disp(num2str([xn_2 f(xn_2) xn_1 f(xn_1) xn f(xn)],'%20.7f'));

flag = flag + 1;
if(flag == maxiter)
break;
end
end
if flag < maxiter
disp(['Root is x = ' num2str(xn)]);
disp(['Iteration Used = ' num2str(flag)]);
else
disp('Root does not exist');
end
MATLAB/P6/Code_6.m
%% Problem 6.

disp('Problem 6.')
% Clear screen
clc
clear all
%% Part (b)
disp('Part (b)')
% Input Parameter
a1 = input('a1 =');
a2 = input('a2 =');
a3 = input('a3 =');
x0 = zeros(1,2);
x0(1) = input('X0(1) =');
x0(2) = input('X0(2) =');
%% Part (a)
disp('Part (a & c)')
% system of nonlinear equations in vector form
fun = @(x)[sin(a1*x(1)*x(2))-2*x(2)-x(1);a2*(exp(2*x(1))-exp(1))+a3*(4*x(2)^2-2*x(1))]
tol=0;
nmax=3;
[x,res,niter] = newtonsys(@fun_newtonsys, @fun_newtonsys_J, x0, tol,nmax,a1,a2,a3)
clear all
%% Part(d)
disp('Part (d)')
x0=[-0.4;0.06];
nmax = 100;
tol=1E-8;
a1=12.5;
a2=0.91;
a3=2.7;
[x,res,niter] = newtonsys(@fun_newtonsys, @fun_newtonsys_J, x0, tol,nmax,a1,a2,a3)
clear all
%% Part(e)
disp('Part (e)')
x0=[-0.46;0.09];
nmax = 200;
tol=1E-8;
a1=6.28;
a2=0.8;
a3=2.75;
[x,res,niter] = newtonsys(@fun_newtonsys, @fun_newtonsys_J, x0, tol,nmax,a1,a2,a3)
MATLAB/P6/fun_newtonsys.m
function f = fun_newtonsys(x, a1, a2,a3)
n = length(x); f = zeros(n, 1);
f(1) = sin(a1*x(1)*x(2))-2*x(2)-x(1);
f(2) = a2*(exp(2*x(1))-exp(1))+a3*(4*x(2)^2-2*x(1));
MATLAB/P6/fun_newtonsys_J.m
function J = fun_newtonsys_J(x, a1, a2,a3)
n = length(x); J = zeros(n, n);
J(1,1) = a1*x(2)*cos(a1*x(1)*x(2))-1; J(1,2) = -2;
J(2,1) = 2*a2*exp(2*x(1)) - 2*a3; J(2,2) = 8*a3*x(2);
MATLAB/P6/newtonsys.m
function [x,res,niter] = newtonsys(Ffun, Jfun, x0, tol,...
nmax, varargin)
niter = 0; err = tol + 1; x = x0;
while err >= tol && niter < nmax
J = Jfun(x,varargin{:}); F = Ffun(x,varargin{:});
delta = - J\F; x = x + delta;
err = norm(delta); niter = niter + 1;
end
res = norm(Ffun(x,varargin{:}));
if (niter==nmax && err> tol)
fprintf(['Fails to converge within maximum',...
'number of iterations.\n',...
'The iterate returned has relative ',...
'residual %e\n'],res);
else
fprintf(['The method converged at iteration ',...
'%i with residual %e\n'],niter,res);
end
return
MATLAB/P6/Untitled.m
x0=[-0.36;0.06];
nmax = 3;
tol=0;
a1=12.5;
a2=0.91;
a3=2.7;
[x,res,niter] = newtonsys(@fun_newtonsys, @fun_newtonsys_J, x0, tol,nmax,a1,a2,a3)
MATLAB/P7/approxJ_fd.m
function J = approxJ_fd(fun, x0, x1, varargin)
h = x0 - x1; % this is a naive way for h, can be improved
n = length(x0); J = zeros(n, n);
F = fun(x1, varargin{:});
for j = 1:n % form the approximate Jacobian
e = zeros(n,1); e(j) = 1; % form the j-th unit vector
J(:, j) = (fun(x1 + h(j)*e, varargin{:}) - F)/h(j);
end
MATLAB/P7/approxJ_fdh.m
function J = approxJ_fdh(fun, x0, h, varargin)
n = length(x0); J = zeros(n, n);
F = fun(x0, varargin{:});
for j = 1:n % form the approximate Jacobian
e = zeros(n,1); e(j) = 1; % form the j-th unit vector
J(:, j) = (fun(x0 + h*e, varargin{:}) - F)/h;
end
MATLAB/P7/Code_7.m
%% Problem No. 7
disp('Problem No. 7')
% clear screen
clc
clear all
%% Part (a)
disp('Part (a)')
a1 = 12.58;
a2 = 0.9;
a3 = 2.73;
fun = @(x)[sin(a1*x(1)*x(2))-2*x(2)-x(1);a2*(exp(2*x(1))-exp(1))+a3*(4*x(2)^2-2*x(1))];
x0 = [-0.3;0.05];
h = 0.01;
niter = 0;
J = approxJ_fdh(fun, x0, h)
delta = -J\fun(x0)
x = x0 + delta
x0 = x;
err = norm(delta)
niter = niter + 1
%% Part(b & c)
disp('Part (b & c)')
tol = 10^(-8); nmax = 100;
[x,res,niter] = newtonsys_approxJ_fdh(fun, x0, h, tol,nmax)
MATLAB/P7/newtonsys_approxJ_fd.m
function [x,res,niter] = newtonsys_approxJ_fd(Ffun, x0, x1, tol,...
nmax, varargin)
niter = 1; err = tol + 1; x = x1;
while err >= tol && niter < nmax
J = approxJ_fd(Ffun, x0, x, varargin{:});
F = Ffun(x, varargin{:});
delta = - J\F; x0 = x; x = x + delta;
err = norm(delta); niter = niter + 1;
end
res = norm(Ffun(x, varargin{:}));
if (niter==nmax && err> tol)
fprintf([' Fails to converge within maximum ',...
'number of iterations.\n',...
'The iterate returned has relative ',...
'residual %e\n'], res);
err
else
fprintf(['The method converged at iteration ',...
'%i with residual %e\n'],niter, res);
end
MATLAB/P7/newtonsys_approxJ_fdh.m
function [x,res,niter] = newtonsys_approxJ_fdh(Ffun, x0, h, tol,...
nmax, varargin)
niter = 1; err = tol + 1;x1=x0-h; x = x1;
while err >= tol && niter < nmax
J = approxJ_fdh(Ffun, x0, h, varargin{:});
F = Ffun(x, varargin{:});
delta = - J\F; x0 = x; x = x + delta;
err = norm(delta); niter = niter + 1;
end
res = norm(Ffun(x, varargin{:}));
if (niter==nmax && err> tol)
fprintf([' Fails to converge within maximum ',...
'number of iterations.\n',...
'The iterate returned has relative ',...
'residual %e\n'], res);
err
else
fprintf(['The method converged at iteration ',...
'%i with residual %e\n'],niter, res);
end
MATLAB/P8/approxJ_compl.m
function J = approxJ_compl(fun,x0,EPS,varargin)
n = length(x0); J = zeros(n, n); %EPS = 2*eps;
for j = 1:n % form the approximate Jacobian
e = zeros(n,1); e(j) = 1;
J(:, j) = (1./EPS).*imag(fun(x0 + EPS.*sqrt(-1).*e));
end
MATLAB/P8/Code_8.m
%% Problem No.8
disp('Problem No.8')
% Clear screen
clc
clear all
%% Part(b)
disp('Part(b)')
x0=[-0.3;0.05];
EPS=.1;
a1=12.576;
a2=0.918;
a3=2.72;
fun = @(x)[sin(a1*x(1)*x(2))-2*x(2)-x(1);a2*(exp(2*x(1))-exp(1))+a3*(4*x(2)^2-2*x(1))];
J = approxJ_compl(fun,x0,EPS)
niter=0;
delta = -J\fun(x0)
x = x0 + delta
x0 = x;
err = norm(delta)
niter = niter + 1
%% Part(d)
disp('Part(d)')
EPS = 10^(-5); tol = 10^(-8); nmax = 100;
[x,res,niter] = newtonsys_approxJ_compl(fun, x0, EPS, tol, nmax)
MATLAB/P8/newtonsys_approxJ_compl.m
function [x,res,niter] = newtonsys_approxJ_compl(Ffun, x0, EPS, tol,...
nmax, varargin)
h=2*EPS;
niter = 1; err = tol + 1;x1=x0-h; x = x1;
while err >= tol && niter < nmax
J = approxJ_compl(Ffun,x0,EPS,varargin{:});
F = Ffun(x, varargin{:});
delta = - J\F; x0 = x; x = x + delta;
err = norm(delta); niter = niter + 1;
end
res = norm(Ffun(x, varargin{:}));
if (niter==nmax && err> tol)
fprintf([' Fails to converge within maximum ',...
'number of iterations.\n',...
'The iterate returned has relative ',...
'residual %e\n'], res);
err
else
fprintf(['The method converged at iteration ',...
'%i with residual %e\n'],niter, res);
end
MATLAB/P9/approxJ_compl.m
function J = approxJ_compl(fun,x0,EPS,varargin)
n = length(x0); J = zeros(n, n); %EPS = 2*eps;
for j = 1:n % form the approximate Jacobian
e = zeros(n,1); e(j) = 1;
J(:, j) = (1./EPS).*imag(fun(x0 + EPS.*sqrt(-1).*e));
end
MATLAB/P9/broyden.m
function [x, res, niter, err, B]=broyden(Ffun, B0, x0, tol, ...
nmax, varargin)
niter = 0; err = tol + 1;
B = B0; x = x0;F = Ffun(x,varargin{:});
while err >= tol && niter < nmax
F = Ffun(x,varargin{:});
delta = -B\F; x = x + delta;
%B = B + (F*delta')/(delta'*delta);
err = norm(delta); niter = niter + 1;
end
res = norm(Ffun(x,varargin{:}));
if (niter==nmax && err> tol)
fprintf(['Fails to converge within maximum',...
'number of iterations.\n',...
'The iterate returned has relative ',...
'residual %e\n'],res);
else
fprintf(['The method converged at iteration ',...
'%i with residual %e\n'],niter,res);
end
return
MATLAB/P9/Code_9.m
%% Problem No.9
disp('Problem No.9')
% Clear Screen
clc
clear all
%% Part(b)
disp('Part(b)')
n=2;
x0=[-0.3;0.05];
a1=12.6;
a2=0.91;
a3=2.71;
fun = @(x)[sin(a1*x(1)*x(2))-2*x(2)-x(1);a2*(exp(2*x(1))-exp(1))+a3*(4*x(2)^2-2*x(1))];
B0=eye(n,n);
tol = 1e-8;
nmax = 3;
[x, res, niter, err, B]=broyden(fun, B0, x0, tol,nmax)
%% Part (c)
disp('Part (c)')
EPS=10^-7;
B0 = approxJ_compl(fun,x0,EPS)
tol = 1e-8;
nmax = 100;
[x, res, niter, err, B]=broyden(fun, B0, x0, tol,nmax)
MATLAB/Report.docx
Q 5.
Code
clc;
clear;
maxiter = 100; % max number of iteration before get the answer
f = input('Enter Function in terms of x: ');
xn_2 = input('Ener Lower Limit: ');
xn_1 = input('Ener Upper Limit: ');
maxerr = input('Enter Maximum Error: ');
xn = (xn_2*f(xn_1) - xn_1*f(xn_2))/(f(xn_1) - f(xn_2));
disp('xn-2 f(xn-2) xn-1 f(xn-1) xn f(xn)');
disp(num2str([xn_2 f(xn_2) xn_1 f(xn_1) xn f(xn)],'%20.7f'));
flag = 1;
while abs(f(xn)) > maxerr
xn_2 = xn_1;
xn_1 = xn;
xn = (xn_2*f(xn_1) - xn_1*f(xn_2))/(f(xn_1) - f(xn_2));

disp(num2str([xn_2 f(xn_2) xn_1 f(xn_1) xn f(xn)],'%20.7f'));

flag = flag + 1;
if(flag == maxiter)
break;
end
end
if flag < maxiter
disp(['Root is x = ' num2str(xn)]);
disp(['Iteration Used = ' num2str(flag)]);
else
disp('Root does not exist');
end
Output
Enter Function in terms of x: @(x) cosh(x)+cos(x)-3
Ener Lower Limit: 0
Ener Upper Limit: 2
Enter Maximum Error: 1E-12
xn-2 f(xn-2) xn-1 f(xn-1) xn f(xn)
0.0000000 -1.0000000 2.0000000 0.3460489 1.4858302 -0.5926628
2.0000000 0.3460489 1.4858302 -0.5926628 1.8104552 -0.0989666
1.4858302 -0.5926628 1.8104552 -0.0989666 1.8755297 0.0387352
1.8104552 ...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here