Recommended commands: (See Matlab 0) . To show all digits in output, start your
ID: 2258800 • Letter: R
Question
Recommended commands: (See Matlab 0) . To show all digits in output, start your code with the command format long For f," define the function df(r) = 312 by using the command df = @(z) 3 * r. ^2; Proble Use the following pseudocode for the Newton-Raphson Method and write a MATLAB code to approximate the cube root a of a given number a with accuracy roughly within 10-8 using0-a/2. Use maximuin 100 iterations. Explain steps by cominenting on them Use f(x) = 23-a. Choose a = 2 + w where w is the last digit of your NAU user name Algorithm Newton-Raphson-Iteration Input: f(r) = rs_a, zo = a/2, tolerance 10-8, maximurn number of iterations 100 Output: an approximation of Va within 10- or a message of failure for i = 1 to 100 if lr - roldl 10 % checking required accuracy % done % leave for environment FoundSolution= true; break 1011; if FoundSolution end if % update zold for the next iteration end tor print z, 'The number of iteration=, i print The required accuracy is not reached in 100 iterations end ifExplanation / Answer
clc;
clear all;
a=3;
f=@(x)x.^3-a;%function
f1=@(x)3.*x.^2; %derivative of function
x0=a/2;%initial guess
n=0;
erorr=0.1;
del=1e-8;
x(1)=x0;
m=2;
while (abs(erorr>del)& (n<=100))
y1=x0-(f(x0)/f1(x0));% Newton method
erorr(n+1)=abs((y1-x0)); %erorr
% if abs(erorr<1e-3)
% break
%end
n=1+n;
x0=y1; % update xold
x(n+1)=x0;
end
if (n<100)
disp('Root is')
x(end)
disp('num_iter x_value erorr')
disp('_______________________________________________________________________________')
for i=1:n
fprintf('%d %20f %20f ',i ,x(i),erorr(i))
end
else
fprintf('The required accuracy is not reached in 100 iteration')
end
Root is
ans =
1.4422
num_iter x_value erorr
_______________________________________________________________________________
1 1.500000 0.055556
2 1.444444 0.002192
3 1.442253 0.000003
4 1.442250 0.000000
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.