Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4609088
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:56:13+00:00 2026-05-22T00:56:13+00:00

One of the example problems i am basing a program off of for secant

  • 0

One of the example problems i am basing a program off of for secant root finding uses x, but doesn’t declare it, but runs successfully. Meanwhile when i use the nearly the same setup i get an error saying x is undeclared. Why does it work in the example program and not mine?

using namespace std;
#include<iostream>
#include<cmath>
#include<iomanip>
// Declaration of functions used
void secant(double, double, double, double,
               double, double, double&, int& );
double fx(double, double, double, double, double);
const double tol=0.0001;    // Tolerance for convergence
const int max_iter=50;      // Maximum iterations allowed
// main program
int main()
{
    int iteration;          // Number of iterations
    double a, b, c, d;      // Constants in f(x)
    double x0, x1;          // Starting values for x
    double root;           // Root found by secant method
    cout<<"Enter a, b, c, and d"<<endl<<"separated by a space ";
    cin>>a>>b>>c>>d;
    cout<<"\nEnter two initial values for x,\nseparated by a space: ";
    cin>>x0>>x1;
    secant(a, b, c, d, x0, x1, root, iteration);// Call "secant"
// Output
    cout<<"\nThe root is = "<<root<<endl;
    cout<<"The number of iterations was = "<<iteration<<endl;
    cout<<"The value of f(x) at the root = "<<fx(a,b,c,d,root)<<endl<<endl;
 system("pause");
 return 0;
}
// Definition of function "secant"
// Receives a, b, c, d and x0 values from main program
// Returns root and the iterations required
void secant(double a,double b, double c, double d, 
        double x0, double x1, double& root, int& iteration)
{
    double xnminus1, xnplus1, xn; // Local variables
    iteration=0;                  // Initialize iterations
    xnminus1=x0;
    xn=x1;
    do
    {
       ++iteration;
       xnplus1 = xn - fx(a,b,c,d,xn)*(xn-xnminus1)/
                                  (fx(a,b,c,d,xn)-fx(a,b,c,d,xnminus1));
       cout<<"x"<<iteration+1<<" = "<<xnplus1<<endl;
       xnminus1 = xn;
       xn=xnplus1;
    }  
    while ((fabs(fx(a,b,c,d,xnplus1)) >= tol )&& (iteration < max_iter));
    root=xnplus1;  
}
// Defines "fx" -- calculates f(x) = ax^3+bx^2+cx+d
double fx(double a, double b, double c, double d, double x)
{
    return a*pow(x,3)+b*pow(x,2)+c*x+d;
}

