Lawrence A. answered 09/25/16
Tutor
5.0
(333)
Patient and experienced tutor
Results
Iter Root
---- ---------
0 3.5000
1 3.3951
2 3.3900
3 3.3900
4 3.3900
---- ---------
0 3.5000
1 3.3951
2 3.3900
3 3.3900
4 3.3900
========================
Matlab code (copy and paste into Matlab editor)
========================
function NRmethod()
clc
% Plot the function first
figure(1); clf
x=[0:.1:4]; y=sin(x)+cos(4*x)-.3;
plot(x,y,'k-'); grid
xlabel('x','FontSize',11);
ylabel('f(x)','FontSize',11);
title('f(x)=sin(x)+cos(4*x)-0.3','FontSize',11);
% Define the function, derivative and starting solution
x0 = 3.5;
f = @(x) sin(x) + cos(4*x) - 0.3;
fp = @(x) cos(x) -4*sin(4*x);
% Execute loop
Root(1) = x0;
k=2;
for i=1:5
x = x0 - f(x0)/fp(x0);
Root(k) = x;
% Set up for next iteration
x0 = x;
k = k+1;
end
% Display iterations
disp('Iter Root');
disp('---- ---------');
for i=1:5
fprintf(1,'%4d %10.4f\n',i-1,Root(i));
end
end
clc
% Plot the function first
figure(1); clf
x=[0:.1:4]; y=sin(x)+cos(4*x)-.3;
plot(x,y,'k-'); grid
xlabel('x','FontSize',11);
ylabel('f(x)','FontSize',11);
title('f(x)=sin(x)+cos(4*x)-0.3','FontSize',11);
% Define the function, derivative and starting solution
x0 = 3.5;
f = @(x) sin(x) + cos(4*x) - 0.3;
fp = @(x) cos(x) -4*sin(4*x);
% Execute loop
Root(1) = x0;
k=2;
for i=1:5
x = x0 - f(x0)/fp(x0);
Root(k) = x;
% Set up for next iteration
x0 = x;
k = k+1;
end
% Display iterations
disp('Iter Root');
disp('---- ---------');
for i=1:5
fprintf(1,'%4d %10.4f\n',i-1,Root(i));
end
end