久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

Python用二分法求平方根的案例

瀏覽:3日期:2022-06-25 15:40:25

我就廢話不多說了,大家還是直接看代碼吧~

def sq2(x,e): e = e #誤差范圍 low= 0 high = max(x,1.0) #處理大于0小于1的數(shù) guess = (low + high) / 2.0 ctr = 1 while abs(guess**2 - x) > e and ctr<= 1000: if guess**2 < x: low = guess else: high = guess guess = (low + high) / 2.0 ctr += 1 print(guess)

補充:數(shù)值計算方法:二分法求解方程的根(偽代碼 python c/c++)

數(shù)值計算方法:

二分法求解方程的根

偽代碼

fun (input x) return x^2+x-6newton (input a, input b, input e)//a是區(qū)間下界,b是區(qū)間上界,e是精確度 x <- (a + b) / 2 if abs(b - 1) < e: return x else: if fun(a) * fun(b) < 0: return newton(a, x, e) else: return newton(x, b, e)c/c++:

#include <iostream>#include <cmath>using namespace std; double fun (double x);double newton (double a, double b,double e); int main(){ cout << newton(-5,0,0.5e-5); return 0;} double fun(double x){ return pow(x,2)+x-6;} double newton (double a, double b, double e){ double x; x = (a + b)/2; cout << x << endl; if ( abs(b-a) < e) return x; else if (fun(a)*fun(x) < 0) return newton(a,x,e); else return newton(x,b,e);}python:

def fun(x): return x ** 2 + x - 6def newton(a,b,e): x = (a + b)/2.0 if abs(b-a) < e: return x else: if fun(a) * fun(x) < 0: return newton(a, x, e) else: return newton(x, b, e)print newton(-5, 0, 5e-5)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。如有錯誤或未考慮完全的地方,望不吝賜教。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 甘谷县| 黄龙县| 平安县| 额敏县| 宁国市| 梁山县| 榆树市| 三亚市| 娄烦县| 宜州市| 高陵县| 尉氏县| 正蓝旗| 松潘县| 中方县| 米泉市| 腾冲县| 泉州市| 宝坻区| 闽清县| 土默特左旗| 南召县| 芮城县| 油尖旺区| 嵩明县| 山西省| 尖扎县| 彭州市| 新野县| 湟源县| 留坝县| 颍上县| 自贡市| 新田县| 宝坻区| 凌源市| 曲阳县| 龙海市| 通海县| 西藏| 莱芜市|