计算cosx时出现Fortran错误 [英] Fortran error when calculating cosx
问题描述
我被分配了以下问题:
创建一个Fortran程序,该程序可以读取检查有效范围(不是类型)的程度[0-360]和它将能够从下面的等式中计算和打印 cos(x)
,其中 x
以弧度为单位:
cos(x)= 1-x ^ 2/2! + x ^ 4/4!-x ^ 6/6!+ x ^ 8/8!-...
作为一个收敛条件,假定 10 ^( - 5)
使用两次连续重复之间的绝对误差(我想它意味着do的)。
为了计算!
,应该使用最大可能类型的整数。最后,应该在屏幕上打印重复的总数。
所以我的代码是这样的:
程序ex6_pr2
隐式无
!变量和常量
integer :: i
real * 8 :: fact,fact2!fact = factorial
real,parameter :: pi = 3.14159265
double precision :: degree,radiants,cosradiants,s,oldcosradiants,difference!degree,radiants = angle
print *,'该程序读取并计算角度' (角度<0度或> 360度),请输入角度'
read *,degree
'的度数!数字范围
read *,degree
print *,'Error input degree'
cycle
end do
radiants =(degree * pi / 180)
fact = 1
fact2 = 1
s = 0
cosradiants = 0
!重复结构
do i = 2,200,1
fact = fact * i
fact2 = fact2 *(i + 2)
oldcosradiants = cosradiants
cosradiants =( - (radiant)** i / fact)+((radiant)**(i + 2))/ fact2)
差额= cosradiants-oldcosra (abs(差异)<1e-5)退出
end do
!打印结果
print *,s + 1。
结束程序
我得到正确的结果,例如45度或 pi / 4
),对于其他例如90度或180度的错误。我检查了我的分解因子我相信这个错误是隐藏的(至少对我来说)。
那么我创建了另一个代码,由于以下错误似乎无法运行:FUNCTION name,(RESULT of PROJECT2_EX6〜FACT),用于没有预料到的地方,可能缺少'()'bb
$ b
program project2_ex6
implicit none
integer(kind = 3):: degrees,i,sign
integer :: n
double precision :: x,err_limit,s_old,s
real,parameter :: pi = 3.14159265359
print *,'这个程序计算出cos(x)'
print *,输入角度度数
read *,degrees
do
if(degrees < ; 0.or.degrees> 360)然后
print *,'Degrees必须介于0-360'之间
其他
x = pi *度/ 180
出口
结束如果
结束
标志= 1
sign = sign *( - 1)
err_limit = 1e-5
n = 0
s = 0
s_old = 0
do
do i = 1, n
end do
s =((( - 1。)** n /(fact(2. * n)))* x **(2. * n))* sign
s = s + s_old
n = n + 1
if(abs(s-s_old)< 1e-5)then
exit
else
s_old = s
cycle
end if
end do
print *,s,i,n
包含
实函数fact(i)
double precision:fact
integer :: i $ b $ if if(i> = 1)then
fact = i * fact(i-1)
else
fact = 1
end if
return
end function
end program
虽然这是你的功课,我会帮你在这里。第一个错误的是你需要替换的因式分解
fact = 1
do j = 1 ,我
fact = fact * j
enddo
你让你的循环做这项工作,以便运行它作为
do i = 4,200,2
并且在do loob之外预定义cosradians与$ / $>
cosradiants = 1-radiants ** 2/2
另外您需要考虑改变你可以在循环中使用的符号
sign = sign *( - 1)
,并在循环之前以
sign = 1
开头
在循环中它然后
cosradiants = cosradiants +符号* radiants ** i /事实上
如果你已经包含了这些东西,它应该可以工作(至少我的代码是这样的) p>
I was assigned the following problem:
Make a Fortran program which will be able to read a degree[0-360] checking validity range(not type) and it will be able to calculate and print the
cos(x)
from the following equation, wherex
is in radians:cos(x)=1-x^2/2! + x^4/4!-x^6/6!+x^8/8!-...
As a convergence criteria assume
10^(-5)
using the absolute error between two successive repeats (I suppose it means do's).For the calculation of the
!
the greatest possible kind of integer should be used. Finally the total number of repeats should be printed on screen.So my code is this:
program ex6_pr2 implicit none !Variables and Constants integer::i real*8::fact,fact2 !fact=factorial real,parameter::pi=3.14159265 double precision::degree,radiants,cosradiants,s,oldcosradiants,difference !degree,radiants=angle print*,'This program reads and calculates an angle`s co-sinus' print*,'Please input the degrees of the angle' read*,degree do while(degree<0 .or. degree>360) !number range read*,degree print*,'Error input degree' cycle end do radiants=(degree*pi/180) fact=1 fact2=1 s=0 cosradiants=0 !repeat structure do i=2,200,1 fact=fact*i fact2=fact2*(i+2) oldcosradiants=cosradiants cosradiants=(-(radiants)**i/fact)+(((radiants)**(i+2))/fact2) difference=cosradiants-oldcosradiants s=s+cosradiants if(abs(difference)<1e-5) exit end do !Printing results print*,s+1. end program
I get right results for angles such as 45 degrees (or
pi/4
) and wrong for other for example 90 degrees or 180.I have checked my factorials where I believe the error is hidden (at least for me).
Well I created another code which seems unable to run due to the following error:FUNCTION name,(RESULT of PROJECT2_EX6~FACT),used where not expected,perhaps missing '()'
program project2_ex6 implicit none integer(kind=3)::degrees,i,sign integer::n double precision::x,err_limit,s_old,s real,parameter::pi=3.14159265359 print*,'This program calculates the cos(x)' print*,"Enter the angle's degrees" read*,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 do do i=1,n end do s=(((-1.)**n/(fact(2.*n)))*x**(2.*n))*sign s=s+s_old n=n+1 if(abs(s-s_old)<1e-5) then exit else s_old=s cycle end if end do print*,s,i,n contains real function fact(i) double precision::fact integer::i if(i>=1) then fact=i*fact(i-1) else fact=1 end if return end function end program
解决方案Although it is your homework, I will help you here. The first thing which is wrong is ýour factorial which you need to replace with
fact = 1 do j = 1,i fact = fact*j enddo
second it is easier if you let your do loop do the job so run it as
do i=4,200,2
and predefine cosradians outside the do loob with
cosradiants = 1-radiants**2/2
additionally you need to take into account the changing sign which you can do in the loop using
sign = sign*(-1)
and starting it off with
sign = 1
before the loopin the loop its then
cosradiants= cosradiants+sign*radiants**i/fact
If you have included these things it should work (at least with my code it does)
这篇关于计算cosx时出现Fortran错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!