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

First write two MATLAB functions that both take as input x and output expression

ID: 3109811 • Letter: F

Question

First write two MATLAB functions that both take as input x and output expressions for f(x) and f'(x) (calculate expression for f'(x) by hand). Then write a MATLAB function that inputs initial guess p_0; number of iterations N; and outputs the p_N of Newton's method. Make sure you call the functions you have when you need values of f(x) or f'(x). (a) Turn in your programs for Newton's method on f(x) = x^2 - 8. (b) Write down your results when p_0 = 2 and N = 5, 10, 20 and when p_0 = 3 and N = 5, 10, 20.

Explanation / Answer

clc;
clear all;
f=@(x)x^2-8; %function

f1=@(x) 2*x; %derivative of function

%N=input('N=')
N=[5 10 20] %intervalus
for i=1:3
  
x=2; %initial value

n=1;
while(n<N)
  
x1=x-(f(x)/f1(x)); %newton method
  
%x=x1;
erorr(i)=abs(f(x)/f1(x));

x=x1;
p(i)=x;
n=n+1;
end
end
p'
erorr'

%% Results

N =

5 10 20


ans =

2.8284
2.8284
2.8284


ans =

1.0e-005 *

0.4248
0.4248
0.4248

%% Initial guess p0=3

clc;
clear all;
f=@(x)x^2-8; %function

f1=@(x) 2*x; %derivative of function

%N=input('N=')
N=[5 10 20] %intervalus
for i=1:3
  
x=3; %initial value

n=1;
while(n<N)
  
x1=x-(f(x)/f1(x)); %newton method
  
%x=x1;
erorr(i)=abs(f(x)/f1(x));

x=x1;
p(i)=x;
n=n+1;
end
end
p'
erorr'

N =

5 10 20


ans =

2.8284
2.8284
2.8284


ans =

1.0e-011 *

0.3189
0.3189
0.3189