Secant 方法介绍

Secant Method

函数 Secant_Methods 简介

1.函数定义

[c, errColumn] = Secant_Method(f, a, b, N, convergence_type, tolerance)

2.输入

%   f - function handle
% a - start position of interval bracket
% b - end position of interval bracket
% N [optional] - max iteration number
% convergence_type [optional] - [= 1] absolute error tolerances
% [= 0] relative error tolerances
% tolerance [optional] - [convergence_type = 1] absolute error tolerances
% [convergence_type = 0] relative error tolerances
%

3.输出

%   c - approximation of root r ( f(r) = 0 )
% errColumn - the absolute/relative errors during the progress
% convergence_step - the number of steps taken, if the method doesn't
% convergence, [convergence_step = inf]

注意,errColumn 长度为 N,若在第k步收敛解后,剩余元素都与收敛步误差相同

4.代码

function [c, errColumn, convergence_step] = Secant_Method(f, a, b, N, convergence_type, tolerance)
% Use Secant Method to find roots of equation [f(x) = 0]
%
% Input:
% f - function handle
% a - start position of interval bracket
% b - end position of interval bracket
% N [optional] - max iteration number (default value: 10)
% convergence_type [optional] - [= 1] absolute error tolerances (default)
% [= 0] relative error tolerances
% tolerance [optional] - [convergence_type = 1] absolute error tolerances
% [convergence_type = 0] relative error tolerances
%
% Output:
% c - approximation of root r ( f(r) = 0 )
% errColumn - the absolute/relative errors during the progress
% convergence_step - the number of steps taken, if the method doesn't
% convergence, [convergence_step = inf]
% Usages:
%
% 1. use default value
% f = @(x) x^2 - 1
% c = false_position(f, 0, 2)
%
% 2. user set value
% c = false_position(f, 0, 2, 20, 0, 1e-5)
%
% Warnning:
% 1. if f(a) and f(b) have the same sign, the function returns an Nan.
% 2. if the false position lines outside the bracket interval, the
% function will throws an error.
% 3. After N times iteration, if the method does not converge, an message
% will be printed on the command window and return the current vaule. %% check input parameters % check the interval is really a bracket
if (a > b)
error('please check the bracket!');
end % check that that neither end-point is a root
% if f(a) and f(b) have the same sign, throw an Nan.
if( f(a) == 0 )
c = a;
return;
elseif ( f(b) == 0 )
c = b;
return;
elseif ( f(a) * f(b) > 0 )
c = Nan;
return;
end % check max iteration number exits
% default value is 10
if ~exist('N', 'var')
N = 10;
end% if % check choice of error tolerances
% default value is 1
if ~exist('convergence_type', 'var')
convergence_type = 1;
tolerance = 1e-6;
end% if % check the tolerances is positive
if tolerance <= 0
error('the tolerances should be positive!');
end % relative error tolerances
errColumn = zeros(N, 1); %% iteration
% iterate at most N times c_old = a;
convergence_step = inf; for k = 1:N
%% find the false position
% c = (a*f(b) + b*f(a))/(f(b) - f(a));
c = ( b*f(a) - a*f(b) )/(f(a) - f(b)); % check c lies within the bracketing interval
if (c < a) || (c > b)
error('convergence problem occurs! please reset bracket interval.')
end %% reset bracketing interval
% Check if c is a root
if ( f(c) == 0 )
% return c
return;
elseif ( f(c)*f(a) < 0 )
% if f(a) and f(c) have opposite signs
% set [a, c] as the new bracketing interval
b = c;
else
% if f(b) and f(c) have opposite signs
% set [c, b] as the new bracketing interval
a = c;
end %% cal the absolute/relative errors
switch convergence_type
case 0 % relative error
errColumn(k) = abs( (c - c_old)/c_old );
if errColumn(k) < tolerance
convergence_step = k;
errColumn(k:end) = errColumn(k);
% set convergence step
% set the remaining step errors
return;
end
case 1 % absolute error
errColumn(k) = abs( f(c) );
if errColumn(k) < tolerance
% set convergence step
% set the remaining step errors
convergence_step = k;
errColumn(k:end) = errColumn(k);
return;
end
end% switch c_old = c;
end fprintf( 'the method did not converge\n\n' );
end

算例 Q1.m

find all roots of \(1000000x^3 − 111000x^2 + 1110x = 1\)

  • 绘制函数函数图像,寻找方程根所在区间

  • 选取区间

    选取3个区间分别为 \([-0.01, 0.005], [0.005, 0.06], [0.06, 0.11]\)。

  • 计算

    第一个区间 \([-0.01, 0.005]\) 为例,选择绝对误差为 \(10^{-6}\),迭代40次。得到误差随迭代次数变化关系为



    其中红色点代表第31步方法收敛位置。

  • 结果

    最终得到方程三个根为

r =

    0.0010
0.0100
0.1000
  • 脚本
%% Q1
f = @(x) 1000000*x.^3 - 111000*x.^2 + 1110*x - 1; % plot function
x = linspace(-0.01, 0.11, 50);
y = f(x);
figure; plot(x,y); grid on; % set interval bracket
a(1) = -0.01; a(2) = 0.005; a(3) = 0.06; a(4) = 0.11; % cal roots in a loop
r = zeros(3,1); for ib = 1:3
[r(ib), errColumn, con_step] = Secant_Method(f, a(ib), a(ib+1), 40); if ib == 1
figure; plot(errColumn); hold on;
plot(con_step, errColumn(con_step), 'ro');
xlabel('Method Steps'); ylabel('Absolute Error')
end% if
end% for

