关于割线法的理论及相关公式如下https://baike.baidu.com/item/%E5%89%B2%E7%BA%BF%E6%B3%95/5806354
本文对理论方面不再进行讨论,仅涉及代码。代码中包含了部分解释。通过对参数(点的位置,初始值,容差)的更改可以满足大部分题目的要求。如果对您有帮助,麻烦点个赞,多谢。
%% We'll try solving 3x^3-5=0 using the secant method
tol = 10^(-4); %% set tolerance to 10^(-4).
epsilon = 1; %% An arbitrarily large value to begin with.
steps = 1; %% To keep track of the number of iterations; initially = 1.
%% If we get in an infinite loop we'll break out of it
%% break_flag = 0 indicates the default state
%% break_flag = 1 indicates a break
break_flag = 0;
%% set the seed points = starting x values.
%% The secant method requires two seed points.
%% Try x0 = -203 and x1 = -10.
x0 = -203;
x1 = -10;
fprintf("{0, %.8f},\n",x0);
fprintf("{1, %