Tuesday, September 11, 2018

day 5 9/11/18

Wave interaction problems
#1 
Implement the pseudocode for your sea level program into c.
read the period and wave height for wave 1
read the period and wave height for wave 2
compute and print the wavelength for each wave 
set new period to the product of the wave periods 
set time increment to new period/200 
set wavemax to 0 
set time to 0 
set steps to 0 
while steps <= 199 
     set sum to wave 1 + wave 2 
     if sum > wavemax 
         set wavemax to sum 
     add 1 to steps 
print wavemax 
#include <stdio.h>
#include <math.h>
#define PI 3.141439
int main(void) {
double period1, height1, length1, wave1, period2, height2, length2, wave2, period3, sum, steps, wavemax = 0, time = 0, timei;
printf("Enter the period (in seconds) and wave height (in feet) for wave 1\n");
scanf("%lf",&period1);
scanf("%lf",&height1);
printf("Enter the period (in seconds) and wave height (in feet) for wave 2\n");
scanf("%lf",&period2);
scanf("%lf",&height2);
length1 = 5.13 * (period1 * period1);
length2 = 5.13 * (period2 * period2);
period3 = period1 * period2;
timei = period3/200;
while(steps <= 199){
wave1 = height1 * sin((1/period1)*PI*(timei * steps));
wave2 = height2 * sin((1/period2)*PI*(timei * steps));
sum = (wave1 + wave2);
if (sum > wavemax){
wavemax = sum;
}
steps++;
}
printf("the wavelength for the first wave is %5.2f feet\n", length1);
printf("the wavelength for the second wave is %5.2f feet\n", length2);
printf("maximum combined wave height is %5.2f feet", wavemax);
return 0;
}
#2
Modify the program so that it finds the maximum crest and the minimum trough for the
combined waves
#include <stdio.h>
#include <math.h>
#define PI 3.141439
int main(void) {
double period1, height1, length1, wave1, period2, height2, length2, wave2, period3, sum, steps, wavemax = 0, wavemin = 0, time = 0, timei;
printf("Enter the period (in seconds) and wave height (in feet) for wave 1\n");
scanf("%lf",&period1);
scanf("%lf",&height1);
printf("Enter the period (in seconds) and wave height (in feet) for wave 2\n");
scanf("%lf",&period2);
scanf("%lf",&height2);
length1 = 5.13 * (period1 * period1);
length2 = 5.13 * (period2 * period2);
period3 = period1 * period2;
timei = period3/200;
while(steps <= 199){
wave1 = height1 * sin((1/period1)*PI*(timei * steps));
wave2 = height2 * sin((1/period2)*PI*(timei * steps));
sum = (wave1 + wave2);
if (sum > wavemax){
wavemax = sum;
}
steps++;
}
while(steps <= 199){
wave1 = -height1 * sin((1/period1)*PI*(timei * steps));
wave2 = -height2 * sin((1/period2)*PI*(timei * steps));
sum = (wave1 + wave2);
if (sum < wavemin){
wavemin = sum;
}
steps++;
}
printf("the wavelength for the first wave is %5.2f feet\n", length1);
printf("the wavelength for the second wave is %5.2f feet\n", length2);
printf("maximum combined wave height is %5.2f feet\n", wavemax);
printf("minimum combined wave height is %5.2f feet", wavemin);
return 0;
}
#3
Modify the program so that it also allows the user to enter a time, then the program computes and prints the combined wave height at that time.
#include <stdio.h>
#include <math.h>
#define PI 3.141439
int main(void) {
double period1, height1, length1, wave1, period2, height2, length2, wave2, period3, sum, time, timei;
printf("Enter the period (in seconds) and wave height (in feet) for wave 1\n");
scanf("%lf",&period1);
scanf("%lf",&height1);
printf("Enter the period (in seconds) and wave height (in feet) for wave 2\n");
scanf("%lf",&period2);
scanf("%lf",&height2);
printf("Enter a time (in seconds) to calculate a combined wave height\n");
scanf("%lf",&time);
length1 = 5.13 * (period1 * period1);
length2 = 5.13 * (period2 * period2);
period3 = period1 * period2;
timei = period3/200;
wave1 = height1 * sin((1/period1)*PI*(time));
wave2 = height2 * sin((1/period2)*PI*(time));
sum = (wave1 + wave2);
printf("the wavelength for the first wave is %5.2f feet\n", length1);
printf("the wavelength for the second wave is %5.2f feet\n", length2);
printf("the combined wave height is %5.2f feet at time: %5.2f seconds", sum, time);
return 0;
}
#4
Modify the program so that it allows the user to specify the number of points to compute for determining the maximum combined wave height over the specified period of time. Should this make any difference in the answer? Explain.
ANSWER:
If you increase the number of points beyond 200 there is no difference however, if you decrease
the number of points below 200 the number decreases.
#include <stdio.h>
#include <math.h>
#define PI 3.141439
int main(void) {
double period1, height1, length1, wave1, period2, height2, length2, wave2, period3, sum, steps, wavemax = 0, time, timei;
printf("Enter the period (in seconds) and wave height (in feet) for wave 1\n");
scanf("%lf",&period1);
scanf("%lf",&height1);
printf("Enter the period (in seconds) and wave height (in feet) for wave 2\n");
scanf("%lf",&period2);
scanf("%lf",&height2);
printf("specify the number of points to calculate IE'200'\n");
scanf("%lf",&time);
length1 = 5.13 * (period1 * period1);
length2 = 5.13 * (period2 * period2);
period3 = period1 * period2;
timei = period3/time;
while(steps <= time-1){
wave1 = height1 * sin((1/period1)*PI*(timei * steps));
wave2 = height2 * sin((1/period2)*PI*(timei * steps));
sum = (wave1 + wave2);
if (sum > wavemax){
wavemax = sum;
}
steps++;
}
printf("the wavelength for the first wave is %5.2f feet\n", length1);
printf("the wavelength for the second wave is %5.2f feet\n", length2);
printf("maximum combined wave height is %5.2f feet", wavemax);
return 0;
}
#5
Modify the program so that it allows the user to specify a phase shift between the two waves. This phase shift should be used as the phase shift for the second wave; the phase shift for the first wave should still be zero. Experiment with different phase shifts to see if they change the maximum possible wave height.
#include <stdio.h>
#include <math.h>
#define PI 3.141439
int main(void) {
double period1, height1, length1, wave1, period2, height2, length2, wave2, period3, sum, steps, wavemax = 0, time = 0, timei, phaseshift;
printf("Enter the period (in seconds) and wave height (in feet) for wave 1\n");
scanf("%lf",&period1);
scanf("%lf",&height1);
printf("Enter the period (in seconds) and wave height (in feet) for wave 2\n");
scanf("%lf",&period2);
scanf("%lf",&height2);
printf("Enter the phaseshift between the two waves\n");
scanf("%lf",&phaseshift);
length1 = 5.13 * (period1 * period1);
length2 = 5.13 * (period2 * period2);
period3 = period1 * period2;
timei = period3/200;
while(steps <= 199){
wave1 = height1 * sin((1/period1)*PI*(timei * steps));
wave2 = height2 * sin(((1/period2)*PI*(timei * steps))+phaseshift);
sum = (wave1 + wave2);
if (sum > wavemax){
wavemax = sum;
}
steps++;
}
printf("the wavelength for the first wave is %5.2f feet\n", length1);
printf("the wavelength for the second wave is %5.2f feet\n", length2);
printf("maximum combined wave height is %5.2f feet", wavemax);
return 0;
}
#6
Modify the program so that it uses units of meters and seconds, instead of feet and seconds. Remember to modify the program that computes the wavelengths appropriately.
#include <stdio.h>
#include <math.h>
#define PI 3.141439
int main(void) {
double period1, height1, length1, wave1, period2, height2, length2, wave2, period3, sum, steps, wavemax = 0, time = 0, timei;
printf("Enter the period (in seconds) and wave height (in meters) for wave 1\n");
scanf("%lf",&period1);
scanf("%lf",&height1);
printf("Enter the period (in seconds) and wave height (in meters) for wave 2\n");
scanf("%lf",&period2);
scanf("%lf",&height2);
height1 = height1 * 3.28;
height2 = height2 * 3.28;
length1 = 5.13 * (period1 * period1);
length2 = 5.13 * (period2 * period2);
period3 = period1 * period2;
timei = period3/200;
while(steps <= 199){
wave1 = height1 * sin((1/period1)*PI*(timei * steps));
wave2 = height2 * sin((1/period2)*PI*(timei * steps));
sum = (wave1 + wave2);
if (sum > wavemax){
wavemax = sum;
}
steps++;
}
length1 = length1/3.28;
length2 = length2/3.28;
wavemax = wavemax/3.28;
printf("the wavelength for the first wave is %5.2f meters\n", length1);
printf("the wavelength for the second wave is %5.2f meters\n", length2);
printf("maximum combined wave height is %5.2f meters", wavemax);
return 0;
}

