Sie sind auf Seite 1von 2

%metodo de la secante

clear,clc
fx=input('ingrese la funcion fx=','s');
syms x
f=inline(fx);
x0=input('ingrese el primer valor: ');
x1=input('ingrese el segundo valor: ');
tol=input('ingrese la tolerancia: ');
error=100;
n=0;
fprintf(' n
x0
x1
x2
error\n');
fprintf(' %i
%4.4f %4.4f ---- -----\n',n,x0 , x1');
while(error>tol)
x2=x1-(x1-x0)*f(x1)/(f(x1)-f(x0));
error=abs(f(x2));
fprintf(' %i %4.4f %4.4f %4.4f\n',n,x0 , x1, x2 , error\n');
x0=x1;
x1=x2;
n=n+1;
end

ingrese la funcion fx=exp(-x)-x


ingrese el primer valor: 0
ingrese el segundo valor: 1
ingrese la tolerancia: 0.00001
n x0

x1

x2

error

0 0.0000 1.0000 ---- ----0 0.0000 1.0000 0.6127


0 1 1.0000 0.6127 0.5638
1.929625e+02 2 0.6127 0.5638 0.5672
4.714841e+04 3 0.5638 0.5672 0.5671
1.182025e+08 >>
RESPUESTA DE LA PREGUNTA 6
ingrese la funcion fx=sin(x)+cos(1+x^2)-1
ingrese el primer valor: 1
ingrese el segundo valor: 3
ingrese la tolerancia: 0.1
n x0

x1

x2

error

0 1.0000 3.0000 ---- -----

0 1.0000 3.0000 -0.0232


0 1 3.0000 -0.0232 -1.2263
3.643319e-01 2 -0.0232 -1.2263 0.2340
7.280212e+00 3 -1.2263 0.2340 0.3964
1.415493e+01 4 0.2340 0.3964 0.9447
7.908081e+00 5 0.3964 0.9447 0.0009
1.089834e+01 6 0.9447 0.0009 -9.2065
3.317613e+00 7 0.0009 -9.2065 3.1306
5.918041e+00 8 -9.2065 3.1306 26.4524
7.849797e+00 9 3.1306 26.4524 171.6552
6.256403e+01 10 26.4524 171.6552 195.5193
1.534198e+01 11 171.6552 195.5193 164.8969
1.790096e+02 >>

Das könnte Ihnen auch gefallen