In: Computer Science
Note: I need a code and other requirement
Note: programming language is c++
If you need more information, please clarify what information you want.
consider solving the equation sin(x) - e^(-x) = 0 write a computer program to solve the given equation using:
1- bisection method
2- fixed-point method
3- newton's
intervals: {0,1},{1,2},{2,3},{3,4},{4,5},{5,6},{6,7},{7,8},{8,9},{9,10}
choose accuracy E = 10^(-5)
Make sure you document your program
Requirement :
1- Mathematical justification
2- algorithem description
3- code (program) with documentation
4-output: roots , number of iteration
Help:
for fixed-point: ln e^(-x) = ln( sin(x) )
-x = ln(sin(x))
sin(x) > 0
ln(a\b) = ln(a) - ln(b)
x = ln (1 \ sin θ )
f(0) = -ve
f(1) = sin(1) - e^(-1) = +ve
f(0).f(1) < 0
f(1) = +ve
f(2) = +ve
#include<iostream>
#include <iomanip>
#include<Math.h>
using namespace std;
double E = 0.00010; //accuracy E is set to
10^(-5)
double f(double x) //declear the function sin(x) - e^(-x) = 0
{
// take the value of x
return sin(x)-exp(-x);
// retruns the value of the function by calculating x
}
double df(double x)
//declear the differentiation function of sin(x)
- e^(-x) = 0
{
// take the value of x
return cos(x)+exp(-x);
// retruns the value of the function by calculating x
}
/* Implementing Bisection method */
void Bisection(double a, double b)
{
/*parameters
a = staring point of the interval
b = ending point of the interval
*/
if (f(a) * f(b) >= 0)
// checks weather the interval is right or
wrong
{
cout << "You have not assumed right a and b\n";
return;
}
double c = a;
while ((b-a) >= E)
{
// Find middle point
c = (a+b)/2;
// Check if middle point is root
if (f(c) == 0.0)
break;
// Decide the side to repeat the steps
else if (f(c)*f(a) < 0)
b = c;
else
a = c;
}
cout << "The value of root is : " << c;
}
/* Implementing Fixed Point Iteration */
void FixedPoint(double x0, int step)
{
/*parameters
x0 = staring point
step = maximum number of iterations
*/
double x1;
int i=1;
do
{
x1 = df(x0);
// print each iteration with
values
cout<<"Iteration
"<<i<<":\t x1 = "<< setw(10)<< x1<<"
and f(x1) = "<< setw(10)<< f(x1)<< endl;
i++;
if(i>step)
// checks the maximum
steps
{
cout<<"Not
Convergent.";
exit(0);
}
x0 = x1;
}while(fabs(f(x1)) < E);
cout<< endl<<"Root is "<<
x1;
}
void NewtonRaphson(double x)
{
/*parameters
x : starting point
*/
double h = f(x) / df(x);
while (abs(h) >= E)
{
h = f(x)/df(x);
x = x - h;
}
cout << "The value of the root is : " << x;
}
int main()
{
// you need to call each function with values
}
you find Mathematical justification here
Bisection : https://en.wikipedia.org/wiki/Bisection_method
Fixed point : https://en.wikipedia.org/wiki/Fixed-point_iteration
Newton : https://en.wikipedia.org/wiki/Newton%27s_method
algorithms
Bisection:
INPUT: Function f,
endpoint values a, b,
tolerance TOL,
maximum iterations NMAX
CONDITIONS: a < b,
either f(a) < 0 and f(b) > 0 or f(a) > 0 and f(b) < 0
OUTPUT: value which differs from a root of f(x) = 0 by less than TOL
N ? 1
while N = NMAX do // limit iterations to prevent infinite loop
c ? (a + b)/2 // new midpoint
if f(c) = 0 or (b – a)/2 < TOL then // solution found
Output(c)
Stop
end if
N ? N + 1 // increment step counter
if sign(f(c)) = sign(f(a)) then a ? c else b ? c // new interval
end while
Output("Method failed.") // max number of steps exceeded
Fixed point
Given an equation f(x) = 0
Convert f(x) = 0 into the form x = g(x)
Let the initial guess be x0
Do
xi+1=
g(xi)
while (none of the convergence criterion C1 or C2 is met
Newton: