Eq_3_grau

isabella · updated May 20, 2022
Fortran
program Eq_3_grau

    implicit none
    
    
    integer(4)::a,b,c,d
    real(8)::delta    
    real(8)::x1,x2,x3 
    real(8)::x0
    real(8)::F,MF,dF
    real(8)::i
    real(8)::X,Y,Z
    real(8)::raizesdelta

    !leitura dos coeficientes    
    read (*,*) a
    read (*,*) b
    read (*,*) c
    read (*,*) d
    
    !impressao dos coef
    write (*,*) a
    write (*,*) b
    write (*,*) c
    write (*,*) d
    
    
    !Newton Raphson
    
    x0=(2**0.5)
    
    i= 0
    
    MF= 1.0
    
    do while (MF > 10**(-17))
        
        F = a*x0**3 + b*x0**2 + c*x0 + d
        
        dF= 3*a*x0**2 + 2*b*x0 + c
        
        if (dF == 0.0) then
        
            x1= x0 - F/(dF + 10**(-13))
            
        else 
        
        x1 = x0 - F/dF
        
        F = a*x1**3 + b*x1**2 + c*x1 + d
        
        MF= abs(F)
        
        i= i + 1
        
        x0 = x1
        end if
        
        if (i > 500) then
        
            write (*,*) "Não Convergiu"
            
        end if
        
        write(*,*) "x1=",x0
    
    end do
    
    ! Divisão de polinômios
    
    X = a 
    
    Y = a*x0 + b
    
    Z = a*x0**2 + b*x0 + c
    
    !Solução Equação de 2 grau

    !Calculo do valor delta    
    
    delta = Y**2 - 4*X*Z
    
    if (delta>=0) then
       
        x1=(-Y - delta**0.5)/(2*X)
        
        x2=(-Y + delta**0.5)/(2*X)
        
        write(*,*) "x2=",x1
        
        write(*,*) "x3=",x2
    
    else 
        
        raizesdelta=complex(0,abs(delta))
        
        x1=(-Y - raizesdelta)/(2*X)
        
        x2=(-Y + raizesdelta)/(2*X)
        
        write(*,*) "x2=",x1
        
        write(*,*) "x3=",x2
    
    end if

end program Eq_3_grau
Output
(Run the program to view its output)

Comments

Please sign up or log in to contribute to the discussion.