Question

In: Computer Science

HI, ANY PROGRAMMING LANGUAGE WOULD WORK, FROM PYTHON TO C++, MOST PREFERABLY OR ELSE ANY FOR...

HI, ANY PROGRAMMING LANGUAGE WOULD WORK, FROM PYTHON TO C++, MOST PREFERABLY OR ELSE ANY FOR YOUR CONVENIENCE.

Let f(x) = x6 + 7x5− 15x4− 70x3 + 75x2 + 175x − 125.

a.) Write a program that carries out the Secant Method on f(x). You do not need to make your program take arbitrary

input (i.e. you can tailor it to this specific f(x)). Your program should take as input the appropriate number of initial

guesses, an interval [a, b] and a desired error tolerance TOL. It should output a root r that is within the desired

tolerance and it should output N, the number of iterations it took to get to within the error tolerance. Make it clear

what procedure you are using to compute the error of your approximations.

b.)  Write a program that carries out Steffensen′s Method on f(x). You do not need to make your program take arbitrary

input (i.e. you can tailor it to this specific f(x)). Your program should take as input the appropriate number of

initial guesses, an interval [a, b] and a desired error tolerance TOL. It should output a root r that is within the

desired tolerance and it should output N, the number of iterations it took to get to within the error tolerance. Make

it clear what procedure you are using as your underlying recursion, and how you are computing the error of your

approximations.

c.) Write a program that carries out Aitken′s Method on f(x). You do not need to make your program take arbitrary input

(i.e. you can tailor it to this specific f(x)). Your program should take as input the appropriate number of initial guesses,

an interval [a, b] and a desired error tolerance TOL. It should output a root r that is within the desired tolerance and

it should output N, the number of iterations it took to get to within the error tolerance. Make it clear what procedure

you are using as your underlying recursion, and how you are computing the error of your approximations.

d.) Write a program that carries out Newton′s Method on f(x). You do not need to make your program take arbitrary

input (i.e. you can tailor it to this specific f(x)). Your program should take as input the appropriate number of initial

guesses, an interval [a, b], and a desired error tolerance TOL. It should output a root r that is within the desired

tolerance and it should output N, the number of iterations it took to get to within the error tolerance. Make it clear

what procedure you are using to compute the error of your approximations

Solutions

Expert Solution

a)Program to find root of an equations using secant method

Last Updated: 04-01-2019

The secant method is used to find the root of an equation f(x) = 0. It is started from two distinct estimates x1 and x2 for the root. It is an iterative procedure involving linear interpolation to a root. The iteration stops if the difference between two intermediate values is less than convergence factor.

Examples :

Input : equation = x3 + x - 1

        x1 = 0, x2 = 1, E = 0.0001

Output : Root of the given equation = 0.682326

         No. of iteration=5

Algorithm

Initialize: x1, x2, E, n         // E = convergence indicator

calculate f(x1),f(x2)

if(f(x1) * f(x2) = E); //repeat the loop until the convergence

    print 'x0' //value of the root

    print 'n' //number of iteration

}

else

    print "can not found a root in the given interval"

// C++ Program to find root of an

// equations using secant method

#include <bits/stdc++.h>

using namespace std;

// function takes value of x and returns f(x)

float f(float x)

{

    // we are taking equation as x^3+x-1

    float f = pow(x, 3) + x - 1;

    return f;

}

  

void secant(float x1, float x2, float E)

{

    float n = 0, xm, x0, c;

    if (f(x1) * f(x2) < 0) {

        do {

            // calculate the intermediate value

            x0 = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1));

  

            // check if x0 is root of equation or not

            c = f(x1) * f(x0);

  

            // update the value of interval

            x1 = x2;

            x2 = x0;

  

            // update number of iteration

            n++;

  

            // if x0 is the root of equation then break the loop

            if (c == 0)

                break;

            xm = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1));

        } while (fabs(xm - x0) >= E); // repeat the loop

                                // until the convergence

  

        cout << "Root of the given equation=" << x0 << endl;

        cout << "No. of iterations = " << n << endl;

    } else

        cout << "Can not find a root in the given inteval";

}

  

// Driver code

int main()

