弦截法求方程根

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 approximation of Newton‘s method. However, the method was developed independently of Newton‘s method, and predated the latter by over 3,000 years.

 1 /*
 2  * =====================================================================================
 3  *
 4  *       Filename:  secant_method.cc
 5  *
 6  *    Description:  secant method
 7  *
 8  *        Version:  1.0
 9  *        Created:  2015年07月16日 13时53分26秒
10  *       Revision:  none
11  *       Compiler:  g++
12  *
13  *         Author:  YOUR NAME (),
14  *   Organization:
15  *
16  * =====================================================================================
17  */
18 #include <iostream>
19 #include <cmath>
20 using namespace std;
21
22 double f(double x)                              //所要求解的函数公式
23 {
24     return x*x*x - 3*x -1;
25 }
26
27 double point(double a, double b)                //求解弦与x轴的交点
28 {
29     return (a*f(b) - b*f(a))/(f(b) - f(a));
30 }
31
32 double root(double a, double b)                 //用弦截法求方程在[a, b]区间的根
33 {
34     double x, y, y1;
35     y1 = f(a);
36     do {
37         x = point(a, b);                        //求交点x坐标
38         y = f(x);                               //求y
39         if (y*y1 > 0)
40             y1 = y, a = x;
41         else
42             b = x;
43     }while (fabs(y) >= 0.000001);               //计算精密度
44     return x;
45 }
46
47 int main()
48 {
49     double a, b;
50     cin>>a>>b;
51     cout<<"root = "<<root(a, b)<<endl;
52     return 0;
53 }
时间: 2024-08-27 13:21:34

弦截法求方程根的相关文章

用弦截法求一元三次方程的根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

用弦截法求解方程的根

//弦截法求解方程的根 //要求:输入左右两个端点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

牛顿迭代法 求方程根

牛顿迭代法 牛顿迭代法(Newton's method)又称为牛顿-拉夫逊方法(Newton-Raphson method),它是牛顿在17世纪提出的一种在实数域和复数域上近似求解方程的方法.多数方程不存在求根公式,因此求精确根非常困难,甚至不可能,从而寻找方程的近似根就显得特别重要. 方法使用函数f(x)的泰勒级数的前面几项来寻找方程f(x) = 0的根.牛顿迭代法是求方程根的重要方法之一,其最大优点是在方程f(x) = 0的单根附近具有平方收敛,而且该法还可以用来求方程的重根.复根,此时线性

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

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

C语言之基本算法24—黄金切割法求方程近似根

//黄金切割法! /* ================================================================ 题目:用黄金切割法求解3*x*x*x-2*x*x-16=0的根. ================================================================ */ #include<stdio.h> #include <math.h> #define E 1e-8 double hs(doub

MATLAB用二分法、不动点迭代法及Newton迭代(切线)法求非线性方程的根

一.实验原理 二.实验步骤 三.实验过程 1.(程序) (1)二分法:求   在区间(1,2)之间的根,取 (a)bipart.m: function [x,m]=bipart(fun,a0,b0,tol) a=a0;b=b0; m=1+round(round(log((b-a)/tol))/log(2)); for k=1:m p=(a+b)/2; if fun(p)*fun(b)<0 a=p; else b=p; end x=p; end end (b)fun1.m: function f=

c语言:用牛顿迭代法求方程在1.5附近的根:2x^3-4x^2+3x-6=0.

用牛顿迭代法求方程在1.5附近的根:2x^3-4x^2+3x-6=0. 解:牛顿迭代法又叫牛顿切线法.设f =2x^3-4x^2+3x-6,f1为方程的导数,则f1 = 6x^2 - 8x+3,且f1=(f(x0)-0)/(x0-x1),推导得:x1 = x0 - f / f1 程序: #include<stdio.h> #include<math.h> int main() { double x0,x1,f,f1; x1 = 1.5; do { x0 = x1; f = 2*x0

特征根法求通项+广义Fibonacci数列找循环节 - HDU 5451 Best Solver

Best Solver Problem's Link Mean: 给出x和M,求:(5+2√6)^(1+2x)的值.x<2^32,M<=46337. analyse: 这题需要用到高中的数学知识点:特征根法求递推数列通项公式. 方法是这样的: 对于这题的解法: 记λ1=5+2√6,λ2=5-2√6,则λ1λ2=1,λ1+λ2=10 根据韦达定理可以推导出:λ1,λ2的特征方程为 x^2-10x+1=0 再使用该特征方程反向推导出递推公式为:a[n]=10*a[n-1]-a[n-2] 再由特征根