Finding the area under a curve is a useful tool in a large number of problems in
ID: 3548752 • Letter: F
Question
Finding the area under a curve is a useful tool in a large number of problems in many areas of science, engineering, and business. Mathematically, this is integration. For a curve produced by a function, you may be able to integrate the function from a to b and calculate the area under the curve in that way. However, for curves produced from data, or for curves that are produced by some complicated functions, analytical integration may not be possible. In these cases, the most common way to find the answer is to perform the integration "numerically". This can be done a number of ways, but one easy way is to employ programming.
Consider the function shown below. How could we calculate the area under the graph from the origin to b?
Mathematically, the area under f(x) from 0 to b is given by the following operation:
When we do not know the function f(x), or we cannot evaluate it analytically, we need to approximate the above integral numerically. This can be done in a number of ways, some of which are better than others. Thetrapezoidal rule is one way to calculate this integral that is (1) easy to implement; (2) quite accurate; and (3) quite robust.
The idea is to break the function up into a number of trapezoids and calculate their areas. The area of the shaded trapezoid above is
The area under the graph is therefore the sum of the trapezoids.
The accuracy of the numerical integration will go up with decreased spacing between the time points. In fact, if you halve the time-spacing of your data (i.e., take twice as many measurements of the same length of time), the accuracy of the numerical integration will go up by a factor of 4.
Write methods for calculating integral. The range, the spacing and the function will be given as parameter. And you will calculate the integral for the given range and given function. Function will be a String and composed of only x variable, constant numbers, +, -, /. No parenthesis allowed. Numbers after x are powers of x. Hint: You can use String class charAt(i) method.
Example parameter functions and their interpretations:
Area = f(x)dx Area = (t2 - t1) [f(t1) + f(t2) / 2]Explanation / Answer
import java.util.Scanner;
public class IntegralCalc
{
public static double calcOp(double num1, double num2, char op)
{
if (op == '+')
return num1 + num2;
if (op == '-')
return num1 - num2;
return num2;
}
public static int getEndOfConstant(String func, int start)
{
int end = start;
while (end < func.length() &&
(func.charAt(end) == '.' || Character.isDigit(func.charAt(end))))
{
end++;
}
return end;
}
public static int skipSpace(String func, int start)
{
while (start < func.length() && Character.isWhitespace(func.charAt(start)))
start++;
return start;
}
public static double calcFunc(String func, double x)
{
double result = 0;
char op = ' ';
double curr = 0;
int i = 0;
while (i < func.length())
{
char ch = func.charAt(i);
if (ch == '+' || ch == '-')
{
result = calcOp(result, curr, op);
op = ch;
curr = 0;
i++;
}
else if (Character.isDigit(ch) || ch == 'x' || ch == 'X') // 0 - 9 or x
{
curr = 0;
if (Character.isDigit(ch))
{
int end = getEndOfConstant(func, i);
curr = Double.parseDouble(func.substring(i, end));
i = skipSpace(func, end);
}
// calculate x^n
if (i < func.length() && (func.charAt(i) == 'x' || func.charAt(i) == 'X'))
{
i = skipSpace(func, i+1);
double base = 1;
if (i < func.length() && Character.isDigit(func.charAt(i)))
{
int end = getEndOfConstant(func, i);
base = Double.parseDouble(func.substring(i, end));
i = skipSpace(func, end);
}
double value = Math.pow(x, base);
if (ch == 'x' || ch == 'X') // no previous constant
curr = value;
else
curr = curr * value;
}
if (i < func.length() && func.charAt(i) == '/')
{
i = skipSpace(func, i+1);
if (i < func.length() && Character.isDigit(func.charAt(i)))
{
int end = getEndOfConstant(func, i);
double div = Double.parseDouble(func.substring(i, end));
i = skipSpace(func, end);
curr = curr / div;
}
}
}
else
{
i++;
}
}
result = calcOp(result, curr, op);
System.out.println("f(" + x + ") = " + result);
return result;
}
public static double calcIntegral(double start, double end, double spacing, String func)
{
double sum = 0;
double last = 0; // previous value
double x = start;
for (; x <= end; x += spacing)
{
if (x == start)
{
last = calcFunc(func, x);
}
else
{
double curr = calcFunc(func, x);
sum += (curr + last) * spacing / 2;
last = curr;
}
}
if (x - spacing < end)
{
x -= spacing;
double curr = calcFunc(func, end);
sum += (curr + last) * (end - x) / 2;
}
return sum;
}
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
do
{
System.out.print("Enter the function: ");
String func = input.nextLine();
System.out.print("Enter start of the range: ");
double start = Double.parseDouble(input.nextLine());
System.out.print("Enter end of the range: ");
double end = Double.parseDouble(input.nextLine());
System.out.print("Enter the spacing: ");
double spacing = Double.parseDouble(input.nextLine());
double integral = calcIntegral(start, end, spacing, func);
System.out.println("The integral is " + integral);
System.out.println();
System.out.print("Would you want to calculate another(y/n): ");
} while (input.nextLine().toLowerCase().startsWith("y"));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.