Exemple #1
0
int main(){
    /*
    a. Rearrange the equation into the form f(x) =0.
    b. Find the derivative, f'(x) of the non-linear function, f(x).
    c. Write two C-functions that output the value of the function and the value of its derivative
    for any given value of x.
    d. Write the calling program (main) that implements Newton-Raphson method to find the
    zero of the non-linear equation in the specified range. Analyze the problem to find a good
    initial guess.
    e. Compute the value of x at equilibrium correct to five decimal places, and print it (to the
    console) with the required number of iterations. In other words, the difference between
    the solution XF and the value of x found on the iteration prior to the last one should not
    be greater than 0.000001.



    x^3 + 3.993 x 10 ^ -4 = 0.165x^2



    a) f(x) = x^3 - 0.165x^2 + 0.0003993
    b) f'(x) 3x^2 - 0.33x^
    */

    int iter = 0;
    float xnew, xold;

    printf("\nDOWN THE TOILET COMPANY");
    printf("\nNewton-Raphson to find zero of f(x) = x^3 - 0.165x^2 + 0.0003993");

    /*float min, max;
    do{
        printf("\nPlease enter range(min max):");
        scanf("%f %f", &min, &max);
    }while(min>max);*/

    printf("\nPlease enter initial value:");
    scanf("%f", &xnew);

    do{
        xold = xnew;
        if (fprime(xold) == 0 ) return -1;
        xnew = xold - f(xold)/fprime(xold);
    } while ((fabs(xnew-xold) > ERR) && (++iter < MAXITER) );

    if (iter > MAXITER) return -1;
    printf("\nThe zero found is: %.5f", xnew);

    return 0;
}
Exemple #2
0
void newton_raphson(double results[], double guess, double abs_pressure,
	double temperature, double a, double b, double max_iterations,
	double converg_criteria) {
	int i = 0;
	double last = guess - f(guess, abs_pressure, a, b, temperature)
						/ fprime(guess, abs_pressure, a, b);
	double this = 0.0;

	for(i = 1; i < max_iterations; i++) {
		this = last - f(last, abs_pressure, a, b, temperature)
			        / fprime(last, abs_pressure, a, b);

		if(fabs((this - last) / this) <= converg_criteria) {
			results[0] = this;
			results[1] = i + 1;
			return;
		}

		last = this;
	}

	results[0] = 0.0;
	results[1] = max_iterations;
}
Exemple #3
0
double BaseCriterion::fprime(const FeatureVector& X, const uint k, const UntiedGaussMixtureDensityClassifier& classifier) {
  vector<double> log_p_xc;
  classifier->classify(X,log_p_xc);
  for(uint i=0;i<log_p_xc.size();++i) {
    log_p_xc[i]*=classifier.priors_[i];
  }
  
  double infprime=log_p_xc[k];
  double sum=0.0;
  for(uint c=0;c<log_p_xc.size();++c) {
    sum+=log_p_xc[c];
  }
  infprime/=sum;
  
  return fprime(infprime);
}
void newton_method(double a, double b)
{
   double x[3];
   unsigned int k;
   x[0] = a;
   x[1] = b;
   
   printf("%d \t %5.20f \t %5.20f \n", 0, x[0], f(x[0]) );
   printf("%d \t %5.20f \t %5.20f \n", 1, x[1], f(x[1]) );

   for(k = 1; k < 30 && f(x[1]) > 0; k++ )
   {
        x[2] = x[1] - f(x[1])/fprime(x[1]);
	x[0] = x[1];
	x[1] = x[2];
        printf("%d \t %5.20f \t %5.20f \n", k+1, x[2], f(x[2]) );
   }
   printf("\n");
}
Exemple #5
0
void newton::solve(real xp)
{
  real v = f(xp);
  real xnew=xp;

  nit = 0;
  for(int k = 1; k <= maxit; ++k,++nit) {
    double derv = fprime(xp);
    if(!derv) {
      std::cerr << "ERROR: Division by 0 occurred in Newton algorithm"
        << std::endl;
      exit(1);
    }

    xnew = xp - v / derv;
    v = f(xnew);
    if(converged(fabs(xnew - xp), fabs(v),tol,criteria)) break;
    xp = xnew;
  }
   done=true;
	alpha=xnew;
	return;
}