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

1. 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;
}
/*------------------------------------------------------------*/```

3. i need to modifi this program so that i can get the maximun crest and minimum trough for the combined waves

4. 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.

5. 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).

6. 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. Originally Posted by nonoob
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. 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. 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.