(四) 二分法求根

 1 # -*- coding: utf-8 -*-
 2 #coding=utf-8
 3 import numpy as np
 4 import matplotlib.pyplot as plt
 5
 6
 7
 8 plt.close()
 9 fig = plt.figure()
10 plt.grid(True)
11 plt.axis([0,2, -2, 5])
12 plt.ion()
13 plt.xlabel("X")
14 plt.ylabel("Y")
15 x = np.linspace(0, 10, 100)
16 y = x**3-3*x+1
17 def f(x):
18     return y
19
20 plt.plot(x, y, label="$y$", color="red", linewidth=1)
21 plt.show()
22 a = input("请输入左边的值:a = ")
23 b = input("请输入右边的值:b = ")
24 c = input("请输入精度值:c = ")
25
26 #在一定精度之外循环
27 while(abs(a-b)>c):
28     # plt.cla()
29     #显示两边值分别为a,b
30     print a,b
31     x = (a+b)/2.0
32     f = a**3 - 3*a + 1
33     f1 = x**3-3*x+1
34
35     #如果x值直接得到结果跳出循环
36     if f1==0:
37         print x
38         break
39
40     #判断中间值在哪边
41     elif f * f1<0:
42         b = x
43     else:
44         a = x
45     plt.plot([a, b], [a ** 3 - 3 * a + 1, b ** 3 - 3 * b + 1])
46     plt.pause(0.05)
47 show_res = ‘[x=‘ + str(x) +  ‘]‘
48 # plt.annotate(show_res,xytext=0.0,xy=x)
49 plt.text(0,0,show_res)
50 print "最终估计解为:x =",x
51
52 while True:
53     plt.pause(0.05)
时间: 2024-08-30 12:00:22

(四) 二分法求根的相关文章

链表实现二分法求根

#include<iostream>#include<iomanip>#include<cmath>using namespace std;class poly{public: double c; int e; poly*next;};poly*input();double f(poly*head,double x);double root(poly*head,double a, double b);int main(){ poly*head; double a, b;

计算方法-C语言二分法求根

问题: 给出方程f(x) = x^3+10x-20,求该方程在(1,2)上的根,其精度不小于10^-4 看似很简单的一个小问题,其实有很有细节值得注意,先给出代码 方法一: #include <stdio.h> #include <math.h> double f(double x) { return x*x*x+10*x-20; } int main() { double l,r,mid,ans; l = 1; r = 2; while(1) { mid = (l+r)/2; d

二分法求根

1 #include<stdio.h> 2 #include<math.h> 3 float f(float x){ 4 return (x*x*x-x-1); 5 } 6 int main(){ 7 float x1, x2; 8 scanf("%f,%f", &x1, &x2); 9 10 float cen=(x1+x2)/2.0; 11 float vc=f(cen); 12 while(fabs(vc) > 1e-6){ 13 1

二分法求三次方程的根

二分法求根 #include "stdio.h" #define f(x) a*x*x*x+b*x*x+c*x+d int main() { freopen("in.txt", "r", stdin); int a, b, c, d; double x1, x2, x, y1, y2, y; while (scanf("%d%d%d%d", &a, &b, &c, &d) != EOF) { x

方程求根——二分法

二分法求根主要应用了区间套定理,这一算法实现简单且结果也迭代的较好,但对于复杂函数其结果不理想 1.代码 %%二分法求根 %%f为函数表达式,interval0为初始区间,epsilon为控制精度 function RD = Roots_dichotomy(f,interval0,epsilon) x_low = interval0(1);x_up = interval0(2);x_ave = (x_low+x_up)/2; %%作图 t = x_low:(x_up-x_low)/1000:x_

用二分法求下面方程在(-10,10)之间的根。【谭浩强第四版课后习题】

用二分法求下面方程在(-10,10)之间的根:2x3-4x2+3x-6=0 编程思路:仿照二分查找,将区间划分为两部分,记录区间左右端点,得到中点.每次运算将中点带入方程: 结果>0:根应该向小一点的值拟合,high=mid; 结果<0:根应该向大一点的值拟合,low=mid; 对于跳出循环的条件,我首先是认为low和high应该无限逼近:while(fabs(low-high)>1e-5),当循环体内结果=0后,便使用break跳出循环:if(temp==0) break;但是没有得到

Openjudge ch0111/t6253 用二分法求方程的根

这题只是考你最后有没有(r-l)/2而已…… 总时间限制: 1000ms 内存限制: 65536kB 描述 用二分法求下面方程在(-10, 10)之间的一个根. 2x3- 4x2+ 3x- 6 = 0 输入 一个小于1的非负实数e,它的值表示所能允许的误差 输出 一个实数,其值为求得的一个根,要求精确到小数点后8位.若该区间上没有根,则输出“No Solution” 样例输入 0 样例输出 2.00000000 提示 对于一个连续函数f(x),若f(a)*f(b) <= 0,则f(x)在区间[a

03-1. 二分法求多项式单根

二分法求函数根的原理为:如果连续函数f(x)在区间[a, b]的两个端点取值异号,即f(a)f(b)<0,则它在这个区间内至少存在1个根r,即f(r)=0. 二分法的步骤为: 检查区间长度,如果小于给定阈值,则停止,输出区间中点(a+b)/2:否则 如果f(a)f(b)<0,则计算中点的值f((a+b)/2): 如果f((a+b)/2)正好为0,则(a+b)/2就是要求的根:否则 如果f((a+b)/2)与f(a)同号,则说明根在区间[(a+b)/2, b],令a=(a+b)/2,重复循环:

UVa 10341 (二分求根) Solve It

很水的一道题,因为你发现这个函数是单调递减的,所以二分法求出函数的根即可. 1 #include <cstdio> 2 #include <cmath> 3 //using namespace std; 4 5 const double e = 1e-14; 6 double p, q, r, s, t, u; 7 8 inline double f(double x) 9 { return p*exp(-x) + q*sin(x) + r*cos(x) + s*tan(x) +