Newton Raphson method (convergence problem)

  • Thread starter Thread starter KarAnw
  • Start date Start date
  • Tags Tags
    Method Newton
AI Thread Summary
The discussion centers around a user new to FORTRAN who is implementing the Newton-Raphson method for calculations but is facing issues with convergence in their results. The user shares their code, which involves complex calculations related to free energy, and expresses concerns about the number of iterations and the handling of summations in their code. Suggestions from other participants include increasing the number of iterations to improve convergence and ensuring proper summation of exponential series. The user has made modifications to their code based on feedback, particularly in the expressions for derivatives and the calculation of Keff. They also acknowledge the importance of proper data type assignments for variables to enhance coding practices. The conversation emphasizes troubleshooting techniques and code optimization for better performance in numerical methods.
KarAnw
Messages
3
Reaction score
0
Hello, I am new in the field of FORTRAN. I started to write a code using Newton Raphson method. Below is the code of main program portion only. During my calculation, I have found that my results are not converging, Any help or advice will be highly appreciated in this case.
Thank you for the effort of helping in advance. Program Free_energy
Implicit None
Integer:: i,j,l,k
Double Precision Gmic,U,Phi
Double Precision s,Uderv,cc,DG0,cf,ctot1,
& Keff,Cagg,ctot,sum,Cagg1,cnew,
& Cagg2,cctot1,kapp,error,Csalt,Csalt1,
& cext
Double precision :: Nelec
Double precision :: Nwat
Double precision :: Nlig

ctot = 1D0
cf=ctot/2
cext = 0.01

sum=0.0
DG0 = -100.0

Do While(DG0.le.20)

do i=0,2
do j=0,10
do l=0,10

Nelec=i
Nwat=j
Nlig=l

Keff=dexp(-Gmic(Nelec,Nwat,Nlig)-Nelec*DG0)
Cagg=Keff*((cf)**Nlig)
Cagg1=sum+Nlig*Cagg
Cagg2=sum+(Nlig**2)*Cagg

enddo
enddo
enddo

ctot1=ctot-Cagg1
cctot1=ctot-Cagg2

cnew = cf-(ctot1/cctot1)

error = abs((cnew-cf)/cnew)

cf = cnew

write(72,*) cf, error
if(error.lt.1D-7) exit

Csalt1 = 0.0

do i=0,2
do j=0,10
do l=0,10

Nelec=i
Nwat=j
Nlig=l

Keff = dexp(-Gmic(Nelec,Nwat,Nlig))
Csalt = Csalt1+Keff*((cf)**Nlig)*Nelec

enddo
enddo
enddo

Kapp = Csalt/cext

write(70,*) DG0, -log(kapp)/2
write(72,*) cf, -log(kapp)/2

DG0 = DG0 + 0.1

enddo
 
Technology news on Phys.org
I find that graphing the results of the iterations can be helpful.
 
Up the number of iterations and check for their convergence...it seems to me at first glance that you are not iterating enough.
 
First I would like to thank you both for the reply,

Since you have mentioned that increasing the iteration number may solve the problem, but my question is how can I do that?

Also in my code, the function has summation over an exponential series of the Gimc, I am afraid of that also because I think that summation is also not happening there properly.
Further, I made two changes in the expression of the derivative of my function (Cagg2) and the expression of Keff in my code. The rest of the code is unchangeable and almost like the previous one except that two lines. I ran this one and I find this plot (-log(Kapp)/2 vs DG0) attached with this reply. My aim was to calculate the "cf" which has also the negative values in the calculated results.

I would like to thank you again for the help. Any advice or direction in this case will become helpful to me in near future.

Note: For your interest, the new corrected version of the code (only the main program portion only) has been given below,

Program Free_Energy
Implicit None
Integer:: i,j,l,k
Double Precision Gmic,U,Phi
Double Precision s,Uderv,cc,DG0,cf,ctot1,
& Keff,Cagg,ctot,sum,Cagg1,cnew,
& Cagg2,cctot1,kapp,error,Csalt,Csalt1,
& cext
Double precision :: Nelec,kB,T
Double precision :: Nwat
Double precision :: Nlig

ctot = 1D0
cf=ctot/2
cext = 0.01

DG0 = -100.0
Do While(DG0.le.20)

do i=0,2
do j=0,10
do l=0,10

Nelec=i
Nwat=j
Nlig=l

Keff=dexp(-Gmic(Nelec,Nwat,Nlig)-Nelec*DG0)
Cagg=Keff*((cf)**Nlig)
Cagg1=sum+Nlig*Cagg
Cagg2=sum+(Nlig**2)*Keff*((cf)**(Nlig-1)) enddo
enddo
enddo

ctot1=ctot - Cagg1
cctot1=ctot - Cagg2
cnew = cf-(ctot1/cctot1)

error = abs((cnew-cf)/cnew)

cf = cnew

write(74,*) cf,error

if(error<1D-7)exit

Csalt1 = 0.0

do i=0,2
do j=0,10
do l=0,10

Nelec=i
Nwat=j
Nlig=l

Keff = dexp(-Gmic(Nelec,Nwat,Nlig))
Csalt = Csalt1+Keff*((cf)**Nlig)*Nelec

write(73,*) cf
enddo
enddo
enddo

Kapp = Csalt/cext

write(70,*) DG0, -log(Kapp)/2

write(72,*) cf, -log(Kapp)/2

DG0 = DG0 + 0.1

enddo
End
 

Attachments

  • app_free.png
    app_free.png
    4.5 KB · Views: 560
part of your issue is that for example you define Nwat as a double, then assign an integer to it, yes it theoretically works, but better programming style is

Nwat = double(j)

it makes a double out of the j so that it is j.00000000
 
Thank you for the reply, I will follow your suggestion and let you know about the result.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top