• swift算法手记-7


        @IBAction func compute(sender: AnyObject) {
            //  19*x^7-31*x^5+16*x^2+7*x-90=0
            //  newton迭代法求一元方程的解,最大求解范围[-100000,100000]
           
         mytitle.stringValue="19*x^7-31*x^5+16*x^2+7*x-90=0"
            let trycount = 120
            var accuracy: Double = 1e-15
            var answer: Double?=nil
            
            // 预计解范围
            var leftbound:Double?=nil
            var rightbound:Double?

    =nil for var bound:Double=1;bound<10000000;bound*=10{ let leftres=comresult(-bound) let rightres=comresult(bound) if (leftres*rightres) < 0 { leftbound = (-bound) rightbound = bound break } else if leftres==0{ answer=leftbound break } else if rightres==0{ answer=rightbound break } } if (leftbound==nil || rightbound==nil){ return } var center=leftbound!+(rightbound!-leftbound!)/2 let centres:Double=comresult(center) if centres==0 { answer=center } if centres*comresult(rightbound!)<0{ leftbound=center } else if centres*comresult(leftbound!)<0{ rightbound=center } if answer==nil{ //计算方程的解 var p0=leftbound!+(rightbound!-leftbound!)/2 var p:Double for i in 1...trycount{ p = newtoncompresult(p0) if abs(p-p0) < accuracy { answer=p0 break } p0=p } } if let ans=answer{ //方程有解 result.stringValue="解:"+String(stringInterpolationSegment: ans)+" " result.stringValue += "解代入方程的值:"+String(stringInterpolationSegment:comresult(ans)) } }


    用牛顿迭代法解非线性方程





    本博客全部内容是原创,假设转载请注明来源

    http://blog.csdn.net/myhaspl/





  • 相关阅读:
    iOS启动速度优化
    iOS Instruments工具使用
    iOS开发 AFN配置https请求
    git使用教程
    iOS之 接入新浪微博 SDK(微信支付) 的坑(registerApp 的问题)
    iOS之应用间跳转
    iOS设置iTunes文件共享
    IOS平台下抓包工具使用以及抓取API接口
    用CornerStone配置SVN,HTTP及svn简单使用说明
    iOS之取消键盘遮挡
  • 原文地址:https://www.cnblogs.com/lytwajue/p/7053932.html
Copyright © 2020-2023  润新知