Swift Algorithm notes-7

Source: Internet
Author: User

    @IBAction func Compute (sender:anyobject) {///19*x^7-31*x^5+16*x^2+7*x-90=0//Newton iterative method for Yi Yuanfang solution, maximum solution fan Wai [ -100000,100000] mytitle.stringvalue= "19*x^7-31*x^5+16*x^2+7*x-90=0" let Trycount = + var accu racy:double = 1e-15 var answer:double?=nil//Estimated solution range Var leftbound:double?=nil var ri Ghtbound:double?=nil for Var bound:double=1;bound<10000000;bound*=10{let Leftres=comresult (- bound) let Rightres=comresult (bound) if (Leftres*rightres) < 0 {Leftbound = (-bo und) Rightbound = bound break} else if leftres==0{ans                Wer=leftbound break} else if rightres==0{Answer=rightbound Break}} if (Leftbound==nil | | rightbound==nil) {return} var cen ter=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{//the solution of the computational equation var p0=leftbound!+ ( rightbound!-leftbound!) /2 var p:double for i in 1...trycount{p = Newtoncompresu                LT (p0) if ABS (P-P0) < accuracy {Answer=p0 break} P0=P}} If let ans=answer{//equation has solution result.stringvalue= "solution:" +s Tring (Stringinterpolationsegment:ans) + "Result.stringvalue + =" The value of the solution into the equation: "+string (stringinterpolationsegment : Comresult (ANS))}}


Solving nonlinear equations by Newton iterative method





This blog all content is original, if reprint please indicate source http://blog.csdn.net/myhaspl/




Swift Algorithm notes-7

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.