matsec

3
function [ xc ] = Secant(f,a,b,tol) r(1)=a; r(2)=b; i=1; error=abs(b-a); while error>tol fa=f(a); fb=f(b); xc=b-(fb*(b-a))/(fb-fa); a=b; b=xc; r(i+2)=xc; error(i)=abs(b-a); i=i+1; end disp(['r=',num2str(r(end),10),' with an absolute error=',num2str(error(end)),' in n=',num2str(i- 1)]) r; error; x=xc; end a=input('a='); b=input('b='); tol=1e-03; f=@(x) x.^3-9; tol=1e-3; Secant(f,a,b,tol); a=2 b=3 r=2.080083266 with an absolute error=0.00012474 in n=4 Published with MATLAB® R2014a

Upload: erica-martinez

Post on 18-Jan-2016

213 views

Category:

Documents


1 download

DESCRIPTION

Matlab code for Secant and Regula Falsi functions

TRANSCRIPT

Page 1: matsec

function [ xc ] = Secant(f,a,b,tol)

r(1)=a;

r(2)=b;

i=1;

error=abs(b-a);

while error>tol

fa=f(a);

fb=f(b);

xc=b-(fb*(b-a))/(fb-fa);

a=b;

b=xc;

r(i+2)=xc;

error(i)=abs(b-a);

i=i+1;

end

disp(['r=',num2str(r(end),10),' with an absolute error=',num2str(error(end)),' in n=',num2str(i-1)])

r;

error;

x=xc;

end

a=input('a=');

b=input('b=');

tol=1e-03;

f=@(x) x.^3-9;

tol=1e-3;

Secant(f,a,b,tol);

a=2

b=3

r=2.080083266 with an absolute error=0.00012474 in n=4

Published with MATLAB® R2014a

Page 2: matsec

function [ xc ] = regulafalsi(f,a,b,tol)

r(1)=a;

r(2)=b;

i=1;

error=abs(b-a);

while error>tol

fa=f(a);

fb=f(b);

xc=b-(fb*(b-a))/(fb-fa);

r(i+1)=xc;

if fa*f(xc)<0

a=b;

b=xc;

else

b=a;

a=xc;

end

error(i)=abs(b-a);

i=i+1;

end

disp(['r=',num2str((r(end)),10),' with an absolute error=',num2str(error(end)),' in n=',num2str(i-1)])

x=xc;

end

a=input('a=');

b=input('b=');

tol=1e-03;

f=@(x) x.^3-9;

tol=1e-3;

Secant(f,a,b,tol);

a=2

b=3

r=2.080083823 with an absolute error=0 in n=12

Published with MATLAB® R2014a