Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用泰勒级数计算cos的Fortran程序打印错误的结果,可能与我的阶乘错误_Fortran_Trigonometry_Taylor Series - Fatal编程技术网

用泰勒级数计算cos的Fortran程序打印错误的结果,可能与我的阶乘错误

用泰勒级数计算cos的Fortran程序打印错误的结果,可能与我的阶乘错误,fortran,trigonometry,taylor-series,Fortran,Trigonometry,Taylor Series,我一直在尝试创建一个程序,可以使用泰勒级数计算辐射源的cos。我考虑了错误,例如: cos(45)=1.4必须为:s=((-1.)**n/(事实*2.*n))*x**(2.*n))*符号 守则: program project2_ex6 implicit none !Reference to variables !------------------------------------------------------- integer(kind=3)::degrees,i,sign !sig

我一直在尝试创建一个程序,可以使用泰勒级数计算辐射源的cos。我考虑了错误,例如:

cos(45)=1.4
必须为:
s=((-1.)**n/(事实*2.*n))*x**(2.*n))*符号

守则:

program project2_ex6
implicit none
!Reference to variables
!-------------------------------------------------------
integer(kind=3)::degrees,i,sign !sign=a random name in order to use it to change the 's' sign consecutively
integer::n
double precision::x,err_limit,s_old,s,fact !x=angle in radiants,err_limit=the absolute error between two results,fact=factorial
real,parameter::pi=3.14159265359
!------------------------------------------------------
print*,'This program calculates the cos(x)'
print*,"Enter the angle's degrees"
read*,degrees
!Checking validity of degrees
!----------------------------------------------------
do
if(degrees<0.or.degrees>360) then
  print*,'Degrees must be between 0-360'
  else
   x=pi*degrees/180
    exit
    end if
    end do
    sign=1
    sign=sign*(-1)
    err_limit=1e-5
 n=0
    s=0
    s_old=0
    fact=1
!Commencing do loop
!-----------------------------------------------------------
do
  do i=1,n
    fact=fact*i
    end do
    if(n==0) then
      s=1
      else
  s=(((-1.)**n/(fact*2.*n))*x**(2.*n))*sign
 s=s+s_old
end if
n=n+1
if(abs(s-s_old)<err_limit) then
  exit
  else
    s_old=s
    cycle
    end if
  end do
  !Printing results
!-----------------------------------------------------------------
  print*,s,i,n
  end program
程序项目2\u ex6
隐式无
!对变量的引用
!-------------------------------------------------------
整数(种类=3)::度,i,符号!符号=一个随机名称,用于连续更改“s”符号
整数::n
双精度::x,错误极限,s旧,s,事实!x=以弧度表示的角度,err_limit=两个结果之间的绝对误差,fact=阶乘
实数,参数::pi=3.14159265359
!------------------------------------------------------
打印*,“此程序计算cos(x)”
打印*,“输入角度的度数”
读*,度
!检查学位的有效性
!----------------------------------------------------
做
如果(360度)那么
打印*,“度必须在0-360之间”
其他的
x=π*度/180
出口
如果结束
结束
符号=1
符号=符号*(-1)
误差极限=1e-5
n=0
s=0
s_old=0
事实=1
!开始做循环
!-----------------------------------------------------------
做
i=1,n吗
事实=事实*i
结束
如果(n==0),则
s=1
其他的
s=((-1.)**n/(事实*2.*n))*x**(2.*n))*符号
s=s+s_旧
如果结束
n=n+1

如果(abs(s-s_old)我发现了四个错误:-

(1) 在主DO循环中,fact应设置为1

(2) 在内部DO循环中,事实本身应乘以2n倍

(3) 在s的主要计算中,分母应为fact,而不是fact*2*n

(4) 在s的主要计算中,不需要乘以符号(如果需要,则应为+1)

现在45度为0.7071

program project2_ex6
implicit none
!Reference to variables
!-------------------------------------------------------
integer::degrees,i,sign !sign=a random name in order to use it to change the 's' sign consecutively
integer::n
double precision::x,err_limit,s_old,s,fact !x=angle in         radiants,err_limit=the absolute error between two results,fact=factorial
real,parameter::pi=3.14159265359
!------------------------------------------------------
print*,'This program calculates the cos(x)'
print*,"Enter the angle's degrees"
read*,degrees
!Checking validity of degrees
!----------------------------------------------------
do
 if(degrees<0.or.degrees>360) then
  print*,'Degrees must be between 0-360'
 else
   x=pi*degrees/180
    exit
  end if
end do
    sign=1
!
! Sign should be +1 or omitted
!
!-   sign=sign*(-1)
 err_limit=1e-5
 n=0
 s=0
 s_old=0

!Commencing do loop
!-----------------------------------------------------------
do
!
! Initialise fact inside do loop
!
  fact=1
!
! Change n to 2n
!
  do i=1,2*n
    fact=fact*i
   end do
  if(n==0) then
     s=1
  else
!
!Change fact*2*n to fact
!
    s=(((-1.)**n/(fact))*x**(2.*n))*sign
    s=s+s_old
  end if
 n=n+1
 if(abs(s-s_old)<err_limit) then
  exit
 else
    s_old=s
    cycle
    end if
end do
  !Printing results
!-----------------------------------------------------------------
 print*,s,i,n
 end program
程序项目2\u ex6
隐式无
!对变量的引用
!-------------------------------------------------------
整数::度数,i,sign!sign=用于连续更改“s”符号的随机名称
整数::n
双精度::x,err_limit,s_old,s,fact!x=以弧度表示的角度,err_limit=两个结果之间的绝对误差,fact=阶乘
实数,参数::pi=3.14159265359
!------------------------------------------------------
打印*,“此程序计算cos(x)”
打印*,“输入角度的度数”
读*,度
!检查学位的有效性
!----------------------------------------------------
做
如果(360度)那么
打印*,“度必须在0-360之间”
其他的
x=π*度/180
出口
如果结束
结束
符号=1
!
!符号应为+1或省略
!
!-符号=符号*(-1)
误差极限=1e-5
n=0
s=0
s_old=0
!开始做循环
!-----------------------------------------------------------
做
!
!初始化do循环中的事实
!
事实=1
!
!将n更改为2n
!
i=1,2*n吗
事实=事实*i
结束
如果(n==0),则
s=1
其他的
!
!将事实*2*n更改为事实
!
s=((-1.)**n/(事实))*x**(2.*n))*符号
s=s+s_旧
如果结束
n=n+1

如果(abs(s-s_old),你应该引用你以前的问题并说明不同之处。你得到了什么结果?为什么你认为你的代码中有错误?我真的建议你不要使用
integer(kind=3)
,您只是在将来自找麻烦。另外,为了便于我们、您的老师和您阅读您的代码,请阅读,将相应的
if
end if
与相应的
do
end do
垂直对齐。不要从一列跳到另一列。这称为缩进和缩进是非常重要的()。虽然现在很多时候我都会遇到“浮点算术溢出”(我不知道为什么),现在似乎一个小角度的度数可以正确计算。非常感谢您的时间,我真的很感激!没问题。我不知道为什么会出现溢出,但有另一种方法,您可以使用t=tprev*(-1)*x**2/(2*n-1)/(2*n)计算展开式中的每个项t这样可以避免溢出。