Question

I was doing my math hw, and it required me to calculate areas between 2 lines. I solved couple of them, I got them right(its online so it shows me whether i got it wrong or right), then I thought about writing a program that does the calculation for me, I created a nice algorithm, used java for the program, but in the end it didn't give me the right answer. I put the data from one of the questions I already solved whether i got it right or wrong.

Can you please tell me the mistake in the algorithm??

public class DailyHelper {

public static double f(double x) {
    double y = 5*x;
    return y;
}

public static double g(double x) {
    double y= 4*x*x;
    return y;
}

public static void main(String[] args) {
    double xLower = 0;
    double xHigher = 5/4;
    double areaF=0;
    double areaG=0;
    double change = (xHigher-xLower)/100000;
    for(double k=xLower; k<xHigher; k=k+change) {
        areaF = areaF+(change*f(k));
    }

    for(double k=xLower; k<xHigher; k=k+change) {
        areaG = areaG+(change*g(k));
    }
    double area = areaF-areaG;
    System.out.println(area);
}
}
Was it helpful?

Solution

Just a quick thought. Your xHigher variable is always 1, since you're dividing int by int. Try 5/4d

OTHER TIPS

If there are 2 straight random lines and you want to calculate area from vertical lines L1 and L2, L1 < L2, then you must construct that trapezoid (by finding the corner coordinates) and calculate the area. However this doesn't work on non-linear "lines", you'll need to follow a rule like that: http://en.wikipedia.org/wiki/Numerical_integration#Methods_for_one-dimensional_integrals

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top