Secant, Exercises of Matlab skills

Solve the equations using the Secant method in MATLAB

Typology: Exercises

2012/2013

Uploaded on 06/19/2013

somarasta20131
somarasta20131 🇮🇷

1 document

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
MATLAB Command Window Page 1
>> %% SECANT METHOD
f = @(x) x^3-7*x^2+14*x-6;
x0=3.2;
x1=4; % x0,x1 initial approximations to location of root
TOL=10^(-5); % absolute error convergence tolerance
Nmax=100;% maximum number of iterations to be performed
flag=0;
older = x0; old = x1;
folder = feval(f,older);
for i = 2 : Nmax
fold = feval(f,old);
dx = fold * ( old - older ) / ( fold - folder );
new = old - dx;
fprintf('\t\t %3d \t %.15f \n' , i, new )
if ( abs(dx) < TOL )
flag=1;
break
else
older = old;
old = new;
folder = fold;
end
end
if flag == 0
disp('Maximum number of iterations exceeded' )
end
2 3.242424242424241
3 3.282135794330914
4 3.674795442889595
5 3.343431945668192
6 3.379834256066227
7 3.423311301253208
8 3.413290807771137
9 3.414190785272790
10 3.414213620680708
11 3.414213562369425
>>

Partial preview of the text

Download Secant and more Exercises Matlab skills in PDF only on Docsity!

MATLAB Command Window Page 1

>> %% SECANT METHOD

f = @(x) x^3-7x^2+14x-6;

x0=3.2; x1=4; % x0,x1 initial approximations to location of root TOL=10^(-5); % absolute error convergence tolerance Nmax=100;% maximum number of iterations to be performed

flag=0; older = x0; old = x1; folder = feval(f,older);

for i = 2 : Nmax fold = feval(f,old); dx = fold * ( old - older ) / ( fold - folder ); new = old - dx;

fprintf('\t\t %3d \t %.15f \n' , i, new )

if ( abs(dx) < TOL ) flag=1; break else older = old; old = new; folder = fold; end

end

if flag == 0 disp('Maximum number of iterations exceeded' ) end 2 3. 3 3. 4 3. 5 3. 6 3. 7 3. 8 3. 9 3. 10 3. 11 3.