Fortran Coder

标题: 寻求帮助 [打印本页]

作者: IDCY    时间: 2023-9-12 11:19
标题: 寻求帮助
本帖最后由 IDCY 于 2023-9-12 15:47 编辑

假设两个txt文件
txt1
t=    x=   y=
0     1      2
0     2      3
0      3      4
txt2
t=    x=   y=
0     1      9
0     2      4
0      3      1

计算txt2中y的值减去txt1中y的值的绝对误差,即abs(y1-y2)
并将结果保存到一个新的txt文件

代码在下面
数据在附上


结果运行出不来


是这两个dat文件中的数据存在问题嘛
如果要修改的话怎么修改


下载.png (17.04 KB, 下载次数: 102)

下载.png

Riemann.dat

1.68 KB, 下载次数: 1

result.dat

1.27 KB, 下载次数: 0


作者: IDCY    时间: 2023-9-12 15:43
program main
implicit none
integer,parameter:: nx=21,ny=4
real::er(nx,ny),ef(nx,ny),e(nx,ny)
integer i,j
call read_data("C:\Users\20455\Desktop\error\result.dat",er)
call read_data("C:\Users\20455\Desktop\error\Riemann.dat",ef)
do i=1,nx
do j=2,ny
e(i,j)= abs(er(i,j)-ef(i,j))
end do
end do
e(:,1)=er(:,1)

call write_data("C:\Users\20455\Desktop\error\error.dat",e)
end program main

subroutine read_data(filename,data)
implicit none
integer i,j
integer,parameter:: nx=21,ny=4
character*(*),intent(in)::filename
real,intent(out)::data(nx,ny)
open(10,file=filename)
do i=1,nx
do j=2,ny
read(10,*)(data(i,j))
end do
end do
close(10)
end subroutine read_data

subroutine write_data(filename,data)
implicit none
integer i,j
integer,parameter:: nx=21,ny=4
character*(*),intent(in)::filename
real,intent(in)::data(nx,ny)
open(10,file=filename)
do i=1,nx
do j=1,ny
write(10,*)(data(i,j))
end do
end do
close(10)
end subroutine write_data
作者: IDCY    时间: 2023-9-12 16:05
好了 ,我已经改好了
是其中的j=2,ny的问题 应该是j=1,ny
作者: IDCY    时间: 2023-9-12 16:06
IDCY 发表于 2023-9-12 15:43
program main
implicit none
integer,parameter:: nx=21,ny=4

subroutine read_data(filename,data)
implicit none
integer i,j
integer,parameter:: nx=21,ny=4
character*(*),intent(in)::filename
real,intent(out)::data(nx,ny)
open(10,file=filename)
do i=1,nx

read(10,*)(data(i,j),j=1,ny)
end do

close(10)
end subroutine read_data

subroutine write_data(filename,data)
implicit none
integer i,j
integer,parameter:: nx=21,ny=4
character*(*),intent(in)::filename
real,intent(in)::data(nx,ny)
open(10,file=filename)
do i=1,nx

write(10,*)(data(i,j),j=1,ny)
end do

close(10)
end subroutine write_data
作者: IDCY    时间: 2023-9-12 16:07
已解决,修改后的代码附上




欢迎光临 Fortran Coder (http://bbs.fcode.cn/) Powered by Discuz! X3.2