The Secant Method is a root-finding algorithm that uses two initial approximations to start the iteration process. This root-finding algorithm uses a succession of roots of secant lines to better approximate a root of a function f(x) from to intial approximation x0 and x1. |
---|
Setting up a New Project in Code::Blocks |
---|
Use the console application template in Code::Blocks to start a new project. Set in C++ as the programming language to be used. For the detailed instruction, feel free to read the the first part of Numerical Method using C++ Programming #1 For this tutorial, save the project as Secant Method. Then, go to Secant Method> Sources > main.cpp |
<cmath>
, and <iomanip>
for number precision. Write the proper syntax as follows:#include <iostream> #include <cmath> #include <iosmanip> using namespace std; |
---|
data_type function( data_type identifier);
. Use a double data type for its flexibility to store characters, integers, and floating point as compared with integer and float data types. So, write your code according to the syntax structure. We will use three identifiers x, x0, and x1 for this tutorial. This identifiers will stroe the values of the root, and two values for our initial approximation.double Secant(double x, double x0, double x1) |
---|
double Secant(double x, double x0, double x1){ 'Secant_Method_Algorithm' }
.Scenario | Syntax used |
---|---|
1. Absolute vale of f(x1) should begreater than as compared to the error | fabs(data_type, indentifier)> e |
2. n approximations should be less than or equal to maximum iteration (Max_Iter) | n > = Max_Iter |
double Secant(double x, double x0, double x1){ int n =2; const int Max_Iter=100; const double e=0.0001; |
---|
while( (fabs(double x1) > e) && ( n <= Max_Iter) |
---|
pow(identifier, exponent) to get the result of raising the number to a certain exponent. This is the syntax
double fx0 = pow(x0,4) + pow(x,2);and
double fx0 = pow(x0,4) + pow(x,2);```, after identifier is being replaced.x=x1 - (fx1 * (x1 - x0)) / (fx1 - fx0);
.double Secant(double x, double x0, double x1){ int n =2; const int Max_Iter=100; const double e=0.001; while( (fabs(double x1) > e) && ( n <= Max_Iter)){ double fx0 = pow(x0,4) + pow(x,2); double fx0 = pow(x0,4) + pow(x,2); x=x1 - (fx1 * (x1 - x0)) / (fx1 - fx0); x0=x1; x1=x; n++; } return n; } |
---|
cout<<'Text to be print ' <<endl;
and ```cin>>'identifier/placeholder' for output and input syntax respectively.int main(double x, double x0, double x1){ cout << 'Secant Method' << endl; cout <<'Enter first initial approximation: '; cin >>x0; cout <<'Enter second initial approximation: ' cin >>x1; cout <<'n The root of the equation is '<<x <<endl; return 0; } |
---|
#include <iostream> #include <cmath> #include <iosmanip> using namespace std; double Secant(double x, double x0, double x1){ int n =2; const int Max_Iter=100; const double e=0.001; while( (fabs(double x1) > e) && ( n <= Max_Iter)){ double fx0 = pow(x0,4) + pow(x,2); double fx0 = pow(x0,4) + pow(x,2); x=x1 - (fx1 * (x1 - x0)) / (fx1 - fx0); x0=x1; x1=x; n++; } return n; } int main(double x, double x0, double x1){ cout << 'Secant Method' << endl; cout <<'Enter first initial approximation: '; cin >>x0; cout <<'Enter second initial approximation: ' cin >>x1; cout <<'n The root of the equation is '<<x <<endl; return 0; } |
---|