Anda di halaman 1dari 20

Mtodos numricos

computacionais
Cesar, Mario, Vinicius e Wagner
Equao:

Representao grfica:
Newton
Algoritmo:
function [x,k]=newton(f,df,x,tol,maxit)
tic;
k=0; fx=feval(f,x);
if abs(fx)<tol
fprintf('A raiz eh x(%3i)=%8.4f\n',k,x)
return
end

while(1)
k=k+1; fx=feval(f,x) ; fl=feval(df,x);
x0=x ; x=x-fx/fl ; fx=feval(f,x);
deltax=fx/fl;
Newton if (abs(fx)<tol | abs(x-x0)<tol)
fprintf('Raiz: x(%2i)=%.6f\n',k,x)
t2=toc;
fprintf('tempo de execuao t(%f)\n',t2)
erro= tol-deltax;
fprintf('Raiz: %.6f\n',erro)
return
end

if (k==maxit)
disp('Maxit atingido. Raiz nao encontrada')
return
end
end
Equao:
+
=
2

Representao grfica:

Bisseco

Condies:
Iterao 1

Bisseco

A,B,X
Iterao 2

Bisseco

A,B,X
Iterao 3

Bisseco

A,B,X
Iterao 4

Bisseco

A,B,X
Iterao 5

Bisseco

A,B,X
Algoritmo:
function x=bissec(f,a,b,tol,maxit)
tic;
fa=feval(f,a);
fb=feval(f,b);

if fa*fb>0.0
error('A funo tem o mesmo sinal nos pontos extremos.')
end
for k=1:maxit
x=(a+b)/2;
fx=feval(f,x);

Bisseco if abs(b-a)<=tol
t2=toc;
erro=b-a;
fprintf('A raiz encontrada eh x(%i)=%.6f\n',k,x)
fprintf('erro=%.6f\n',erro)
fprintf('tempo de execuao t(%f)\n',t2)
break;
end

if ((fx*fa)< 0)
b=x;
else
a=x;
fa=fx;
end;
Equao:

Representao grfica:
Secante
Algoritmo:
function xfinal=secante(fx,a,b,tol,maxit)
tic;
x(1)=a;
x(2)=b;
y(1)=feval(fx,x(1));
y(2)=feval(fx,x(2));

erro=1; k=2;
while erro>tol & k<maxit
x(k+1)=x(k)-y(k)*(x(k)-x(k-1))/(y(k)-y(k-1));
y(k+1)=feval(fx,x(k+1));
erro(k)=( abs(x(k+1)-x(k) )) ;
if erro(k)<tol;
disp('O metodo da Secante converge com |x(k)-x(k-1)| < tolerancia.')
xfinal=x(k+1);

Secante break;
end

if (y(k+1)==0)
disp('Encontrado o valor exato da raiz.');
break;
end;
k=k+1;
end

if (k==maxit) & (abs(erro(k-1))>=tol)


disp('A raiz nao foi encontrada com a tolerancia desejada.')
end

t2=toc;
fprintf('A raiz encontrada eh x(%i)=%.6f',k,xfinal)
fprintf('erro=%.6f\n',erro)
fprintf('tempo de execuao t(%f)\n',t2)
Equao: Fator:

Representao grfica:
Pgaso
Algoritmo:
function xf=pegaso(f,a,b,Toler,IterMax)
tic;
fa=feval(f,a);
fb=feval(f,b);

x = b;
fx = fb;
iter=0;
while 1
iter = iter + 1;
DeltaX = - fx/(fb-fa)*(b-a);
x = x + DeltaX;;
fx = feval(f,x);
if ( abs(DeltaX)<Toler & abs(fx)<Toler) | (iter >= IterMax)
break;
Pgaso end
if fx*fb <0
a = b;
fa = fb;
else
fa = fa * fb / (fb+fx);
end
b = x;
fb = fx;
end
Raiz = x ;
t2=toc;
fprintf('Raiz: x(%2i)=%.6f %.6f\n',iter,Raiz,t2)
if (abs(DeltaX) < Toler) & (abs(fx) < Toler)
Erro = 0
else
Erro = 1
end
y= 4x3 + x + cos(x) -10

6.21
6.21 Raz Iteraes Erro Tempo Tempo rel tol a b x0

Bisseco 1,282261 18 no 0,002118 1 10^-5 1 2 -

Secante 1,282264 7 no 0,001158 0,54674221 10^-5 1 2 -

Newton 1,282264 4 no 0,001591 0,75118036 10^-5 - - 1

Pgaso 1,282264 6 no 0,001037 0,48961284 10^-5 1 2 -


y= x4 - 2x3 + 2x -1

6.22
6.22 Raz Iteraes Erro Tempo Tempo rel tol a b x0

Bisseco 0,999999 20 no 0,002797 1 10^-5 0 3 -

Secante 0,999975 37 no 0,005676 2,02931713 10^-5 0 3 -

Newton 0,983171 9 no 0,002117 0,75688237 10^-5 - - 0

Pgaso 0,999964 46 no 0,001149 0,41079728 10^-5 0 3 -


y= (x - 2)((x -2) - 1)

6.23
6.23 Raz Iteraes Erro Tempo Tempo rel tol a b x0

Bisseco - - sim* - - 10^-5 1 5 -

Secante 1,999986 25 no 0,001011 - 10^-5 1 5 -

Newton 1,997602 8 no 0,000595 - 10^-5 - - 1

Pgaso 1,999985 27 no 0,002092 - 10^-5 1 5 -

*A funo tem o mesmo sinal nos pontos extremos(f(a).f(b)> 0).


y= (x + 1)(x - 1)(x - 3)5

6.29
6.29 Raz Iteraes Erro Tempo Tempo rel tol a b x0

Bisseco 3,0000000 36 no 0,000603 1 10^-10 2 5 -

Secante 3,0000000 139 no 0,004736 7,85406302 10^-10 2 5 -

Newton 3,0000000 7 no 0,000567 0,94029851 10^-10 - - 2

Pgaso 3,0000000 189 no 0,004402 7,30016584 10^-10 2 5 -


y= (x + 2)3(x2 + 1)1/2

6.30
6.30 Raz Iteraes Erro Tempo Tempo rel tol a b x0

Bisseco -2,000000 36 no 0,004169 1 10^-10 -3 0 -

Secante -2,0000000 78 no 0,010790 2,58815064 10^-10 -3 0 -

Newton -2,0000000 17 no 0,006926 1,66130967 10^-10 - - 0

Pgaso -2,0000000 101 no 0,014060 3,37251139 10^-10 -3 0 -


Mtodo de Newton realizou para todos os problemas o menor
nmero de interaes, seguido de bisseco, secante e Pgaso.
Quanto ao tempo de execuo os mtodos de Pgaso e Newton
foram os mais rpidos.
Logo, avaliamos que o mtodo mais eficiente o de Newton, por
possuir o menor nmero de iteraes e ser um dos mais rpidos
em tempo de execuo.

Concluses Exerccio Mtodo mais


rpido
Mtodo mais
lento
Menor nmero Maior nmero
de iteraes de iteraes
6.21 Pgaso Bisseco Newton Bisseco
6.22 Pgaso Secante Newton Pgaso
6.23 Newton Pgaso Newton Pgaso
6.29 Newton Secante Newton Pgaso
6.30 Bisseco Pgaso Newton Pgaso

Anda mungkin juga menyukai