Please help me: a problem on my FOTRAN program

  • Thread starter Thread starter mohammedalwar
  • Start date Start date
  • Tags Tags
    Program
AI Thread Summary
A user developed a FORTRAN program to solve the equation f(t0,y0) = 1/2*(t-y) using the Euler method but encountered an issue where the output was consistently returning the same value (1.0) for all iterations. After sharing the code, it was identified that the problem stemmed from using integer division with the expression 1/2, which resulted in the function always returning 0. The solution involved replacing 1/2 with 0.5 to ensure proper floating-point division. After making this correction, the program began to run correctly. The user also acknowledged a minor syntax error in the language name, confirming their learning process in FORTRAN.
mohammedalwar
Messages
2
Reaction score
0
Friends,
I have written a program in FOTRAN for solving a equation [f(t0,y0)= 1/2*(t-y) by EulerMethod

I'm in the learning stage of FOTRAN.
after executing my program and save the result in txt file
the result For example be
1.000000000000000 1.000000000000000
2.000000000000000 1.000000000000000
3.000000000000000 1.000000000000000
4.000000000000000 1.000000000000000
5.000000000000000 1.000000000000000
6.000000000000000 1.000000000000000
7.000000000000000 1.000000000000000
8.000000000000000 1.000000000000000
9.000000000000000 1.000000000000000
I don't know what the problem in this function; because the other function working perfectly
as f(t0,y0)= (t+y)

So please don't mind if my program has got any silly mistakes.




My code



program EulerMethod
implicit none
real(8)::a,b,h,y_0,t

print*,"Enter the interval a,b ,the value of the step-size h and the value of y_0"
read*,a,b,h,y_0

open(1, file="resultEuler.txt",status='new',position="rewind")

call euler(y_0,a,b,h)
close(1)

contains
subroutine euler(y_0,a,b,h)
implicit none
real(8), intent(inout)::a,h,b,y_0
real(8):: y,t
t=a
y=y_0
do while(t<=b)
y=y+h*f(y,t)
write(1,*)t, y
t=t+h
end do
end subroutine
function f(y,t)
implicit none
real(8)::f,y,t

f=1/2*(t-y)


end function


end program EulerMethod
 
Technology news on Phys.org
Please provide the input a,b,h,y_0 for a simple example and what the output should be.

Just saying "it prints 1,2,3,4,5,6,7,8. what is wrong with it?" doesn't give enough information for someone to be able to step through the problem and find the error.
 
I suspect that your f function is causing problems.
Code:
function f(y,t)
 implicit none
 real(8)::f,y,t
 
f=1/2*(t-y)
end function

In the value you return, you should replace 1/2 by 0.5. I'm pretty sure that 1/2 is causing your function to always return 0. This is because 1/2 evaluates to 0. In other words, integer division is being performed, not floating point division.

BTW, the name of the language is FOR[/color]TRAN.
 
^_______^ Thanks guys
thank you Mark44 for found the problem ,,, my program running perfectly ...

BTW, I know the name of this language is ForTran ,,,, but i had syntax error hhhhhhhh
... Thank you again
Bye :)
 
mohammedalwar said:
^_______^ Thanks guys
thank you Mark44 for found the problem ,,, my program running perfectly ...
Good to hear.
mohammedalwar said:
BTW, I know the name of this language is ForTran ,,,, but i had syntax error hhhhhhhh
... Thank you again
Bye :)
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Replies
15
Views
33K
Replies
3
Views
3K
Replies
8
Views
2K
Replies
20
Views
2K
Replies
3
Views
3K
Back
Top