@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