why x works but is undeclared? c++ -
one of example problems basing program off of secant root finding uses x, doesn't declare it, runs successfully. meanwhile when use same setup error saying x undeclared. why work in example program , 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 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 x double root; // root found secant method cout<<"enter a, b, c, , d"<<endl<<"separated space "; cin>>a>>b>>c>>d; cout<<"\nenter 2 initial values x,\nseparated space: "; cin>>x0>>x1; secant(a, b, c, d, x0, x1, root, iteration);// call "secant" // output cout<<"\nthe root = "<<root<<endl; cout<<"the number of iterations = "<<iteration<<endl; cout<<"the value of f(x) @ root = "<<fx(a,b,c,d,root)<<endl<<endl; system("pause"); return 0; } // definition of function "secant" // receives a, b, c, d , x0 values main program // returns root , 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; { ++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 program used same structure saying x 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 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 x double root; // root found 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; (int velocity = 16000; velocity <= 17500; velocity += 500) { (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 = "<<root<<endl; cout<<"the number of iterations = "<<iteration<<endl; cout<<"the value of f(x) @ root = "<<fx(radians, velocity, kr, uc, q, b, root)<<endl<<endl; system("pause"); return 0; } // definition of function "secant" // receives a, b, c, d , x0 values main program // returns root , 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; { ++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); }} }
double fx(double radians,double velocity, double kr, double uc, double q, double b, double x);
you have semicolon ";" @ end of line. :)
should be
double fx(double radians,double velocity, double kr, double uc, double q, double b, double x)
you have same error when defining secant function. when define function don't put semicolon @ end.
final code should 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 convergence const int max_iter=50; // maximum iterations allowed using namespace std; // <- goes after including headers // main program int main() { int iteration; // number of iterations double kr, uc, q, b, radians; double x0, x1; // starting values x double root; // root found 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; (int velocity = 16000; velocity <= 17500; velocity += 500) { (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 = "<<root<<endl; cout<<"the number of iterations = "<<iteration<<endl; cout<<"the value of f(x) @ root = "<<fx(radians, velocity, kr, uc, q, b, root)<<endl<<endl; system("pause"); return 0; } // definition of function "secant" // receives a, b, c, d , x0 values main program // returns root , 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; { ++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); }
Comments
Post a Comment