Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Program for calucluating the displacement of a mass on a spring*/
- #include <iostream>
- #include <cmath>
- #include <math.h>
- #include <algorithm>
- #include <iomanip>
- bool valid;
- using namespace std;
- double X(double A, double V1, double Wd, double t)
- {
- return A * (exp(V1 * t)) * cos(Wd*t);
- }
- double ScaledResult(double original, double Mn, double Mx)
- {
- return ((original - Mn)/(Mx - Mn))*(40-1)+1;
- }
- int main()
- {
- // declaring initial values
- double A, K, M, S; // declare variables
- // allow user to input values
- cout << "Enter Value For Initial Displacement: ";
- cin >> A;
- cout << "Enter Value For Spring Constant: ";
- cin >> K;
- cout << "Enter Value For Mass: ";
- cin >> M;
- do
- {
- valid = true;
- cout << "Please enter a dampening ratio: ";
- cin >> S;
- cin.ignore(100, '\n');
- if (S <= 0 || S >= 1)
- {
- valid = false;
- cout << "Dampening ratio must be greater than 0 and less than 1. Try again." << endl;
- }
- } while (!valid);
- //initialise variables
- double Wn = sqrt(K / M);
- double Wd = Wn * sqrt(1 - pow(S, 2));
- double V1 = -S * Wn;
- cout << "Therefore";
- cout << "\n";
- cout << "Wn = " << Wn;
- cout << "\n";
- cout << "Wd = " << Wd;
- cout << "\n";
- double value[100];
- double results[100];
- for (size_t i = 0; i < 100; ++i)
- {
- double t = (i * 250) / 1000.0;
- results[i] = X(A, V1, Wd, t);
- std::cout << (results[i]) << "*\n";
- }
- double Mn = *std::min_element(results, results + 100);
- double Mx = *std::max_element(results, results + 100);
- std::cout << " The maximum displacement is: " << Mx << endl;
- std::cout << " The minimum displacement is: " << Mn << endl;
- for (size_t i = 0; i < 100; ++i)
- {
- std::cout << std::stew(ScaledResult(results[i], Mn, Mx)) << "*\n";
- }
- system("Pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement