Você está na página 1de 3

G:\ICADS-UFOB\ENSI_\CURSOS\DISCIPLINAS\...-ESA&ECivil\2010.2\Programas\METNEWTON.

f
!
!
!
!

ICAD/UFBA
PROF. PEDRO C. S. VIEIRA
OBJETIVO: CALCULAR A RAIZ DE UMA EQUACAO PELO METODO DE NEWTON
DATA ULTIMA ATUALIZACAOO: 20.07.10
PROGRAM NEWTON
IMPLICIT NONE
COMMON/C0/N
COMMON/C1/P,DP,E1,E2,E3,F1,F2,F3,F4,F5,F6
INTEGER:: I,ITERMAX,N
REAL *8:: TOLER,A,B,P,DP,X0,FXA,FXB,E1,E2,E3,F1,F2,F3,F4,F5,F6
REAL *8, DIMENSION (:), ALLOCATABLE::FX,DFX,X,ERRO,C,D
CHARACTER*1 ::TESTE

12

13

17

14

16

!
!
!

30

!
20

21

22

!
23

24

25

26

WRITE(*,1)
FORMAT(/,'CALCULAR A RAIZ DE UMA EQACAO PELO METODO DE NEWTON',//&
&)
WRITE(*,12)
FORMAT(T1,'LIMITE INFERIOR (A):')
READ(*,*) A
WRITE(*,13)
FORMAT(/,T1,'LIMITE SUPERIOR (B):')
READ(*,*) B
WRITE(*,17)
FORMAT(/,T1,'VALOR INICIAL (XO):')
READ(*,*) X0
WRITE(*,14)
FORMAT(/,T1,'TOLERANCIA (TOLER):')
READ(*,*) TOLER
WRITE(*,16)
FORMAT(/,T1,'NUMERO MAXIMO DE ITERACOES (ITERMAX):')
READ(*,*)ITERMAX
FUNO POLINOMIAL COM TERMOS SENO, COSENO E EXPONENCIAL
F(X)=C1*X^D1+C2*X^D2+...+CN-1*X^DN-1+CN*X^DN+
+E1*X^F1*SEN(X)^F2+E2*X^F3*COS(X)^F4+E3*X^+F5*E(X)^F6=0
WRITE(*,30)
FORMAT(/,'POLINOMIO A SER CALCULADO',//,'F(X)=C1*X^D1+C2*X^D2+...+
&CN-1*X^DN-1+CN*X^DN',/,'+E1*X^F1*SEN(X)^F2+E2*X^F3*COS(X)^F4+E3*X^
&F5*E(X)^F6=0',/)
TERMOS DA PARTE POLINOMIAL
WRITE(*,20)
FORMAT(/,'ENTRE COM A QUANTIDADE DE TERMOS(N) PARA C E D:')
READ*,N
ALLOCATE (C(N),D(N))
IF (N.NE.0) THEN
WRITE(*,21)
FORMAT(/,'ENTRE COM AS CONSTANTES C(I) E D(I)')
DO I=1,N
WRITE(*,22)I,I
FORMAT('C (',I3,'),','D(',I3,'):')
READ*, C(I),D(I)
END DO
ENDIF
TERMOS COM FUNES SENO, COSENO E EXPONENCIAL
WRITE(*,23)
FORMAT(/,'EXISTE TERMOS PARA FUNCAO SENO?',/,'SIM (S) OU NAO (N)
&:')
READ*,TESTE
IF ((TESTE.EQ.'S').OR.(TESTE.EQ.'s')) THEN
WRITE(*,24)
FORMAT(/,'ENTRE COM AS CONSTANTES E1,F1 e F2:')
READ*, E1,F1,F2
END IF
WRITE(*,25)
FORMAT(/,'EXISTE TERMOS PARA FUNCAO COSENO?',/,'SIM (S) OU NAO (N)
&:')
READ*,TESTE
IF ((TESTE.EQ.'S').OR.(TESTE.EQ.'s')) THEN
WRITE(*,26)
FORMAT(/,'ENTRE COM AS CONSTANTES E2,F3 e F4:')
READ*, E2,F3,F4
END IF

G:\ICADS-UFOB\ENSI_\CURSOS\DISCIPLINAS\...-ESA&ECivil\2010.2\Programas\METNEWTON.f
27

28

WRITE(*,27)
FORMAT(/,'EXISTE TERMOS PARA FUNCAO EXPONENCIAL?',/,'SIM (S) OU NA&
&O (N):')
READ*,TESTE
IF ((TESTE.EQ.'S').OR.(TESTE.EQ.'s')) THEN
WRITE(*,28)
FORMAT(/,'ENTRE COM AS CONSTANTES E3,F5 e F6:')
READ*, E3,F5,F6
END IF
ALLOCATE(ERRO(ITERMAX-1),FX(ITERMAX),DFX(ITERMAX),X(ITERMAX))

TESTE DO INTERVALO
CALL PX(A,C,D)
FXA=P
CALL PX(B,C,D)
FXB=P
IF (FXA*FXB.GT.0) THEN
WRITE(*,*) "NAO EXISTE RAIZ NESTE INTERVALO"
STOP
ENDIF

11
2

31

PROCESSO ITERATIVO DO MTODO DE NEWTON


X(1)=X0
DO I=1,ITERMAX
CALL PX(X(I),C,D)
FX(I)=P
DFX(I)=DP
X(I+1)=X(I)-(FX(I)/DFX(I))
IF (I.EQ.1) THEN
WRITE(*,11)
FORMAT(//,T1,'ITER',T9,'X',T19,'FX',T29,'DFX',T39,'ERRO')
WRITE(*,2)I,X(I),FX(I),DFX(I)
FORMAT(T1,I3,T6,F8.4,T16,F8.4,T26,F8.4,T40,'-')
ELSE
ERRO(I)=ABS(X(I)-X(I-1))
WRITE(*,3)I,X(I),FX(I),DFX(I),ERRO(I)
FORMAT(T1,I3,T6,F8.4,T16,F8.4,T26,F8.4,T36,F8.4)
IF (ERRO(I).LE.TOLER) THEN
WRITE(*,31)X(I)
FORMAT(/,'A RAIZ :',F12.7)
STOP
ELSE IF (I.GT.ITERMAX) THEN
WRITE(*,*)'O NUMERO DE ITERACOES NAO FOI SUFICIENTE'
ENDIF
ENDIF
END DO
END PROGRAM NEWTON
SUBROUTINE PX(X,C,D)
INTEGER ::J
REAL *8::X,P,DP
REAL, PARAMETER :: pi=3.14159
REAL *8,DIMENSION (N)::C,D
COMMON/C0/N
COMMON/C1/P,DP,E1,E2,E3,F1,F2,F3,F4,F5,F6

CALCULA O VALOR DA FUNCAO NO PONTO X (P) E SUA DERIVADA (DP)

!
!

F(X)=C1*X^D1+C2*X^D2+...+CN-1*X^DN-1+CN*X^DN+
+E1*X^F1*SEN(X)^F2+E2*X^F3*COS(X)^F4+E3*X^+F5*E(X)^F6=0

!
!
!
!
!

DF(X)=D1*C1*X^(D1-1)+D2*C2*X^(D2-1)+...+DN-1*CN-1*X^((DN-1)-1)+
DN*CN*X^((DN)-1)+
(E1*F1*X^(F1-1)*SIN(X)^F2)+(E1*F2*X^F1*SIN(X)^(F2-1)*COS(X))+
(E2*F3*X^(F3-1)*COS(X)^F4)-(E2*F4*X^F3*COS(X)^(F4-1)*SIN(X))+
F5*F6*EXP(X)^F6=0
P=0.0
DP=0.0
IF (N.NE.0) THEN
DO J=1,N
P=P+C(J)*X**D(J)

G:\ICADS-UFOB\ENSI_\CURSOS\DISCIPLINAS\...-ESA&ECivil\2010.2\Programas\METNEWTON.f
DP=DP+D(J)*C(J)*X**(D(J)-1)
END DO
ENDIF
P=P+E1*X**F1*sin(X)**F2+E2*X**F3*cos(X)**F4+F5*exp(X)**F6
DP=DP+E1 * X ** F1 * F1 / X * sin(X) ** F2 + E1 * X ** F1 * sin(X)
# ** F2 * F2 * cos(X) / sin(X) + E2 * X ** F3 * F3 / X * cos(X) **
#F4 - E2 * X ** F3 * cos(X) ** F4 * F4 * sin(X) / cos(X) + F5 * exp
#(X) ** F6 * F6

END