Absolute Value Methods



   public  static  double  abs(double  value)
   
   public  static  float  abs(float  value)
   
   public  static  int  abs(int  value)
   
   public  static  long  abs(long  value)

abs() returns the absolute value of the input argument. This method is overloaded to accept double , float , int , and long arguments. If the argument is infinite, the return value is positive infinity. If the argument is the value NaN , the return value will be NaN .

Example: Using the abs() Method

A typical use for the abs() method is to check the convergence level of a computation. When an equation is being iterated, updates to the dependent variable or variables may be either positive or negative. When the absolute value of the updates reaches a specified tolerance, the result is assumed to be converged and the computation can stop.

As an example, we will compute the surface temperature of a body exposed to a gas flow by performing a heat balance on the body surface. If the body is nonconducting, the heat coming in to the body due to a thermal gradient in the gas flow around the body is equal to the heat radiating back from the body. Eq. (16.1) shows a simplified governing equation for this situation.

Equation 16.1

graphics/16equ01.gif


In Eq. (16.1), k is the thermal conductivity of the gas, T / x is the temperature gradient at the wall, s is the Stefan-Boltzmann constant with a value of 5.67e-12, e is the emissivity of the surface, and T wall is the wall temperature. If we assume that the temperature gradient can be recast as a finite-difference expression (Eq. (16.2)),

Equation 16.2

graphics/16equ02.gif


then finding the wall temperature according to the surface heat balance comes down to solving a fourth-order equation.

Equation 16.3

graphics/16equ03.gif


Eq. (16.3) cannot be solved directly so a solution must be found by iteration. A standard solution technique for this type of problem is the Newton-Raphson iteration. Starting with an initial guess for the value of T wall , updates to the value are obtained from the expression

Equation 16.4

graphics/16equ04.gif


The iteration in Eq. (16.4) is continued until the absolute value for the updates to the wall temperature is less than a specified tolerance. The AbsDemo class demonstrates a Newton-Raphson solution of the heat balance problem. It follows the preceding discussion with some slight simplifications along the way. For one thing the thermal conductivity is assumed to be a constant when in fact it will be a function of temperature. The abs() method is used inside the iteration loop to determine if the calculation has reached convergence.

 public class AbsDemo {   public static void main(String args[]) {   //  Define some variables and some constants     double sigma = 5.67e-12;     double emissivity = 0.85;     double kappa = 0.05;     double T2 = 1000.0;     double deltaX = 1.0e-2;     double tolerance = 0.01;     double T, A, f, dfdT, deltaT;     A = sigma*emissivity*deltaX/kappa;     //  Make an initial guess for wall temperature and     //  iterate the heat balance equation.  When the     //  absolute value of deltaT is below a specified     //  tolerance, the iteration is complete.     T = 500.0;     System.out.println("Wall temperature is "+T+" K");     do {       f = A*Math.pow(T,4.0) + T - T2;       dfdT = 4.0*A*Math.pow(T,3.0) + 1.0;       deltaT = -f/dfdT;       T += deltaT;       System.out.println("Wall temperature is "+T+" K");     } while ( Math.abs(deltaT) > tolerance );   } } 

Output ”

 Wall temperature is 500.0 K Wall temperature is 999.6989263524445 K Wall temperature is 999.0397993308752 K Wall temperature is 999.0397968305151 K 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net