Modify program so that it finds maximum crest & minimum trough for combined waves

This is a discussion on Modify program so that it finds maximum crest & minimum trough for combined waves within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <math.h> #define PI 3.141593 int main(void) { /* Declare variables. */ int k; double A1, A2, ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    Modify program so that it finds maximum crest & minimum trough for combined waves

    Code:
    #include <stdio.h>
    #include <math.h>
    #define PI 3.141593
    
    int main(void)
     {
       /*  Declare variables.  */
             int k;
             double A1, A2, freq1, freq2, height1, height2, length1, length2;
             double T1, T2, w1, w2, sum, new_period, new_height, time_incr, t;
             double maxwave=0;
    
       /*  Get user input from the keyboard.  */
             printf("Enter integer wave period (s) and wave height (ft) \n");
             fflush (stdout);
             printf("for wave 1: \n");
             fflush (stdout);
             scanf("%lf %lf",&T1,&height1);
             printf("Enter integer wave period (s) and wave height (ft) \n");
             fflush (stdout);
             printf("for wave 2: \n");
             fflush (stdout);
             scanf("%lf %lf",&T2,&height2);
    
       /*  Determine and print wavelengths.  */
            length1 = 5.13*T1*T1;
            length2 = 5.13*T2*T2;
            printf("Wavelengths (in ft) are: %.2f %.2f \n",length1,length2);
            fflush (stdout);
    
       /*  Determine period of combined waves.  */
            new_period = T1*T2;
    
       /*  Compute 200 points of the combined waves over the  */
       /*  period specified, and find the maximum height.     */
            time_incr = new_period/200;
            A1 = height1/2;
            A2 = height2/2;
            freq1 = 1/T1;
            freq2 = 1/T2;
       
        for (k=0; k<=199; k++)
       {
          t = k*time_incr;
          w1 = A1*sin(2*PI*freq1*t);
          w2 = A2*sin(2*PI*freq2*t);
          sum = w1 + w2;
          if (sum > maxwave)
             maxwave = sum;
       }
       new_height = maxwave*2;
    
       /*  Print new wave maximum.   */
            printf("Maximum combined wave height is %.2f ft \n",new_height);
            fflush (stdout);
    
       /*   Exit program.  */
       return 0;
    }
    /*------------------------------------------------------------*/

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,739
    So, what about this code?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    2
    i need to modifi this program so that i can get the maximun crest and minimum trough for the combined waves

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,739
    Quote Originally Posted by buzzlightyear
    i need to modifi this program so that i can get the maximun crest and minimum trough for the combined waves
    Sounds good. Free feel to ask for help if you run into a problem after doing some research and attempted implementation.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    If you want to combine the troughs with the crests you need to introduce a phase factor into your code because at the moment all you do is take two sine waves, calculate the values of each one and sum them, but the fact you are using two of the same wave automatically means the waves are in phase. If you made one a cosine wave then you would get a phase factor of pi/2.

    All you need to do is modify your implementation a bit. Try something like this:
    Code:
    w1 = A1*sin(2*PI*freq1*t + phase1);
    w2 = A2*sin(2*PI*freq2*t + phase2);
    The last few questions I've answered on this forum haven't had anything to do with programming but mathematics/physics errors in code. It looks like it could become a regular thing. lol. Good job I'm so good at physics

    Laserlight is right though. This isn't a programming question.

    Edit:

    When you take an input of the phase factor it might be worth modifying it so that it lies between 0 and 2*pi (or -pi and pi) because otherwise you might get errors from the sine approximation if the value is too large. The same goes for the other term in the sine function (i.e. 2*pi*freq*t).
    Last edited by Swarvy; 10-06-2010 at 01:36 PM.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    I'm sure there is a more direct algebraic way to find minimum and maximum of sum of sines other than going through samples at a fine resolution.

  7. #7
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Quote Originally Posted by nonoob View Post
    I'm sure there is a more direct algebraic way to find minimum and maximum of sum of sines other than going through samples at a fine resolution.
    You could calculate the derivatives and hard code expressions into the program if you wanted. Then get the program to solve the corresponding equation. Another alternative is to numerically calculate the derivatives (i.e. calculate (delta y)/(delta t) etc) and then take smaller increments in the range when the derivative changes sign. e.g. if (delta y)/(delta t) > 0 for t = 29, but (delta y)/(delta t) < 0 for t = 30, then take a smaller range of t between 29 and 30 and repeat. Then you might find it switches again at t = 29.7, then repeat and you might find it switches at t = 29.73. The more you repeat the more decimal places you can calculate, but I'm not going to help the OP with that unless the OP asks and makes an attempt first. lol

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Thanks, Swarvy. Yeah, derivatives... worst case.
    Maybe something can be done with this
    sin A + sin B = 2sin ((A+B)/2) cos ((A-B)/2)
    but it isn't any easier that way.

    I was hoping for some simpler function involving factorization - treating the sines as triangular waves for example, then iterating around the areas showing the most promise.

    Sorry, I guess the problem isn't as easy as I thought initially.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    2,577
    The maximum crest for combined waves is the peak of both waves added together assuming the two waves are not of the same frequency.

    When the maximum crest or min trough happens is harder to figure out.

    GCD of T1 and T2 should give how often it happens.

    Edit: The above is an EET Student viewpoint; should be correct for radio waves; water waves might work different.

    Tim S.
    Last edited by stahta01; 10-06-2010 at 03:41 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-08-2009, 08:26 PM
  2. maximum and minimum
    By aslak in forum C Programming
    Replies: 35
    Last Post: 12-14-2008, 02:54 PM
  3. Displaying Minimum and Maximum values from input
    By mgardnertech in forum C Programming
    Replies: 1
    Last Post: 06-29-2008, 08:47 PM
  4. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  5. Replies: 2
    Last Post: 10-31-2002, 06:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21