{

    // initializing the values

    float x1 = 0, x2 = 1, E = 0.0001;

    secant(x1, x2, E);

    return 0;

}

Output :

Root of the given equation = 0.682326

No. of iterations = 5

Time Complexity = O(1)


Related Solutions

Discuss any API / libraries of your favorite programming language (e.g. python, c#, java etc.) that...
Discuss any API / libraries of your favorite programming language (e.g. python, c#, java etc.) that you have used/reused in any of your previous projects and how it contributes to the overall project.
(20 pts) Using your programming language of choice (from C++, Java, or Python) , also drawing...
(20 pts) Using your programming language of choice (from C++, Java, or Python) , also drawing on your experience from program 1, read an integer, n from keyboard (standard input). This integer represents the number of integers under consideration. After reading that initial integer in, read n integers in, and print the minimum and maximum of all the integers that are read in. Example: Input Output 7 1 5 3 6 9 22 2 Min: 1 Max: 22 C++ preferred
Hi, I am working on an assignment in C-Programming language dealing with LInked lists, in the...
Hi, I am working on an assignment in C-Programming language dealing with LInked lists, in the code there is instructions on where to write the code. I do not know how to write Linked Lists. Has to be in the C-language, Any help is greatly appreciated   //agelink.c //maintains list of agents //uses linked list #include <stdio.h> #include <stdlib.h> #define TRUE 1 void listall(void); void newname(void); void delink(void); void memexit(void); void wfile(void); /********************************************************************* this is the structure to hold a agent...
Implement the MSI cache coherence protocol in your favorite programming language (C, C++, Java, python, etc.)....
Implement the MSI cache coherence protocol in your favorite programming language (C, C++, Java, python, etc.). Wikipedia has a nice high level description of the protocol. Consider only one level of cache which is a write back cache. Moreover, assume that there are 4 processing cores working on a single shared memory. To simplify, assume that you are writing the code for only one block of cache and that block can hold 4 different memory locations.
Java, Python, and C++ are three of the most useful programming languages to learn. Compare the...
Java, Python, and C++ are three of the most useful programming languages to learn. Compare the functionalities of all three programming languages. Why would you choose one language over another? Provide code examples demonstrating their usefulness in a real-world scenario.
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...
The programming language is Python Instructions: Create a function that will delete a node in a...
The programming language is Python Instructions: Create a function that will delete a node in a Linked List based on position number. On below example, if you want to delete position #2, it will remove the Banana (arrangement of nodes below is Apple, Banana, Cherry, Grapes, Orange). myLinkedList = LinkedList() myLinkedList.append("Banana") myLinkedList.append("Cherry") myLinkedList.append("Grapes") myLinkedList.append("Orange") myLinkedList.prepend("Apple") myLinkedList.deleteByPositionNum(2) node = myLinkedList.head while node: print(node.value, " ") node = node.next_node You may start with the function head: def deleteByPositionNum(self, positionNum):
Programming language Python It have to be in Functions with a main function Samuel is a...
Programming language Python It have to be in Functions with a main function Samuel is a math teacher at Hogwarts School of Witchcraft and Wizardry. He loves to give his students multiplication exercises. However, he doesn’t care about the actual operation result but the unit sum of its digits. At Hogwarts School of Witchcraft and Wizardry, they define the unit sum (US) of N as the unit that it is left after doing the sum of all the digits of...
Programming language is C# A common place to buy candy is from a machine. A new...
Programming language is C# A common place to buy candy is from a machine. A new candy machine has been purchased for the gym , but it is not working properly. The machine sells candies, chips, gum, and cookies. You have been asked to create aprogram for this candy machine so that it can be put into operation. The program should do the following: 1. Show the customer the different products sold by the candy machine. 2. Let the customer...
In the C programming language, implement the translation from regular English to Euroglish. If the letters...
In the C programming language, implement the translation from regular English to Euroglish. If the letters were uppercase, keep them uppercase in the replacement. 1. Remove one“e”from the end of words that are more than three characters long, if they happen to end in “e”. 2. Change all double letters to a single letter (including double spaces). Do not remove double line spacing (i.e. “\n”). 3. When a word ends in “ed”, change that to just “d”. Text: The cat...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT