|
Response Details:
function yt= f(x)
a=1;
b=3;
c=3;
d=1;
yt= a*x^3 + b*x^2+c*x+d;
% bisection method
clear; % clear memory
% Initial interval is [a,b]; tol is tolerance
a = input( 'Enter value of a: ');
b = input( 'Enter value of b: ');
tol = input( 'Enter vlaue of tolerance: ');
maxIts=20; % maximum number of Iterations
% Check that a,b are good
if f(a)*f(b) > 0 ,
disp( 'f(a)*f(b) > 0; cannot guarantee a zero in [a,b]');
return; % terminate and return to prompt
end ;
%
disp( ' Left Right f(mid)') % Print column headings
% Set up plotting
N=100; %
x=a:(b-a)/(N-1):b; y=zeros(size(x));
for (i=1:N); y(i)=f(x(i)); end;
min=min(y); max=max(y);
hold off;
plot(x, y, 'r-');
hold on;
plot([a,b],[0,0], 'r-');
xlabel( 'x'); ylabel('f(x)'); title('Bisection algorithm');
% Start bisection
left = a; right = b; fleft = f(left);
n = 0; % counter
while n <= maxIts,
if abs(right - left) < tol
break
end; % iteration completed
mid = left + (right - left)/2; fmid = f(mid);
% mark old interval green
plot([left,left], [min,max], 'g-');
plot([right,right], [min,max], 'g-');
% update
if (fmid*fleft > 0 )
left = mid;
else
right = mid;
end;
% Results to screen.
disp(sprintf( '%12.6e %12.6e %12.6e', left, right, fmid))
n = n + 1; % increase counter
% mark new interval blue
plot([left,left], [min,max], 'b-');
plot([right,right], [min,max], 'b-');
disp( 'Hit any key');
pause;
end ;
if n > maxIts,
disp( 'No convergence to requested accuracy')
else
disp( 'Succesfully computed root');
alpha=mid
end ;
hold off;
ANSWER
-0.9996
newton method
x(1)= -0.5;
a=1;
b=3;
c=3;
d=1;
n=15;
disp( ' x(i+1)-x(i) x_n')
disp( '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
for i=1:(n + 1 )
x(i+1) = x(i) -( a*(x(i))^3+b*(x(i))^2 +c*x(i) +d)/ ...
( 3*a*(x(i))^2+ 2*b*(x(i)) +c );
if rem(i-1 ,1) ==0
fprintf( '%15.8f%19.7f\n',x(i)-x(i+1) ,x(i) );
end
end
>> newton101
x(i+1)-x(i) x_n
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0.16666667 -0.5000000
0.11111111 -0.6666667
0.07407407 -0.7777778
0.04938272 -0.8518519
0.03292181 -0.9012346
0.02194787 -0.9341564
0.01463192 -0.9561043
0.00975461 -0.9707362
0.00650307 -0.9804908
0.00433538 -0.9869939
0.00289025 -0.9913292
0.00192684 -0.9942195
0.00128456 -0.9961463
0.00085637 -0.9974309
0.00057091 -0.9982873
0.00038061 -0.9988582
|