(三) 弦截法(试位法)求根

 1 # -*- coding: utf-8 -*-
 2 #coding=utf-8
 3 import numpy as np
 4 from sympy import *
 5 import math
 6 import matplotlib.pyplot as plt
 7
 8 plt.close()
 9 fig = plt.figure()
10 #网格可见
11 plt.grid(True)
12 plt.axis([0, 2, -10, 50])
13 #开启交互
14 plt.ion()
15 #设置xy轴的名称
16 plt.xlabel("X")
17 plt.ylabel("Y")
18 x = np.linspace(0, 10, 100)
19 y = x**3+4*x**2-10
20 plt.plot(x, y, label="$x**3+4*x**2-10$", color="red", linewidth=1)
21 plt.show()
22 #获取零点俩边的x值
23 x1 = float(input("请输入零点左边x1的值:x1 = "))
24 x2 = float(input("请输入另一边x2的值:x2 = "))
25
26 while(1):
27    #每运行一次输出俩边的值
28     print x1,x2
29
30    # 函数式
31     x=Symbol("x")
32     f = x**3+4*x**2-10
33
34     #两边的y值为
35     y1 = x1**3 + 4*x1**2 - 10
36     y2 = x2**3 + 4*x2**2 - 10
37     print y1,y2
38
39    #两点连线与x轴交点的x值
40     x = x.subs(x,x2-(y2*(x1-x2)/(y1-y2)))
41     #print x
42     #刚好是函数零点时
43     if f==0:
44         break
45
46     #交于y=0时
47     if x==0.0:
48          if x2>x:
49             x1 = x
50             y1 = x1 ** 3 + 4 * x1 ** 2 - 10
51          elif x2<x:
52             x2 = x
53             y2 = x2 ** 3 + 4 * x2 ** 2 - 10
54
55      #纵坐标乘积为负且不满足终止条件时继续循环
56     elif y1*y2<0:
57         x1=x
58         y1 = x1 ** 3 + 4 * x1 ** 2 - 10
59     #取终止值为 0.01  满足条件
60     if  (x**3+4*x**2-10)>-0.01 and  (x**3+4*x**2-10)<0.01 :
61         break
62
63     plt.plot([x1, x2], [x1**3+4*x1**2-10, x2**3+4*x2**2-10])
64     plt.pause(0.05)
65 show_res = ‘[x=‘ + str(x) +  ‘]‘
66 plt.text(1, 5, show_res)
67 print "估计结果为:x = "
68 print x
69 while True:
70     plt.pause(0.05)
时间: 2024-08-30 06:53:58

(三) 弦截法(试位法)求根的相关文章

用弦截法求一元三次方程的根x^3-5x^2+16x-80=0 ;带注释!

//用弦截法求一元三次方程的根x^3-5x^2+16x-80=0 #include<stdio.h>#include<math.h> float f(float x) //定义子函数f(x) = x^3-5x^2+16x-80,当f(x) →0时,则x即为所求的实数根:  {     float y;     y=((x-5.0)*x+16.0)*x-80.0;     return(y);          //返回f(x)的值  }    float xpoint( float

弦截法求方程根

THE SECANT METHOD In numerical analysis, the secant method is a root-finding algorithm that uses a succession of roots of secant lines to better approximate a root of a function f. The secant method can be thought of as a finite difference approximat

用弦截法求解方程的根

//弦截法求解方程的根 //要求:输入左右两个端点x值 //结果:在一定精度范围内求解出方程的根 //难点:1)推导出x处的横坐标的求解公式 2)迭代掉原来的左端点或者右端点 #include "pch.h" #include <iostream> #include <cmath> #include <iomanip> using namespace std; double f(double x); double xpoint(double x1,

弦截法求一元三次方程的近似解

1 #include<stdio.h> 2 #include<math.h> 3 4 //计算一元三次方程的根大致分布位置 5 6 //计算的方程为 x*x*x-8*x*x+12*x-30=0 7 8 //计算函数值 9 float f(float x) 10 { 11 return ((x-8.0)*x+12.0)*x-30.0; 12 } 13 14 //计算弦与坐标x轴的交点 15 16 float xpoint(float x1,float x2) 17 { 18 retu

触屏三点校准法

1.  两点校准法: 关系: X = k1* x + datx; Y = k2* y + daty; 其中X,Y是屏的物理坐标(液晶屏的坐标),x,y是屏逻辑坐标(触屏的坐标)k1,k2为x,y方向的比例因子,datx,daty为x,y方向的迁移量.四个未知数我们就需要四个方程(其实是两组独立的方程),所以我们就在液晶屏指定的物理坐标(X,Y)位置显示我们校准符号然后通过点击触屏读出x,y带入上面的式子求出k1,k2,datx,daty,以后就通过上面的关系将物理坐标和逻辑坐标联系起来了,但是我

用三个函数分别实现求三角形,正方形,圆形面积(所有底高半径都由用户 输入);在主函数中,通过用户不同的选择分别进行调用;

/*2.用三个函数分别实现求三角形,正方形,圆形面积(所有底高半径都由用户输入):在主函数中,通过用户不同的选择分别进行调用:*/ #include <stdio.h>#define P 3.14double sanjiao(double di,double gao){ double mianji = (di * gao)/2 ; return mianji;} double zhengfangxing(double bian){ double mianji2 = bian*bian; ret

[LeetCode] Sum Root to Leaf Numbers 求根到叶节点数字之和

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

OJ刷题之《牛顿迭代法求根》

题目描述 用牛顿迭代法求根.方程为ax3+bx2+cx+d=0.系数a,b,c,d的值一次为1,2,3,4,由主函数输入.求x在1附近的一个实根.求出根后由主函数输出.结果保留两位小数. 输入 系数a,b,c,d的值 输出 x在1附近的一个实根 样例输入 1 2 3 4 样例输出 -1.65 提示 主函数已给定如下,提交时不需要包含下述主函数 /* C代码 */ int main() { double solut(double ,double ,double ,double ); double

链表实现二分法求根

#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;