subroutine Newmark(TOL, lnit, ave_erru, ave_erru1, errU_max, errV_max, t_errU_max, t_errV_max)
use work

implicit real(8) (a-h, o-z)
real(8) m1

t_errU_max =t0
t_errV_max =t0

u  =u0
u1 =u10
u2 =u20

MAIN_DO: DO ipas= 1, numar_pasi
	t1 =t0 +ipas*dt

	u0  =u
	u10 =u1
	u20 =u2

	u  =u0  +u10*dt +u20*dt2/2. 
	u1 =u10 +u20*dt 
	u2 =u20

	if(liniar) then
	dp = p(t1) -m*u2 -c*u1 -k*u
	m1 = m +c*g*dt +k*b*dt2
	du2= dp/m1

	else	! solve by Newton's method
	! iteration:
	w0 =0_8
	newton_do: do iter =1, lnit

! mu'' + ga(u,u') +fk(u) =p(t):
!	f =m*(u2+w0) +fk(u +b*dt2*w0) +ga(u +b*dt2*w0, u1 +g*dt*w0) -p(t1)
!	df =m +b*dt2*dfk(u +b*dt2*w0) +b*dt2*dga(u +b*dt2*w0, u1 +g*dt*w0) +dga1(u +b*dt2*w0, u1 +g*dt*w0)*g*dt
	x  =  u + b*dt2*w0
	x1 = u1 + g*dt*w0
	x2 = u2 + w0       

	f =m*x2 +fk(x) +ga(x,x1) -p(t1)
	df =m +b*dt2*dfk(x) +b*dt2*dga(x, x1) +g*dt*dga1(x, x1)

	w =w0 -f/df	
	if( dabs(w -w0) <= TOL) then
	exit newton_do
	else
	w0 =w
	endif
	enddo newton_do

77	du2 =w
	endif

	u  = u +b*dt2*du2
	u1 =u1 +g*dt*du2
	u2 =u2 +du2

if(true_solution) then
UE =ufct(t1)
eru =UE-u
ave_erru =ave_erru +eru*eru
!errU_max =dmax1(errU_max, dabs(eru))
if(dabs(eru) > errU_max) then
errU_max =dabs(eru)
t_errU_max =t1
endif

U1E =vfct(t1)
eru1 =U1E-u1
ave_erru1 =ave_erru1 +eru1*eru1
!errV_max =dmax1(errV_max, dabs(eru1))
if(dabs(eru1) > errV_max) then
errV_max =dabs(eru1)
t_errV_max =t1
endif

endif


IF(npas ==1 .or. mod(ipas, npas) ==0) THEN
if(true_solution) then
write(2,70) t1, u, u1, eru, eru1
else
write(2,70) t1, u, u1
endif

if(write_dep) then
	if(npas ==1 .or. mod(ipas, npas_dep) ==0) then
	write(3,70) t1, u
	endif
endif
ENDIF


ENDDO MAIN_DO

70	format(' ',f10.3, 2(1pG21.11), 2(1pG21.3))

60	format &
('      T        U                    V                            ERR(U)               ERR(V)'/)

! Write results at tEnd:
write(1,'(/, a)') ' Rezultate la tEnd:'
write(1,60)
if(true_solution) then
write(1,70) t1, u, u1, eru, eru1
else
write(1,70) t1, u, u1
endif
END
