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

Matlab code only please! will rate if correct! 3. Let f(x) = 23-5. Write a progr

ID: 3168345 • Letter: M

Question

Matlab code only please! will rate if correct!

3. Let f(x) = 23-5. Write a program (using Matlab/Octave or C/C++) that implements secant method as discussed in class (i.e., do not use any built-in secant method functions, if they are available). Run your program using Po 2.25, pi 2.75, and 10-6. How many iterations were required to achieve an absolute error less than ? What were the final pn and en-1 value? Give your answers using 9 significant figures (8 digits after the decimal). Hint: Since you have two initial guesses, save them both into the array p before starting the loop. Hint: Start with your Newton's method code from Question 1 and update the calculation of pn+1 to use the formula for secant method.

Explanation / Answer

clc;
clear all;
format long
f=@(x)x.^2-5;
df=@(x)2*x;


x=2.25 %initial value
tol=1e-6;
erorr=0.1;
n=1;
disp('_____________________________________________________________________________________')

disp('n x(n)) f(x(n)) df(x(n)) error')
disp('_____________________________________________________________________________________')
while(erorr>tol&n<100)

x(n+1)=x(n)-(f(x(n))/df(x(n))); %newton method
  
%x=x1;
erorr(n)=abs((f(x(n))/df(x(n))));

y=df(x(n));

fprintf('%d %15f %15f %15f %15f ',n ,x(n),f(x(n)),df(x(n)),erorr(n))
n=n+1;
end

x =

2.250000000000000

_____________________________________________________________________________________

n x(n)) f(x(n)) df(x(n)) error

_____________________________________________________________________________________

1 2.250000 0.062500 4.500000 0.013889

2 2.236111 0.000193 4.472222 0.000043

3 2.236068 0.000000 4.472136 0.000000

>>

number of iterations is 3

clc;
clear all;
format long
f=@(x)x.^2-5;
df=@(x)2*x;


x=2.75 %initial value
tol=1e-6;
erorr=0.1;
n=1;
disp('_____________________________________________________________________________________')

disp('n x(n)) f(x(n)) df(x(n)) error')
disp('_____________________________________________________________________________________')
while(erorr>tol&n<100)

x(n+1)=x(n)-(f(x(n))/df(x(n))); %newton method
  
%x=x1;
erorr(n)=abs((f(x(n))/df(x(n))));

y=df(x(n));

fprintf('%d %15f %15f %15f %15f ',n ,x(n),f(x(n)),df(x(n)),erorr(n))
n=n+1;
end

x =

2.750000000000000

_____________________________________________________________________________________

n x(n)) f(x(n)) df(x(n)) error

_____________________________________________________________________________________

1 2.750000 2.562500 5.500000 0.465909

2 2.284091 0.217071 4.568182 0.047518

3 2.236573 0.002258 4.473146 0.000505

4 2.236068 0.000000 4.472136 0.000000

>>

number of iterations is 4