viewcontroller.swift//learn5////Created by MYHASPL on 16/1/23.//Copyright (c) 2016 MYHASPL. All rights Reserved.//import Cocoaimport Foundationclass viewcontroller:nsviewcontroller {override func ViewDidLoad () {super.viewdidload ()//Do any additional setup after loading the view. } override Var Representedobject:anyobject? {didset {//Update the view, if already loaded. }}private func Comresult (inputnum:double)->double{//5*x^7-3*x^5+16*x^2+7*x+90=0 let MYRESULT:DOUBL E = 5 * POW (inputnum,7)-3 * POW (inputnum,5) + * POW (inputnum,2) + 7 * inputnum + + return Myresult} @IBOut Let weak var result:nstextfield! @IBAction func Compute (sender:anyobject) {//5*x^7-3*x^5+16*x^2+7*x+90=0//to find the solution of the Yi Yuanfang, the maximum solution range [ -100000,100000] let TR Ycount = accuracy:double var = 0.00000000000001 var answer:double?
=nil//Expected solution range Var leftbound:double?=nil var rightbound:double?=nil for var bound:double=1;bound<100000 00;bound*=10{let Leftres=comresult (-bound) let Rightres=comresult (bound) if (Leftres*rightres) < 0 {Leftboun D = (-bound) Rightbound = Bound Break}} if (Leftbound==nil | | rightbound==nil) {return}//the solution for calculating equations for I in 1. . trycount{result.stringvalue=string (i) let center=leftbound!+ (rightbound!-leftbound!) /2let Leftres:double=comresult (leftbound!) Let Rightres:double=comresult (rightbound!) Let Centres:double=comresult (center) if centres==0 {answer=centerbreak}else if ABS (rightbound!-leftbound!) < Accuracy {answer=leftbound!break}else if Leftres*centres<0{rightbound=center}else if rightres*centres<0{ Leftbound=center}} If let ans=answer{//equation has solution result.stringvalue= "solution:" +string (Stringinterpolationsegment:ans) + "" Result.stringvalue + = "value of the solution into the equation:" +string (Stringinterpolationsegment:comresult (ANS))} }}
the whole content of this blog is original. If reproduced please indicate source http://blog.csdn.net/myhaspl/
Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "/>
Swift Algorithm notes-5