Thread: Need help for solving a function.

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    6

    Need help for solving a function.

    How can I multiply x with y? I mean I have to find AREA between function line and X-Axis.

    Exercise is like this:
    Function: y=tan(x/2) -> tan(x/2)
    Determination area: 0,2<x<1,55

    Code:
    #include<stdio.h>
    #include<math.h>
    #include<conio.h>
    
    
    int main()
    {
    float x,y,sum;
    
    
    for(x=0.2; x<1.55;x*tan(x/2)){ 
            x = x + 0.0135*(x*tan(x/2));
            y =tan(x/2);
            sum=
            printf("%f %f\n",x,y);
        }
            printf("Answer is: %f\n",sum);
        _getch();
        return 0;
    
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by peppeNzjik View Post
    How can I multiply x with y?
    Code:
    z = x * y;

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    6
    Quote Originally Posted by quzah View Post
    Code:
    z = x * y;

    Quzah.
    Yeah, I understand that, but when it prints x and y, then x is from 0.36 to 1.56 and y is 0.18 to 0.99.

    I need to sum all numbers in X column and all numbers in Y column. How can I do it when there are so many numbers in one column?

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Can you be more clear? Do you want to multiply 'x' and 'y' before, during, or after you sum their column of values?

    I need to sum all numbers in X column and all numbers in Y column. How can I do it when there are so many numbers in one column?
    Like this?

    Code:
    float x,y;
    float x_sum = 0;
    float y_sum = 0;
    
    loop()
    {
        // do stuff to 'x'
        x_sum += x;
        // do stuff to 'y'
        y_sum += y;
    }

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    So you are looking for the integral of the function, the area under the curve?

    It appears you are approximating it as a series of rectangles.
    In that case you want to multiply each value of y by the step size of x.
    And these individual areas (y * step size of x) are added up.

    I believe you only want to increment x by 0.0135 each time through the loop:
    (I'm guessing that 0.0135 is the step size you want to use)

    x = x + 0.0135;

    not all this:

    x = x + 0.0135*(x*tan(x/2));

    You are trying to accumulate the sum in x. You need to accumulate the sum in 'sum':

    sum = sum + 0.0135*y;
    Last edited by megafiddle; 06-27-2012 at 03:43 PM.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    The reason x isn't printing out with it's starting value of 0.2 is that you are modifying it from it's starting value
    before you are printing it out.

    Also some suggestions:

    Initialize 'sum' to 0 before the loop begins.

    In the "for" statement:

    for(x=0.2; x<1.55;x*tan(x/2),

    this part "x*tan(x/2)" doesn't do anything. What you should put here is the increment of the x variable: x = x+0.0135, and remove the increment of x
    from the body of the loop. The print statement will also output the value of x as you expect, since x will not be incremented until the end of the loop.

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    6
    Quote Originally Posted by megafiddle View Post
    So you are looking for the integral of the function, the area under the curve?

    It appears you are approximating it as a series of rectangles.
    In that case you want to multiply each value of y by the step size of x.
    And these individual areas (y * step size of x) are added up.

    I believe you only want to increment x by 0.0135 each time through the loop:
    (I'm guessing that 0.0135 is the step size you want to use)

    x = x + 0.0135;

    not all this:

    x = x + 0.0135*(x*tan(x/2));

    You are trying to accumulate the sum in x. You need to accumulate the sum in 'sum':

    sum = sum + 0.0135*y;
    This is exactly what I mean.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Your program only needs those few changes.
    Have you tried fixing it up?

    Post a corrected version of it, and it should be simple to get it working from there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can anyone help me solving that Q?
    By Amjad Salman in forum C Programming
    Replies: 1
    Last Post: 03-04-2012, 11:50 AM
  2. How should I go about solving this problem?
    By ARod609 in forum C Programming
    Replies: 11
    Last Post: 08-06-2011, 09:37 PM
  3. program not solving
    By baphomet90 in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2010, 09:25 AM
  4. Solving maze using rec'
    By gavra in forum C Programming
    Replies: 14
    Last Post: 07-13-2008, 09:20 AM
  5. Help solving this problem
    By marcelomdsc in forum C++ Programming
    Replies: 6
    Last Post: 05-13-2008, 08:32 AM