here is my program which i used the same structure but it is saying x is undeclared?

 using namespace std;
    #include<iostream>
    #include<cmath>
    #include<iomanip>
    #include<fstream>
    // Declaration of functions used
    void secant(double, double, double, double,
                   double, double, double, double, double&, int& );
    double fx( double, double, double, double, double, double, double);
    const double tol=0.0001;    // Tolerance for convergence
    const int max_iter=50;      // Maximum iterations allowed
    // main program
    int main()
    {
        int iteration;          // Number of iterations

        double kr, uc, q, b, radians;

        double x0, x1;          // Starting values for x
        double root;           // Root found by secant method
    const double PI = 4.0*atan(1.0);
    ifstream datain ("shuttle.txt");
    ofstream dataout ("results.txt");
    datain >> kr >> uc >> q >> b;
    int velocity = 16000;
    double angle =10;
    x0= 1000;
    x1 = 200;
    for (int velocity = 16000; velocity <= 17500; velocity += 500) {
        for (int angle = 10; angle <= 70; angle += 15) {
    radians= angle * PI/180  ;
                  cout << velocity << endl;
                  cout << radians << endl;
                  cout << angle << endl;
                  secant (radians, velocity, kr, uc, q, b, x0, x1, root, iteration);

    // Output
        cout<<"\nThe root is = "<<root<<endl;
        cout<<"The number of iterations was = "<<iteration<<endl;
        cout<<"The value of f(x) at the root = "<<fx(radians, velocity, kr, uc, q, b, root)<<endl<<endl;
     system("pause");
     return 0;
    }
    // Definition of function "secant"
    // Receives a, b, c, d and x0 values from main program
    // Returns root and the iterations required
    void secant(double radians,double velocity, double kr, double uc, double q, double b, double x0, double x1, double& root, int& iteration);
    {
        double xnminus1, xnplus1, xn; // Local variables
        iteration=0;                  // Initialize iterations
        xnminus1=x0;
        xn=x1;
        do
        {
           ++iteration;
           xnplus1 = xn - fx(radians, velocity, kr, uc, q, b, xn)*(xn-xnminus1)/
                                      (fx(radians, velocity, kr, uc, q, b, xn)-fx(radians, velocity, kr, uc, q, b, xnminus1));
           cout<<"x"<<iteration+1<<" = "<<xnplus1<<endl;
           xnminus1 = xn;
           xn=xnplus1;
        }  
        while ((fabs(fx(radians, velocity, kr, uc, q, b, xnplus1)) >= tol )&& (iteration < max_iter));
        root=xnplus1;  
    }
    // Defines "fx" -- calculates f(x) = ax^3+bx^2+cx+d
    double fx(double radians,double velocity, double kr, double uc, double q, double b, double x);
    {
         return kr * pow(x,4.0) + uc * x - q - pow(velocity/b, 2.0) * sin(radians);

    }}
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:56:14+00:00Added an answer on May 22, 2026 at 12:56 am
    double fx(double radians,double velocity, double kr, double uc, double q, double b, double x);
    

    you have an extra semicolon “;” at the end of the line. 🙂

    Should be

    double fx(double radians,double velocity, double kr, double uc, double q, double b, double x)
    

    You also have the same error when defining the secant function. When you define a function you don’t put a semicolon at the end.

    Final code should look something like this

    #include<iostream>
    #include<cmath>
    #include<iomanip>
    #include<fstream>
    // Declaration of functions used
    void secant(double, double, double, double, double, double, double, double, double&, int& );
    double fx( double, double, double, double, double, double, double);
    const double tol=0.0001;    // Tolerance for convergence
    const int max_iter=50;      // Maximum iterations allowed
    
    using namespace std; // <- Goes after including the headers
    
    // main program
    int main()
    {
            int iteration;          // Number of iterations
    
            double kr, uc, q, b, radians;
    
            double x0, x1;          // Starting values for x
            double root;           // Root found by secant method
            const double PI = 4.0*atan(1.0);
            ifstream datain ("shuttle.txt");
            ofstream dataout ("results.txt");
            datain >> kr >> uc >> q >> b;
            int velocity = 16000;
            double angle =10;
            x0= 1000;
            x1 = 200;
            for (int velocity = 16000; velocity <= 17500; velocity += 500)
            {
                for (int angle = 10; angle <= 70; angle += 15)
            {
                radians= angle * PI/180  ;
                        cout << velocity << endl;
                        cout << radians << endl;
                        cout << angle << endl;
                        secant (radians, velocity, kr, uc, q, b, x0, x1, root, iteration);
            }
        }
    
        // Output
            cout<<"\nThe root is = "<<root<<endl;
            cout<<"The number of iterations was = "<<iteration<<endl;
            cout<<"The value of f(x) at the root = "<<fx(radians, velocity, kr, uc, q, b, root)<<endl<<endl;
            system("pause");
         return 0;
    }
    
    
    // Definition of function "secant"
    // Receives a, b, c, d and x0 values from main program
    // Returns root and the iterations required
    void secant(double radians,double velocity, double kr, double uc, double q, double b, double x0, double x1, double& root, int& iteration)
    {
            double xnminus1, xnplus1, xn; // Local variables
            iteration=0;                  // Initialize iterations
            xnminus1=x0;
            xn=x1;
            do
            {
               ++iteration;
               xnplus1 = xn - fx(radians, velocity, kr, uc, q, b, xn)*(xn-xnminus1)/
                                          (fx(radians, velocity, kr, uc, q, b, xn)-fx(radians, velocity, kr, uc, q, b, xnminus1));
               cout<<"x"<<iteration+1<<" = "<<xnplus1<<endl;
               xnminus1 = xn;
               xn=xnplus1;
            }  
            while ((fabs(fx(radians, velocity, kr, uc, q, b, xnplus1)) >= tol )&& (iteration < max_iter));
            root=xnplus1;  
    }
    
    
    // Defines "fx" -- calculates f(x) = ax^3+bx^2+cx+d
    double fx(double radians,double velocity, double kr, double uc, double q, double b, double x)
    {
            return kr * pow(x,4.0) + uc * x - q - pow(velocity/b, 2.0) * sin(radians);
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Even today, one frequently sees character encoding problems with significant frequency. Take for example
I found one example in which buttons are added to panels (instances of JPanel
I'm getting the following from a third party library (one example): @%SystemRoot%\SomePath\SomeFile.Dll,-203 I know
I start to use Java annotations heavily. One example is taking method with annotations
I wanted to develop one HTTP example on win32 platform, which is asynchronous. I
I have a table, has many rows. for example one of from rows(all rows
I have a table, has many rows. for example one of from rows(all rows
In one of the example servers given at golang.org: package main import ( flag
Can some one post an example of using syslog outputter for log4r, I am
Situation: I'm trying run an https store (xcart) under one domain secure.example.com and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.