Question

In: Computer Science

Note: I need a code and other requirement Note: programming language is c++ If you need...

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

Solutions

Expert Solution

#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:

  1. Start
  2. Read x, e, n, d
    *x is the initial guess
    e is the absolute error i.e the desired degree of accuracy
    n is for operating loop
    d is for checking slope*
  3. Do for i =1 to n in step of 2
  4. f = f(x)
  5. f1 = f'(x)
  6. If ( [f1] < d), then display too small slope and goto 11.
    *[ ] is used as modulus sign*
  7. x1 = x – f/f1
  8. If ( [(x1 – x)/x1] < e ), the display the root as x1 and goto 11.
    *[ ] is used as modulus sign*
  9. x = x1 and end loop
  10. Display method does not converge due to oscillation.
  11. Sto

Related Solutions

I need C++ programming with output. I have tried other programming and it does not work....
I need C++ programming with output. I have tried other programming and it does not work. So please give me the one that actually works. Assignment 1 Design your own linked list class that works as a template class. It should provide member functions for appending, inserting and deleting nodes. The destructor should destroy the list. The class should also provide a member function that will display the contents of the list to the screen. The class should also provide...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing chess himself to practice his abilities. The chess that Jojo played was N × N. When Jojo was practicing, Jojo suddenly saw a position on his chessboard that was so interesting that Jojo tried to put the pieces of Rook, Bishop and Knight in that position. Every time he put a piece, Jojo counts how many other pieces on the chessboard can be captured...
I am building a game in C programming language where I need to add objects of...
I am building a game in C programming language where I need to add objects of various length into a game board. The game board is 8X8 and we must account for the boundaries for the board and not go over them with our objects. The boards upper left corner is at 0x0 and we must return 1 if it fits and -1 if it does not fit. I have the following 2 functions to start with: ```int add_object_vert(int r,...
I need a program(code) in any programming language that performs the following operations: union, concatenation and...
I need a program(code) in any programming language that performs the following operations: union, concatenation and conversion DFA-NDFA.
NEED IN C++ For the following programming problems, you need to time a section of code...
NEED IN C++ For the following programming problems, you need to time a section of code in C++. For example, the following statements time the execution of the function doSomething: #include clock_t start = clock(); doSomething(); clock_t finish = clock(); double overallTime = static_cast(finish - start)/ CLOCKS_PER_SEC; Consider the following two loops: //Loop A for(i = 1; i <= n; i++)    for(j = 1; j <= 10000; j++)     sum = sum + j; //Loop B for(i = 1;...
How would I code the following in assembly language? Use the Keil programming environment to code...
How would I code the following in assembly language? Use the Keil programming environment to code the C8051F330 SiLabs 8051 micro controller. All the documentation is available on efundi. The program shall be done in assembler and you shall use the DJNZ instruction to generate a delay time to flash the LED. The LED shall flash in the following sequence: 1. On for 50mS, 2. Off for 400mS, 3. On for 50mS, 4. Off for 1.5S, 5. Repeat the sequence...
C Programming language problem I need to write a program which uses several threads (like 8...
C Programming language problem I need to write a program which uses several threads (like 8 threads for example) to sum up a number. The following program is using 1 thread but I need several threads to compile it. There was a hint saying that using multiple separate for loop and combining them will make a multi-threaded program. #include <stdio.h> #include <stdlib.h> #include <pthread.h> int sum; // this data is shared by the threads void *runner(void *param); // threads call...
Rewrite the C PROGRAMMING LANGUAGE CODE in terms of only dereferencing (*) and pointer addition (+)...
Rewrite the C PROGRAMMING LANGUAGE CODE in terms of only dereferencing (*) and pointer addition (+) AND extend the code so that allocated memory is freed properly. Thank you struct foo { int a; char b; }; int main(void) { struct foo* arr[5]; int x; for(x = 0; x < 5; x++) { arr[x] = malloc(sizeof(struct foo)); arr[x]->a = 0; arr[x]->b = 'b'; } }
Only Program in C for this. No other programming language is allowed. Using a function, we...
Only Program in C for this. No other programming language is allowed. Using a function, we will add a range of values of an array. The range is going to be determined by the user. In this example, if you put the following array down as: 1.5 -5.6 8.9 4.6 7.8 995.1 45.1 -5964.2 … and the user tells you to add from the 3rd element to the 6th element, your program is going to need to add the values:...
The following is for C programming language: I want to scan for initials in a line...
The following is for C programming language: I want to scan for initials in a line of text. my line of text is as follows: 12345 3.5000 a j 12346 4.1000 s p The first number represents the student ID, the second number represents the gpa, the third character represents the first initial and the fourth character represents the last initial of the student. My text file contains these values. The following is my code: fscanf(fp, "%d %c %c", &studentID,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT