Advertisement
Guest User

mini project 4

a guest
Nov 26th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. /*Program for calucluating the displacement of a mass on a spring*/
  2. #include <iostream>
  3. #include <cmath>
  4. #include <math.h>
  5. #include <algorithm>
  6. #include <iomanip>
  7.  
  8. bool valid;
  9. using namespace std;
  10.  
  11. double X(double A, double V1, double Wd, double t)
  12. {
  13. return A * (exp(V1 * t)) * cos(Wd*t);
  14. }
  15.  
  16. double ScaledResult(double original, double Mn, double Mx)
  17. {
  18. return ((original - Mn)/(Mx - Mn))*(40-1)+1;
  19. }
  20. int main()
  21. {
  22. // declaring initial values
  23. double A, K, M, S; // declare variables
  24.  
  25. // allow user to input values
  26. cout << "Enter Value For Initial Displacement: ";
  27. cin >> A;
  28. cout << "Enter Value For Spring Constant: ";
  29. cin >> K;
  30. cout << "Enter Value For Mass: ";
  31. cin >> M;
  32.  
  33. do
  34. {
  35. valid = true;
  36. cout << "Please enter a dampening ratio: ";
  37. cin >> S;
  38. cin.ignore(100, '\n');
  39.  
  40. if (S <= 0 || S >= 1)
  41. {
  42. valid = false;
  43. cout << "Dampening ratio must be greater than 0 and less than 1. Try again." << endl;
  44. }
  45.  
  46. } while (!valid);
  47.  
  48. //initialise variables
  49. double Wn = sqrt(K / M);
  50. double Wd = Wn * sqrt(1 - pow(S, 2));
  51. double V1 = -S * Wn;
  52.  
  53. cout << "Therefore";
  54. cout << "\n";
  55. cout << "Wn = " << Wn;
  56. cout << "\n";
  57. cout << "Wd = " << Wd;
  58. cout << "\n";
  59.  
  60.  
  61. double value[100];
  62. double results[100];
  63.  
  64. for (size_t i = 0; i < 100; ++i)
  65. {
  66. double t = (i * 250) / 1000.0;
  67. results[i] = X(A, V1, Wd, t);
  68. std::cout << (results[i]) << "*\n";
  69. }
  70.  
  71. double Mn = *std::min_element(results, results + 100);
  72. double Mx = *std::max_element(results, results + 100);
  73. std::cout << " The maximum displacement is: " << Mx << endl;
  74. std::cout << " The minimum displacement is: " << Mn << endl;
  75.  
  76. for (size_t i = 0; i < 100; ++i)
  77. {
  78. std::cout << std::stew(ScaledResult(results[i], Mn, Mx)) << "*\n";
  79. }
  80.  
  81. system("Pause");
  82.  
  83. return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement