Question

write a c++ program that can solve problems using the secant method

write a c++ program that can solve problems using the secant method

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

Question : write a c++ program that can solve problems using the secant method

ANSWER : secant method is a root-finding algorithm.

Algorithm :
1) Enter the values of initial guesses, a and b.
2) Enter the value of the tolerance error, e (accuracy)
3) Repeat
set a = b
set b = c
set c = b-f(b)*(a-b)/(f(a)-f(b))
until |cn-c|<Tolerance value (e)
4) Print 'C', tne root in the interval [a,b].

Program :

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
double f(double x); //declare the function for the given equation
double f(double x) //declare the function for the given equation
{
double a=pow(x,3)-x-11.0; //write the equation whose roots are to be determined
return a;
}
int main()
{
cout.precision(4);
cout.setf(ios::fixed); //set the precision of the output
double a,b,c,e;
cout<<"Enter the initial guess: \na = ";
cin>>b;
cout<<"b = ";
cin>>c;
cout<<"Enter the degree of accuracy\n";
cin>>e; //take the desired accuracy
do
{
a=b;
b=c;
c=b-(b-a)/(f(b)-f(a))*f(b); //calculate c
if (f(c)==0)
{
cout<<"\nThe root of the equation is "<<c; //print the root
return 0;
}
}while(abs(c-b)>=e); //check if the error is greater than the desired accuracy
cout<<"\nThe root of the equation is "<<c; //print the root
return 0;
}

OUTPUT :

崀main.cpp [secanttt-Code:Blocks 16.01 File Edit View Search Project Build Debug Fortran woSmithTools Tools+Plugins DoxyBlocks

崀main.cpp [secanttt-Code:Blocks 16.01 File Edit View Search Project Build Debug Fortran woSmithTools Tools+Plugins DoxyBlocks

Add a comment
Know the answer?
Add Answer to:
write a c++ program that can solve problems using the secant method
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
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