Fortran 板


LINE

各位fortran版的先进大家好 小弟我又来发问了 目前要计算fortran内建函数sin(x)的值 及利用泰勒级数展开的级数和(有限项)来计算sin值 且由使用者输入一计算精度 使两者的差值达到此精度 并输出泰勒级数需要几项才能达到 附上题目: Write a Fortran program that: Reads in a value of x in degrees and then calculates the sine of x using the sine intrinsic function. Next calculate the sine of x using above truncated infinite series to the prescribed accuracy which is an input value. Be careful with how you evaluate and sum up the terms. Output the values of the sine of x calculated using both intrinsic function and the truncated series, and the number of terms of the truncated series required. 我的程式码如下: program hw6 implicit none real(kind=8) :: x !角度值 real(kind=8) :: sum=0.0 !利用truncated infinite series所计算的sin(x) real :: temp=1.0 !用来使用阶乘 real(kind=8) :: e !误差值 integer :: precision !计算精度 integer :: i=1 !回圈累加 !用来读取输入的角度和精度 write(*,*) "请输入角度:" read(*,*) x write(*,*) "请输入计算精度(准确至小数点後第几位):" read(*,*) precision !转换角度,使角度范围在0~360度 if(x>=0) then x=x-real(floor(x/360.0))*360.0 else x=x+real(floor(abs(x)/360.0)+1)*360 end if !转换角度,使角度范围在-90~90度 if(x<=90) then x=x else if(x<=270) then x=180-x else x=x-360 end if write(*,*) "转换後的角度=",x x=x*acos(-1.0)/180.0 !角度转换为径度 e=10.0**(-1.0*real(precision)) !设定期望误差值 sum=sum+x !先设定The truncated series的第一项 !利用回圈来计算sum of the truncated series !当级数和和内建函数sin(x)的误差小於等於期望误差值e时,跳出回圈 do i=i+1 temp=temp/(real(2*i-1)*real(2*i-2)) sum=sum+(-1.0)**real(i-1)*temp*x**real(2*i-1) write(*,*) "级数前",i,"项之和=",sum if(abs(sin(x)-sum)<=e) exit end do write(*,*) "sin(x)=",sin(x) !内建函数计算之结果 write(*,*) "sum of sine truncatd series=",sum !级数计算之结果 write(*,*) "需要",i,"项" !级数需要之项数 stop end 目前的问题是如果角度的输入值过大(例如输入13位数的值) 或是精度过高(例如误差要计算到小数点後第九位) 都会无法达成跳出回圈的条件 甚至出现NAN的值 想请教各位 此问题是否有解? 感谢! --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.112.233.49
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/Fortran/M.1413127013.A.61A.html
1F:推 sin55688: 先观察无法跳出回圈的原因。观察每次的结果,应该就能 10/13 20:18
2F:→ sin55688: 看出端倪。另外类似这种回圈,建义都设一个最大跌代次数 10/13 20:19
3F:→ sin55688: 的限制,避免程式挂在那边。 10/13 20:20
4F:→ charlesdc: 跑不完 => 跟收敛特性有关, nan => 跟值有关系 10/13 22:20
5F:→ screase: 感谢两位的回覆 我再研究一下 10/14 18:57







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Tech_Job站内搜寻

TOP