Need help with #1 and matlab only please. Will rate if answer is correct! Proble
ID: 3168333 • Letter: N
Question
Need help with #1 and matlab only please. Will rate if answer is correct!
Problem Set 1. Let f(x) = 2 - 5. Write a program (using Matlab/Octave or C/C++) that implements Newton's method as discussed in class ie, do not use any built-in Newton's method functions, if they are available). Run your program using Po = 2.5 and e = 10 -6. How many iterations were required to achieve an absolute error less than e? What were the final Pn and en1 values? Give your answers using 9 significant figures (8 digits after the decimal). Hint: Find g(x) by hand (see Homework 2 Question 4) and hard code it in your program. HintSince Newton's method is a type of fixed point iteration, you may make use of your code from Homework 2 Question 3. : 2. Let f(x) = T - COS r. a) Starting with the initial guesses P % = 0 and P1 = 1, run secant method by hand to complete the following table. Keep as many digits of precision as you calculator (or program if you write one!) gives (hopefully at least 8) in intermediate calculations and from one iteration to the next, but round to 8 significant figures when filling in the table to turn in. n | Pn I lnt 1 - Pn a len 0 0 N/AExplanation / Answer
clc;
clear all;
format long
f=@(x)x.^2-5;
df=@(x)2*x;
x=2.5; %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
_____________________________________________________________________________________
n x(n)) f(x(n)) df(x(n)) error
_____________________________________________________________________________________
1 2.500000 1.250000 5.000000 0.250000
2 2.250000 0.062500 4.500000 0.013889
3 2.236111 0.000193 4.472222 0.000043
4 2.236068 0.000000 4.472136 0.000000
>>