Program that uses integrals to calculate the maxima of a function.

read more

Sample output:


The function reaches a minimum at the input -3.160000000000039
The value of f(x) at this value is: -56.62509600000119 + constant
The function reaches a maximum at the input 0.009999999999937692
The value of f(x) at this value is: -31.626624990001016 + constant
The function reaches a minimum at the input 3.169999999999914
The value of f(x) at this value is: -56.6240009100012 + constant		

import java.math.*;

public class calculateMaximaUsingIntegrals {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// Write an equation using integrals that tests f(x) and determines the
		// max and min points using the integral of the first derivative.
		// This could be done by finding the first derivative, then take the sum
		// of the product of the change of output and the change of input for
		// all inputs. For each change of input,
		// compare the previous product (that of the output and the input) to
		// the following product, if there is a sign change (> or < zero), then
		// a maximum or minimum value is found.
		// If the sign change is from less than zero to greater than zero, there
		// exists a minimum. If greater than zero to less than zero, there is a
		// maximum. The sum corresponds
		// to the value at the input.

		
		//We're inputing the function f'(x). We're obtaining information about f(x). 
		double inputs[] = new double[5000];
		double outputs[] = new double[5000];
		double x = -5; // The starting x value
		int i = 0;
		double endx = Math.abs(x); // The ending x value
		double h = .01; // The change in x values, decreasing this value
						// increases accuracy
		double outputold = 0;
		double outputnew = 0;
		double sum = 0;

		while (x < endx) {
			inputs[i] = x;
			outputs[i] = (inputs[i]*inputs[i] * inputs[i])-(10*inputs[i]);
			sum += (h*outputs[i]);
			outputnew = outputs[i];
			if (outputnew > 0 && outputold < 0) {
				System.out
						.println("The function reaches a minimum at the input "
								+ x);
				System.out
						.println("The value of f(x) at this value is: " + sum +" + constant");

			}
			if (outputnew < 0 && outputold > 0) {
				System.out
						.println("The function reaches a maximum at the input "
								+ x);
				System.out
						.println("The value of f(x) at this value is: " + sum +" + constant");

			}
			outputold = outputnew;
			i++;
			x += h;
		}

	}
}