Arduino temperature sensor and RGB bulb
#1
the base program here just illustrates how the temperature sensor works
const int POT=A0; int val =0; void setup() { Serial.begin(9600); } void loop() { val = analogRead(POT); Serial.println(val); delay(500); }
#2
Write the temperature alert sketch that the causes the LED to light blue when the temperature is cold (ice will be provided), red when the temperature is hot (from sqeezing with your fingers) and green when the temperature is just right.
const int POT=A0;
const int BLED=11;
const int GLED=10;
const int RLED=9;
double celsius=22;
void setup() {
  pinMode(BLED, OUTPUT);
  pinMode(GLED, OUTPUT);
  pinMode(RLED, OUTPUT);
  Serial.begin(9600);}
void loop() {
  int Val = analogRead(POT);
  double Vout = (Val/1023.)*5000.;
  double Celsius = (Vout-500)/10.;
  if(Celsius >=celsius+1.5){
    digitalWrite(RLED, HIGH);
    digitalWrite(BLED, LOW);
    digitalWrite(GLED, LOW);
  }
  else if(Celsius <=celsius-2.5){
    digitalWrite(RLED, LOW);
    digitalWrite(BLED, HIGH);
    digitalWrite(GLED, LOW);
  }
  else{
    digitalWrite(RLED, LOW);
    digitalWrite(BLED, LOW);
    digitalWrite(GLED, HIGH);
  }
  Serial.println(Celsius);
  delay(500);
  }
 

No comments:

Post a Comment