FORTRAN问题

1 fortran

在Fortran需要帮助......

这是该程序的主循环..

do iStep=0,nStep
    write(7,*)iStep

    !* Compute new temperature using FTCS scheme.
    do i=1,N
        if( istep==0) then  !only for t=0
            tt_new(i)=250
    write(7,*)tt_new(i)
        else
            if(i==1) then
                tt_new(i)=2*coeff*(tt(i+1)+35.494)-0.036*tt(i)
                write(7,*)tt(i)
            else 
                if(i==N) then
                    tt_new(i)=2*coeff*(tt(i-1)+35.494)-0.036*tt(i)
                    write(7,*)tt(i)
                else           
                    tt_new(i) = coeff*(tt(i+1) + tt(i-1)+33.333)+(1 - 2*coeff)*tt(i)
                    write (7,*) tt_new(i)
                end if
            end if
        end if     
    end do

    do i=1,N
        tt(i) = tt_new(i)     ! Reset temperature to new values
    enddo
end do
Run Code Online (Sandbox Code Playgroud)

这是输出....

0
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
2.5000000E+02
1
2.5000000E+02     <--
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.6666650E+02
2.5000000E+02   <--
Run Code Online (Sandbox Code Playgroud)

正如你所看到的......程序不计算第一个和最后一个节点的值......你能告诉我为什么???

Tim*_*omb 5

对于i=1i=N,您正在打印tt(i)而不是tt_new(i)- 正在正确执行计算,但结果将无法正确显示.在这种情况下,使用调试器逐步执行代码非常有用.

我还建议重组你的if陈述,但我不会像gimel那样 - 我认为意图会更清晰

if (iStep == 0) then
    ! Perform actions for time 0
else
    ! Perform actions for time > 0
    if (i == 1) then
        ! Perform actions for first endpoint
    else if (i == N) then
        ! Perform actions for last endpoint
    else
        ! Perform actions for midsection
    end if
end if
Run Code Online (Sandbox Code Playgroud)

因为您有两种类型的特殊情况 - 空间约束(如何以不同方式处理端点)和时间约束(如何处理初始条件).