Está en la página 1de 4

Codigo en matlab “metodo de la secante”

clc

clear

f=input('Ingrese la funcion:\n','s');;

f=inline(f);

x0=input('Ingrese el primer punto inicial:\n');;

x1=input('Ingrese el segundo punto inicial:\n');

xra=0;

xr=0;

tol=0.00000001;

i=1;

error_aprox=1;

error=0;

fx1=f(x1);

fx0=f(x0);

xr=x1-((x1-x0)/(fx1-fx0))*fx1;

fprintf('It. X0 X1 Xr Error aprox \n');

fprintf('%2d \t %11.7f \t %11.7f \t %11.7f \t %11.7f \n',i,x0,x1,xr,error);

while error_aprox >= tol,

xra=xr;

x1=xr;

fx1=f(x1);

fx0=f(x0);

xr=x1-((x1-x0)/(fx1-fx0))*fx1;

error = abs((xr - xra) / xr);

error_aprox = error;

i=i+1;
fprintf('%2d \t %11.7f \t %11.7f \t %11.7f \t %11.7f \n',i,x0,x1,xr,error_aprox);

end

corrida

del primer ejercicio

Ingrese la funcion:

(x*x)-(2*x)-1

Ingrese el primer punto inicial:

2.6

Ingrese el segundo punto inicial:

2.5

It. X0 X1 Xr Error aprox

1 2.6000000 2.5000000 2.4193548 0.0000000

2 2.6000000 2.4193548 2.4145299 0.0019983

3 2.6000000 2.4145299 2.4142331 0.0001230

4 2.6000000 2.4142331 2.4142148 0.0000076

5 2.6000000 2.4142148 2.4142136 0.0000005

6 2.6000000 2.4142136 2.4142136 0.0000000

7 2.6000000 2.4142136 2.4142136 0.0000000

Segundo ejercicio

Ingrese la funcion:

(x*x)-x-3

Ingrese el primer punto inicial:

1.7

Ingrese el segundo punto inicial:

1.67

It. X0 X1 Xr Error aprox


1 1.7000000 1.6700000 2.4637131 0.0000000

2 1.7000000 2.4637131 2.2721126 0.0843270

3 1.7000000 2.2721126 2.3089944 0.0159731

4 1.7000000 2.3089944 2.3015299 0.0032433

5 1.7000000 2.3015299 2.3030258 0.0006496

6 1.7000000 2.3030258 2.3027254 0.0001305

7 1.7000000 2.3027254 2.3027857 0.0000262

8 1.7000000 2.3027857 2.3027736 0.0000053

9 1.7000000 2.3027736 2.3027760 0.0000011

10 1.7000000 2.3027760 2.3027756 0.0000002

11 1.7000000 2.3027756 2.3027757 0.0000000

Tercer ejercicio

Ingrese la funcion:

(x*x*x)-x+2

Ingrese el primer punto inicial:

1.5

Ingrese el segundo punto inicial:

-1.52

It. X0 X1 Xr Error aprox

1 1.5000000 -1.5200000 -1.5263980 0.0000000

2 1.5000000 -1.5263980 -1.5031919 0.0154379

3 1.5000000 -1.5031919 -1.5881465 0.0534930

4 1.5000000 -1.5881465 -1.2877907 0.2332335

5 1.5000000 -1.2877907 -2.4673651 0.4780705

6 1.5000000 -2.4673651 0.4345154 6.6784296


7 1.5000000 0.4345154 -0.3535555 2.2289877

8 1.5000000 -0.3535555 -3.0876001 0.8854918

También podría gustarte