Assignment #1

 

1 Introduction to Integration

Integration is a way of adding slices to find the whole. Integration can be used to find areas, volumes, central points and many useful things. But it is easiest to start with finding the area under the curve of a function like this:

pic01

What is the area under y = f(x) ?

 

2 Slices

A simple way of computing the area under a given curve f(x) is by computing the area of small rectangles under the curve and adding them up, as shown in the figure below:

 pic02

Each rectangle (or slices) can be obtained by multiplying the base Δx by its high that is the value of f(x) for a given x.

The larger the value of Δx, the bigger will be the difference between the sum of the slices and the actual integral value. As the value of Δx gets smaller and smaller the answer is gets better and better.

pic03

And as the slices approach zero in width, the answer approaches the true answer. We now write dx to mean the Δx slices are approaching zero in width.

pic04

3 Program

Create a program that integrates the f(x) = sin(x) based on the slice procedure discussed above and for a given interval, such as a < x < b. For a given large number of slices the following two results would be expected from your program (not the GUI, but just the results). (Note: see video here for how your program should looks like when running)

 pic05  pic06
For a = 1.1 and b = 2.0, the integral of sine is 0.95645 For a = 0 and b = PI, the integral of sine is 2

In the sections below, a description of the main features of your program as described.

3.1 Inputting the necessary data to compute the area

Your program shall request the initial integration point a, final integration point b, and a value that represents how many slices (Δx) your program shall use between point a and b. These three numbers shall be provided in a single input and separated by comma. Your program shall use JOptionPane to prompt the user to enter the data.

3.1.1  Program shall allow the user to compute multiple areas

After computing the area under f(x) curve for a given interval (a,b), the program will ask again for another set of inputs (a, b, and Δx). This can be used to check how values of Δx impact in the final computed area. The program shall finish when the user provides -1 as input.

3.1.2  Program shall verify the validity of the inputs

Your program shall check if:

  1. All the numbers are numbers. Example: -1, 2, 100 is a valid set, while -1, two, 100 is not.
  2. Another input error is when the user provides more or less input values as expected (there should be always three numbers separated by comma being provided).
  3. The value of a must be smaller than value of b.
  4. The number of slices must be bigger than 0.
  5. When finding an invalid input set, the program shall use the JOptionPane to display a message that the input is invalid and after the user clicks OK, he/she is prompted to re-enter the set once again.

3.2  Outputting the necessary data to compute the area

Your computer shall show the final integral value of the f(x) as shown on the following figure. Your JOptionPane shall show the function being integrated, the intervals (a,b) and the number of slices.

4  Overall program structure

Your program will be graded not only for successfully implemented the functionality described above, but also on how you have structured your program, as described below:

  • Correct use of camelCase for class name, method name, and variable name.
  • Use meaningful variable names (temp1, temp2, temp3 are not good names since we do not know what they are storing).
  • Create methods that contain only one functionality: one for input, other for checking input validity, one for the function to be integrated (i.e., a method that request the x and returns f(x) value for any given point between (a,b).
  • Avoid magic numbers
  • Correct Indentation
  • Error-free compilation

5 Test your program

  • Go to the website http://www.wolframalpha.com/widgets/view.jsp?id=8ab70731b1553f17c11a3bbc87e0b605 and try to enter different intervals a and b and see if the final area matches with your program’s output.
  • Check if your program recognizes invalid inputs.
  • Check if your program exists correctly.
  • Check your code format and things discussed in Section 4.
  • Try to use another function f(x). If you have used methods in your code, this change should be very simple and very localized.