Question

write a computer program ( C++ ) that gives roots of an equation by newton raphson's...

write a computer program ( C++ ) that gives roots of an equation by newton raphson's methode.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The coding is done based on your requirement and is given below with sample output :

code:

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
double f(double x); //declare the function for the given equation
double f(double x) //define the function here, ie give the equation
{
double a=pow(x,3.0)-x-11.0; //write the equation whose roots are to be determined
return a;
}
double fprime(double x);
double fprime(double x)
{
double b=3*pow(x,2.0)-1.0; //write the first derivative of the equation
return b;
}
int main()
{
double x,x1,e,fx,fx1;
cout.precision(4); //set the precision
cout.setf(ios::fixed);   
cout<<"Enter the initial guess\n"; //take an intial guess
cin>>x1;
cout<<"Enter desired accuracy\n"; //take the desired accuracy
cin>>e;
fx=f(x);   
fx1=fprime(x);
cout <<"x{i}"<<" "<<"x{i+1}"<<" "<<"|x{i+1}-x{i}|"<<endl;   
  
do   
{
x=x1; /*make x equal to the last calculated value of x1*/
fx=f(x); //simplifying f(x)to fx
fx1=fprime(x); //simplifying fprime(x) to fx1
x1=x-(fx/fx1); /*calculate x{1} from x, fx and fx1*/
cout<<x<<" "<<x1<<" "<<abs(x1-x)<<endl;   
}while (fabs(x1-x)>=e); /*if |x{i+1}-x{i}| remains greater than the desired accuracy, continue the loop*/
cout<<"The root of the equation is "<<x1<<endl;
return 0;
}

Sample Output:

gcc version 4.6.3 Enter the initial guess 2 Enter desired accuracy 0001 xi x(i+1) 2.0000 2.4545 2.3764 2.3737 The root of the


Hope this Helps, if you have any doubs please comment i will get back to you and please thumbs up, thank you.

Add a comment
Know the answer?
Add Answer to:
write a computer program ( C++ ) that gives roots of an equation by newton raphson's...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • question number 2 1. Write a computer program to solve the equation x = tan x...

    question number 2 1. Write a computer program to solve the equation x = tan x by means of Newton's method. Find the roots nearest 4.5 and 7.7. ( 2. (Continuation) Write and test a program to compute the first ten roots of the equation tan x =x. (This is much more difficult than the preceding computer problem.) Cultural note: If a1, a2, ... are all the positive roots of this equation, then Zie A2 = 1/10. (Amer. Math. Monthly,...

  • . Write a program using C++ that used a function call Roots which will find the...

    . Write a program using C++ that used a function call Roots which will find the roots of the          second degree equation your function supposed to receive the three coefficient      of the second degree equation and return the two roots of the equation after       checking what type of roots.

  • CSC 211 - Lab-2 Write a C++ program to find the roots of a quadratic equation...

    CSC 211 - Lab-2 Write a C++ program to find the roots of a quadratic equation ax +bx+c=0. The roots are given by the formula, x=-b I56²-4ac 2a x = -b+ √b²-4ac 2. x2 = -b-√6²-4ac 2a Instructions: 1. Type, compile, and run the program in an online C++ compiler. 2. Open a word document and copy the following items onto it: a. The source code b. Screenshot of your program's result 3. Save the word document as lastnameFirstname_Lab2.docx (for...

  • C++ The roots of the quadratic equation ax² + bx + c = 0, a ≠...

    C++ The roots of the quadratic equation ax² + bx + c = 0, a ≠ 0 are given by the following formula: In this formula, the term b² - 4ac is called the discriminant. If b² - 4ac = 0, then the equation has a single (repeated) root. If b² - 4ac > 0, the equation has two real roots. If b² - 4ac < 0, the equation has two complex roots. Instructions Write a program that prompts the...

  • The roots of the quadratic equation ax2 + bx + c = 0, a following formula:...

    The roots of the quadratic equation ax2 + bx + c = 0, a following formula: 0 are given by the In this formula, the term i2 - 4ac is called the discriminant. If b4ac 0 then the equation has a single (repeated) root. If -4ac > 0, th equation complex roots. Write a program that prompts the user to input the value of a (the coefficient of ), b (the coefficient of x), and c (the n has two...

  • Write a C++ program to compute both roots of the quadratic equation when the user provides...

    Write a C++ program to compute both roots of the quadratic equation when the user provides the three coefficients A, B, and C. Specifically, A. Display “Your Name” B. Display “Project_2 Problem_1” C. Display “This program computes both roots of a quadratic equation” D. Display “given the coefficients A, B, and C” E. Real_1 = 0 F. Real_2 = 0 G. Imag = 0 H. D = 0 I. DD =0 J. Flag = ‘Y’ K. DO a. A =...

  • Use C++, visual studio format. Please Write a program to solve the quadratic equation and find...

    Use C++, visual studio format. Please Write a program to solve the quadratic equation and find only the real and equal roots. the program should test for three types of roots tell the users the roots type and then calculate the real roots. the user enters the values of a ,b and c. you calculate r1 and r2. c

  • 4-6 on matlab 4. Write a program in a script file that determines the real roots...

    4-6 on matlab 4. Write a program in a script file that determines the real roots of a quadratic equation ax2+bx+c 0 When the file runs, it asks the user to enter the values of the constants a, b, and c. To calculate the roots of the equation the program calculates the discriminant D, given by: D b2-4ac When D 0, the program displays message "The equation has two roots," and the roots are displayed in the next line. When...

  • reword m the program into a design document diregard the m Write a C++ program that...

    reword m the program into a design document diregard the m Write a C++ program that solves a quadratic equation to find its roots. The roots of a quadratic equation ax2 + bx + c = 0 (where a is not zero) are given by the formula -b + b2 - 4ac 2a The value of the discriminant b2 - 4ac determines the nature of roots. If the value of the discriminant is zero, then the equation has a single...

  • In Python. The two roots of a quadratic equation ax^2 + bx + c = 0...

    In Python. The two roots of a quadratic equation ax^2 + bx + c = 0 can be obtained using the following formula: r1 = (-b + sqrt(b^2 - 4ac) / (2a) and r2 = (-b - sqrt(b^2 - 4ac) / (2a) b^2 - 4ac is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative, the equation has no...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT