Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

- Create a main MATLAB script that performs the tasks described below. - Between

ID: 3873703 • Letter: #

Question

- Create a main MATLAB script that performs the tasks described below.

- Between each task, place a comment indicating the task your code is performing next.

2. Newton's Method: Pseudocode y-sa) fa ,) I define f(x) define f(x) input x0, tol Solution f(x while (e > to!) x x0-f(x0)/f'(x0) Slope a) Slope f(x en Xi+ 1 = Xi-704) print x Task 2a [15 points Implement Newton's method to solve the equation f(x) = cos(x) e-x = 0 assuming an initial guess value x0 1.0 and a tolerance (If(x)I) of 1x10 Task 2b [10 points] What solution would you get if you used x0 4.0 Is the solution different from what you got for 2a? Why? Task 2c [25 points). In a file named "newtsolver.m" create a function function xn = newtsolver(fcn,dfcndx, x0,tol,nmax), which returns the root for function f(x), which is described by the function handle fen. The function uses Newton's method to find the root of f(x), and the derivative of f(x) is given by the function handle dfcndx. x0 is the initial guess, xn is numerical solution, tol is tolerance for lfen(x1, and nmax is the maximum number of iterations allowed Use your function function to solve f(x) = cos(x) e-x-0, for x0-1.0, tol = 1x10-5, and nmax = 20.

Explanation / Answer

2a)

function [root, ex]= newraph(f,df,x0,tol) % function header

ex=abs(f(x0)); % initial error
x=x0; % initialised the value of x
f = inline(f); % function will we difine in function argumnet
df = inline(df); % defivetive of function will we difine in function argumnet
while ex>tol % starting loop . Loop will run till error is larger than tol
x=x0-(f(x)/df(x)); %calculate the root
ex=abs(f(x)); % calculate error
x0=x; % store the calculated root
end
root=x; % final solution

Output:

>> [solution, error]=newraph('cos(x)*exp(-x)','-(exp(-x)*(sin(x)+cos(x)))',1.0,10^-5)

solution =

1.5708


error =

8.8473e-08

2b)

>> [solution, error]=newraph('cos(x)*exp(-x)','-(exp(-x)*(sin(x)+cos(x)))',4.0,10^-5)

solution =

4.7124


error =

3.6380e-08

2c)

%--------f.m--- define the function cos(x)e-x--------------------

function f=f(x)
f= cos(x)*exp(-x);

%--------------------f.m ----------------end here--------------------------------------------------------------

%--------df.m--- defivative of the function cos(x)e-x--------------------

function df=df(x)
df=-(exp(-x)*(sin(x)+cos(x)));

%-------------------df.m ----------------end here--------------------------------------------------------------

% -----------newtsolve.m-------------file-----------------------

function xn= newtsolve(f,df,x0,tol,nmax)
iter=0; % initialised the iteration count
ex=abs(f(x0));
x=x0;
while (ex>tol | iter<nmax) % the loop will terminate if error is less than lot or nmax itereation is completed.
x=x0-(f(x)/df(x));
ex=abs(f(x));
x0=x;
iter=iter+1; % update the iteration count
end
xn=x;

%--------newtsolve.m--file end here--------------------

>> xn=newraph(fcn,dfcndx,1.0,10^-5,20)

xn =

1.5708