Secant 方法求方程多个根的更多相关文章

  1. OpenJudge计算概论-求一元二次方程的根【含复数根的计算、浮点数与0的大小比较】

    /*====================================================================== 求一元二次方程的根 总时间限制: 1000ms 内存限 ...

  2. 计算概论(A)/基础编程练习1(8题)/4:求一元二次方程的根

    #include<stdio.h> #include<math.h> int main() { // 待解方程数目 int n; scanf("%d", & ...

  3. 求方程x1+x2+x3=15的整数解的数目

    求方程x1+x2+x3=15的整数解的数目要求0≤x1≤5,0≤x2≤6,0≤x3≤7.解:令N为全体非负整数解(x1,x2,x3),A1为其中x1≥6的解:y1=x1-6≥0的解:A2为其中x2≥7 ...

  4. 【编程题目】题目:定义 Fibonacci 数列 输入 n,用最快的方法求该数列的第 n 项。

    第 19 题(数组.递归):题目:定义 Fibonacci 数列如下:/ 0 n=0f(n)= 1 n=1/ f(n-1)+f(n-2) n=2输入 n,用最快的方法求该数列的第 n 项. 思路:递归 ...

  5. poj3660 Cow Contest(Floyd-Warshall方法求有向图的传递闭包)

    poj3660 题意: 有n头牛, 给你m对关系(a, b)表示牛a能打败牛b, 求在给出的这些关系下, 能确定多少牛的排名. 分析: 在这呢先说一下关系闭包: 关系闭包有三种: 自反闭包(r), 对 ...

  6. 给定一个正整数,实现一个方法求出离该整数最近的大于自身的 换位数 <把一个整数各个数位进行全排列>

    """给定一个正整数,实现一个方法求出离该整数最近的大于自身的 换位数 -> 把一个整数各个数位进行全排列""" # 使用 permu ...

  7. hdu 2545 求当前结点到根节点的距离

    求当前结点到根节点的距离 Sample Input 2 1 //n m 1 2 1 2 //询问 5 2 1 2 1 3 3 4 3 5 4 2 //询问 4 5 0 0 Sample Output ...

  8. 用递归的方法求一个数组的前n项和

    用递归的方法求一个数组的前n项和 public class Demo1 { /* * 用递归的方法求一个数组的前n项和 */ public static void main(String[] args ...

  9. FFT模板 生成函数 原根 多项式求逆 多项式开根

    FFT #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> ...

随机推荐

  1. Microsoft Porject Online 学习随手记一:环境创建和数据导入

    没有想像的简单,也没那么复杂 Project OL之前是Dynamics 365 Enterprise P1中的一个模块,目前最新版本只能简单创建并且已经没有Enterprise P1选项. 主要流程 ...

  2. 吴恩达深度学习课后习题第5课第1周第3小节: Jazz Improvisation with LSTM

    目录 Improvise a Jazz Solo with an LSTM Network Packages 1 - Problem Statement 1.1 - Dataset What are ...

  3. [软工顶级理解组] Alpha阶段事后分析

    目录 设想和目标 计划 资源 变更管理 设计/实现 测试/发布 团队的角色,管理,合作 总结 质量提高 会议截图 设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰 ...

  4. 嵌入式STM32的GPIO口工作模式的介绍

    一.输入模式 1. 浮空输入 浮空输入模式下,上拉和下拉两个开关断开,高或低电平通过施密特触发器到达输入数据寄存器,CPU可以通过读取输入数据寄存器从而读取到外部输入的高低电平值. 2. 输入上拉模式 ...

  5. 计算机网络之网络层IP组播(IGMP、组播路由选择协议、组播地址)

    文章转自:https://blog.csdn.net/weixin_43914604/article/details/105318560 学习课程:<2019王道考研计算机网络> 学习目的 ...

  6. C++链表常见面试考点

    链表常见问题: 单链表找到倒数第n个节点 用两个指针指向链表头,第一个指针先向前走n步,然后两个指针同步往前走,当第一个指针指向最后一个节点时,第二个指针就指向了倒数第n个节点. 判断链表有没有环 快 ...

  7. Luogu P2081 [NOI2012]迷失游乐园 | 期望 DP 基环树

    题目链接 基环树套路题.(然而各种错误调了好久233) 当$m=n-1$时,原图是一棵树. 先以任意点为根做$dp$,求出从每一个点出发,然后只往自己子树里走时路径的期望长度. 接着再把整棵树再扫一遍 ...

  8. SpringBoot整合reids之JSON序列化文件夹操作

    前言 最近在开发项目,用到了redis作为缓存,来提高系统访问速度和缓解系统压力,提高用户响应和访问速度,这里遇到几个问题做一下总结和整理 快速配置 SpringBoot整合redis有专门的场景启动 ...

  9. Linux&C 线程控制 课后习题

    Q1:多线程与多进程相比有什么优势? 多进程程序耗费的资源大,因为fork()的时候子进程需要继承父进程的几乎所有东西,但是多线程程序线程只继承一部分,即自己的私有数据,例如自己的线程ID,一组寄存器 ...

  10. uni-城市列表滑动组件,点击字母跳转到指定位置

    本插件由博主自主开发,比uni插件市场的城市列表滑动组件性能好,且不会出现闪屏的情况. 通过计算城市列表的高度实现滚动到指定位置,使用了uni滚动到指定位置的api city-chooce为页面入口页 ...