Advertisement
NUCLEARESOL

Before Grad-decent

Jul 9th, 2023 (edited)
898
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.34 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading;
  4. using System.Globalization;
  5.  
  6. class Program
  7. {
  8.  
  9.     static void Main()
  10.     {
  11.         int version = 1;
  12.         double gravitationalA = 9.81d;
  13.         double[] startPoint = { 400, 200 };
  14.         double[] endpoint = { 0, 0 };
  15.         Boolean calculateDistance = true;
  16.  
  17.         Console.WriteLine("Following Variables are used:");
  18.         Console.WriteLine("Gravitational Accel: " + gravitationalA + " m/s");
  19.         Console.WriteLine("Goal: " + endpoint[0] + "," + endpoint[1]);
  20.         Console.WriteLine("Start: " + startPoint[0] + "," + startPoint[1]);
  21.         Console.WriteLine("Calculate Distance" + calculateDistance);
  22.         Console.WriteLine("y/n");
  23.         string confirmation = Console.ReadLine();
  24.  
  25.         if (confirmation != "y")
  26.         {
  27.             Console.WriteLine("");
  28.             Console.WriteLine("Abort, Please change the variable");
  29.             Console.WriteLine("Gravitational Acceleration");
  30.             gravitationalA = double.Parse(Console.ReadLine(), CultureInfo.InvariantCulture.NumberFormat);
  31.             Console.WriteLine("Start Point X");
  32.             startPoint[0] = double.Parse(Console.ReadLine(), CultureInfo.InvariantCulture.NumberFormat);
  33.             Console.WriteLine("Start Point Y");
  34.             startPoint[1] = double.Parse(Console.ReadLine(), CultureInfo.InvariantCulture.NumberFormat);
  35.             Console.WriteLine("");
  36.             Console.WriteLine("Following Variables are used:");
  37.             Console.WriteLine("Gravitational Accel: " + gravitationalA + " m/s");
  38.             Console.WriteLine("Goal: " + endpoint[0] + "," + endpoint[1]);
  39.             Console.WriteLine("Start: " + startPoint[0] + "," + startPoint[1]);
  40.             Thread.Sleep(3000);
  41.         }
  42.  
  43.         Stopwatch stopwatch = new Stopwatch();
  44.         stopwatch.Start();
  45.         Console.WriteLine("");
  46.         Console.WriteLine("Starting calculation");
  47.         double x = startPoint[0];
  48.         double velX = 0f;
  49.         double timestep = 0.0001f;
  50.         double time = 0f;
  51.         double initialCollvar = 1;
  52.         double initialCollvarStep = 0.1f;
  53.         double zeroInAccuracy = 0.0001f;
  54.         double maxTime = double.PositiveInfinity;
  55.         double limit = 50 * (double)Math.Sqrt((2 * startPoint[1]) / gravitationalA);
  56.         string strg = "";
  57.         Console.WriteLine(limit);
  58.         Tuple<double, double> calculateAcceleration(double X, double collVar, double depvar1, double depvar2)
  59.         {
  60.             double localSlope = 2.0f * collVar * (X - depvar1);
  61.  
  62.             //return -(float)(Math.Cos(Math.Atan(1f / localSlope)) * Math.Sin(Math.Atan(1f / localSlope)) * gravitationalA);
  63.             return Tuple.Create(-(double)(Math.Cos(Math.Atan(1f / localSlope)) * Math.Sin(Math.Atan(1f / localSlope)) * gravitationalA), localSlope);
  64.         }
  65.  
  66.         Console.WriteLine(strg);
  67.         Console.WriteLine("");
  68.         strg = "";
  69.         static double CalculateTime(double initialCollvar)
  70.         {
  71.             double h = 0;
  72.             double k = 0;
  73.             double distance = 0;
  74.             double deltaX = 0;
  75.             return (double)(1 + 1);
  76.         }
  77.  
  78.         Tuple<double, double, double, double> jumpingDecend()
  79.         {
  80.             //   Console.WriteLine(Math.Abs(initialCollvarStep)+","+zeroInAccuracy);
  81.             double h = 0;
  82.             double k = 0;
  83.             double distance = 0;
  84.             double deltaX = 0;
  85.             while (Math.Abs(initialCollvarStep) >= zeroInAccuracy)
  86.             {
  87.                 // --  Console.Write("BOB");
  88.                 initialCollvar = initialCollvar + initialCollvarStep;
  89.                 x = startPoint[0];
  90.                 velX = 0f;
  91.                 time = 0f;
  92.                 distance = 0f;
  93.                 h = 0.5f * (startPoint[0] - (startPoint[1] / (startPoint[0] * initialCollvar)));
  94.                 k = -initialCollvar * (double)Math.Pow(h, 2);
  95.                 // Console.WriteLine(calculateAcceleration(x, initialCollvar, h, k) +"XX");
  96.                 while (x >= 0 && time < limit)
  97.                 {
  98.                     Tuple<double, double> calculatedVales = calculateAcceleration(x, initialCollvar, h, k);
  99.                     deltaX = (double)(velX * timestep + 0.5f * calculatedVales.Item1 * Math.Pow(timestep, 2));
  100.                     x = (double)(x + deltaX);
  101.                     velX = velX + (0 + calculateAcceleration(x, initialCollvar, h, k).Item1) * timestep / 1f;
  102.                     distance = distance + (double)Math.Sqrt(deltaX * deltaX * (calculatedVales.Item2 * calculatedVales.Item2 + 1.0f));
  103.                     time = time + timestep;
  104.                 }
  105.                 // Console.WriteLine(distance);
  106.                 Console.WriteLine(initialCollvar + "(x-" + h + ")^2+" + k);
  107.                 strg = strg + "(" + initialCollvar + "," + time + ")" + ",";
  108.                 if (maxTime > time)
  109.                 {
  110.                     maxTime = time;
  111.                     initialCollvarStep = initialCollvarStep * 2;
  112.                 }
  113.  
  114.                 else if (time > maxTime)
  115.                 {
  116.                     if (time >= 400)
  117.                     {
  118.                         initialCollvar = initialCollvar - initialCollvarStep;
  119.                         initialCollvarStep = -initialCollvarStep / 4;
  120.                     }
  121.                     else
  122.                     {
  123.                         initialCollvarStep = -initialCollvarStep;
  124.                     }
  125.                 }
  126.                 else
  127.                 {
  128.                     initialCollvarStep = initialCollvarStep / 2;
  129.                 }
  130.             }
  131.             Console.WriteLine("Finished");
  132.             return Tuple.Create(initialCollvar, time, h, k);
  133.         }
  134.         Tuple<double, double, double, double> resultTuple = jumpingDecend();
  135.         Console.WriteLine("");
  136.         Console.WriteLine("time taken " + resultTuple.Item2 + " seconds");
  137.         Console.WriteLine("At variable = " + resultTuple.Item1);
  138.         Console.WriteLine("h = " + resultTuple.Item3);
  139.         Console.WriteLine("k = " + resultTuple.Item4);
  140.         Console.WriteLine();
  141.         stopwatch.Stop();
  142.         Console.WriteLine("");
  143.         Console.WriteLine("Calculation Time is {0} s", stopwatch.ElapsedMilliseconds / 1000f);
  144.         Console.WriteLine("Closing In 10 seconds");
  145.         Console.WriteLine(strg);
  146.         Thread.Sleep(10000);
  147.     }
  148. }
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement