Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Threading;
- using System.Globalization;
- class Program
- {
- static void Main()
- {
- int version = 1;
- string type = "circle";
- double gravitationalA = 9.81d;
- double[] startPoint = { 5,10};
- double[] endpoint = { 0, 0 };
- const Boolean calculateDistance = false;
- Console.WriteLine("Following Variables are used:");
- Console.WriteLine("Gravitational Accel: " + gravitationalA + " m/s");
- Console.WriteLine("Goal: " + endpoint[0] + "," + endpoint[1]);
- Console.WriteLine("Start: " + startPoint[0] + "," + startPoint[1]);
- Console.WriteLine("Calculate Distance" + calculateDistance);
- Console.WriteLine("y/n");
- string confirmation = Console.ReadLine();
- if (confirmation != "y")
- {
- Console.WriteLine("");
- Console.WriteLine("Abort, Please change the variable");
- Console.WriteLine("graph type");
- type = Console.ReadLine();
- Console.WriteLine("Gravitational Acceleration");
- gravitationalA = double.Parse(Console.ReadLine(), CultureInfo.InvariantCulture.NumberFormat);
- Console.WriteLine("Start Point X");
- startPoint[0] = double.Parse(Console.ReadLine(), CultureInfo.InvariantCulture.NumberFormat);
- Console.WriteLine("Start Point Y");
- startPoint[1] = double.Parse(Console.ReadLine(), CultureInfo.InvariantCulture.NumberFormat);
- Console.WriteLine("");
- Console.WriteLine("Following Variables are used:");
- Console.WriteLine("Gravitational Accel: " + gravitationalA + " m/s");
- Console.WriteLine("Goal: " + endpoint[0] + "," + endpoint[1]);
- Console.WriteLine("Start: " + startPoint[0] + "," + startPoint[1]);
- //Thread.Sleep(3000);
- }
- // Console.WriteLine("");
- // Console.WriteLine("Starting calculation");
- double x = startPoint[0];
- double velX = 0f;
- const double timestep = 0.0001f;
- double time = 0f;
- double initialCollvar = 1;
- double initialCollvarStep = 0.1f;
- double zeroInAccuracy = 0.0001f;
- double maxTime = double.PositiveInfinity;
- double limit = 400;
- string strg = "";
- Console.WriteLine(limit);
- var start = Process.GetCurrentProcess().TotalProcessorTime;
- int i = 0;
- static double NewtonsMethod(double target, double initialGuess, double tolerance, int maxIterations)
- {
- double x = initialGuess;
- for (int i = 0; i < maxIterations; i++)
- {
- double fx = (2 * x - Math.Sin(2 * x)) / (1 - Math.Cos(2 * x));
- double derivative = -2 * ((x / Math.Tan(x)) - 1) * Math.Pow(1 / Math.Sin(x), 2);
- Console.WriteLine(x + " " + fx + " " + derivative);
- //Thread.Sleep(1000);
- if (Math.Abs(fx - target) < tolerance)
- {
- Console.WriteLine($"Converged after {i} iterations.");
- return x;
- }
- x = x - (fx - target) / derivative;
- }
- Console.WriteLine("Did not converge within the specified number of iterations.");
- return x;
- }
- Tuple<double, double> calculateAcceleration(double X, double collVar, double depvar1, double depvar2)
- {
- double localSlope = 2.0f * collVar * (X - depvar1);
- //Console.WriteLine(-(float)(Math.Cos(Math.Atan(1f / localSlope)) * Math.Sin(Math.Atan(1f / localSlope)) * gravitationalA));
- return Tuple.Create(-(double)(Math.Cos(Math.Atan(1f / localSlope)) * Math.Sin(Math.Atan(1f / localSlope)) * gravitationalA), localSlope);
- }
- Tuple<double, double> calculateAccelerationCircle(double X, double Y, double collVar, double h, double k)
- {
- double localSlope = (h - X) / (Y - k);
- //return -(float)(Math.Cos(Math.Atan(1f / localSlope)) * Math.Sin(Math.Atan(1f / localSlope)) * gravitationalA);
- return Tuple.Create(-(double)(Math.Cos(Math.Atan(1f / localSlope)) * Math.Sin(Math.Atan(1f / localSlope)) * gravitationalA), localSlope);
- }
- Tuple<double, double> calculateAccelerationCubic(double X, double collVar, double h, double k)
- {
- double localSlope = collVar*3.0d*Math.Pow(X-h,2.0);
- //return -(float)(Math.Cos(Math.Atan(1f / localSlope)) * Math.Sin(Math.Atan(1f / localSlope)) * gravitationalA);
- return Tuple.Create(-(double)(Math.Cos(Math.Atan(1f / localSlope)) * Math.Sin(Math.Atan(1f / localSlope)) * gravitationalA), localSlope);
- }
- Console.WriteLine(strg);
- Console.WriteLine("");
- strg = "";
- Tuple<double, double, double, double> CalculateTimeParabola(double initialCollvar)
- {
- double h = 0;
- double k = 0;
- double distance = 0;
- double deltaX = 0;
- i = i + 1;
- x = startPoint[0];
- velX = 0f;
- time = 0f;
- distance = 0f;
- h = 0.5f * (startPoint[0] - (startPoint[1] / (startPoint[0] * initialCollvar)));
- k = -initialCollvar * (double)Math.Pow(h, 2);
- while (x >= 0 && time < limit)
- {
- Tuple<double, double> calculatedVales = calculateAcceleration(x, initialCollvar, h, k);
- deltaX = (double)(velX * timestep + 0.5f * calculatedVales.Item1 * Math.Pow(timestep, 2));
- x = (double)(x + deltaX);
- velX = velX + (0 + calculateAcceleration(x, initialCollvar, h, k).Item1) * timestep / 1f;
- if (calculateDistance)
- {
- distance = distance + (double)Math.Sqrt(deltaX * deltaX * (calculatedVales.Item2 * calculatedVales.Item2 + 1.0f));
- }
- time = time + timestep;
- }
- Console.WriteLine(Math.Abs(Math.Abs(initialCollvarStep) * 100 - 100) + " % complete");
- return Tuple.Create(time, initialCollvar, h, k);
- }
- Tuple<double, double, double, double> CalculateTimeCircle(double initialCollvar)
- {
- if (initialCollvar < (double)Math.Sqrt(Math.Pow(startPoint[1], 2) + Math.Pow((Math.Pow(startPoint[1], 2) - Math.Pow(startPoint[0], 2)) / (-2 * startPoint[0]), 2)))
- {
- return Tuple.Create(limit, initialCollvar, 0.0d, 0.0d);
- }
- double q = Math.Sqrt(Math.Pow(startPoint[1], 2) + Math.Pow(startPoint[0], 2));
- double x3 = startPoint[0] / 2;
- double y3 = startPoint[1] / 2;
- double h = x3 + (Math.Sqrt(Math.Pow(initialCollvar, 2) - Math.Pow(q / 2, 2)) * (-startPoint[1])) / q;
- double k = y3 + (Math.Sqrt(Math.Pow(initialCollvar, 2) - Math.Pow(q / 2, 2)) * (startPoint[0])) / q;
- double distance = 0;
- double deltaX = 0;
- x = startPoint[0];
- velX = 0f;
- time = 0f;
- distance = 0f;
- double y = k - Math.Sqrt(Math.Pow(initialCollvar, 2) - Math.Pow(x - h, 2));
- while (x >= 0 && time < limit)
- {
- y = k - Math.Sqrt(Math.Pow(initialCollvar, 2) - Math.Pow(x - h, 2));
- Tuple<double, double> calculatedVales = calculateAccelerationCircle(x, y, initialCollvar, h, k);
- deltaX = (double)(velX * timestep + 0.5f * calculatedVales.Item1 * Math.Pow(timestep, 2));
- x = (double)(x + deltaX);
- velX = velX + (0 + calculateAccelerationCircle(x, y, initialCollvar, h, k).Item1) * timestep / 1f;
- if (calculateDistance)
- {
- distance = distance + (double)Math.Sqrt(deltaX * deltaX * (calculatedVales.Item2 * calculatedVales.Item2 + 1.0f));
- }
- time = time + timestep;
- }
- return Tuple.Create(time, initialCollvar, h, k);
- }
- Tuple<double, double, double, double> CalculateTimeCubic(double initialCollvar)
- {
- double h = 0;
- double k = 0;
- double distance = 0;
- double deltaX = 0;
- i = i + 1;
- x = startPoint[0];
- velX = 0f;
- time = 0f;
- distance = 0f;
- if (initialCollvar <= 0.00001 |initialCollvar >= 12.0 * startPoint[1] / (3.0 * Math.Pow(startPoint[0], 3)))
- {
- return Tuple.Create(limit+100, initialCollvar, h, k);
- }
- h = (3.0d * Math.Pow(startPoint[0], 2) - Math.Sqrt(9.0d * Math.Pow(startPoint[0], 4) - 4.0 * 3.0 * startPoint[0] * (Math.Pow(startPoint[0], 3) - startPoint[1] / initialCollvar)))/2;
- k = initialCollvar * (double)Math.Pow(h, 3);
- while (x >= 0 && time < limit)
- {
- Tuple<double, double> calculatedVales = calculateAccelerationCubic(x, initialCollvar, h, k);
- deltaX = (double)(velX * timestep + 0.5f * calculatedVales.Item1 * Math.Pow(timestep, 2));
- x = (double)(x + deltaX);
- velX = velX + (0 + calculateAccelerationCubic(x, initialCollvar, h, k).Item1) * timestep / 1f;
- if (calculateDistance)
- {
- distance = distance + (double)Math.Sqrt(deltaX * deltaX * (calculatedVales.Item2 * calculatedVales.Item2 + 1.0f));
- }
- time = time + timestep;
- }
- Console.WriteLine(Math.Abs(Math.Abs(initialCollvarStep) * 100 - 100) + " % complete");
- return Tuple.Create(time, initialCollvar, h, k);
- }
- Tuple<double, double, double, double, double, double> jumpingDecend()
- {
- // Console.WriteLine(Math.Abs(initialCollvarStep)+","+zeroInAccuracy);
- double var1 = 0;
- double var2 = 0;
- double var3 = 0;
- double var4 = 0;
- if (type == "parabola")
- {
- var1 = 0;
- var2 = 0;
- }
- if (type == "circle")
- {
- initialCollvar = (double)Math.Sqrt(Math.Pow(startPoint[1], 2) + Math.Pow((Math.Pow(startPoint[1], 2) - Math.Pow(startPoint[0], 2)) / (-2 * startPoint[0]), 2));
- // Console.WriteLine(initialCollvar+"RADIUS");
- }
- if (type == "cubic")
- {
- initialCollvar = (12.0 * startPoint[1] / (3.0 * Math.Pow(startPoint[0], 3)))/2;
- initialCollvarStep = (12.0 * startPoint[1] / (3.0 * Math.Pow(startPoint[0], 3))) / 10;
- zeroInAccuracy = (12.0 * startPoint[1] / (3.0 * Math.Pow(startPoint[0], 3))) / 100000;
- Console.WriteLine(initialCollvar+"RADIUS");
- }
- while (Math.Abs(initialCollvarStep) >= zeroInAccuracy)
- {
- // -- Console.Write("BOB");
- initialCollvar = initialCollvar + initialCollvarStep;
- if (type == "parabola")
- {
- time = CalculateTimeParabola(initialCollvar).Item1;
- }
- else if (type == "circle")
- {
- time = CalculateTimeCircle(initialCollvar).Item1;
- }
- else if (type == "cubic")
- {
- time = CalculateTimeCubic(initialCollvar).Item1;
- }
- // Console.WriteLine(distance);
- // Console.WriteLine(initialCollvar + "(x-" + h + ")^2+" + k);
- strg = strg + "(" + initialCollvar + "," + time + ")" + ",";
- if (maxTime > time)
- {
- maxTime = time;
- initialCollvarStep = initialCollvarStep * 2;
- }
- else if (time > maxTime)
- {
- if (time >= limit)
- {
- initialCollvar = initialCollvar - initialCollvarStep;
- initialCollvarStep = -initialCollvarStep / 4;
- }
- else
- {
- initialCollvarStep = -initialCollvarStep;
- }
- }
- else
- {
- initialCollvarStep = initialCollvarStep / 2;
- }
- }
- if (type == "circle")
- {
- Tuple<double, double, double, double> resultcal = CalculateTimeCircle(initialCollvar);
- time = CalculateTimeCircle(initialCollvar).Item1;
- var1 = resultcal.Item1;
- var2 = resultcal.Item3;
- var3 = resultcal.Item4;
- }
- else if (type == "parabola")
- {
- Tuple<double, double, double, double> resultcal = CalculateTimeParabola(initialCollvar);
- time = CalculateTimeParabola(initialCollvar).Item1;
- var1 = resultcal.Item2;
- var2 = resultcal.Item3;
- var3 = resultcal.Item4;
- }
- else if (type == "cubic")
- {
- Tuple<double, double, double, double> resultcal = CalculateTimeCubic(initialCollvar);
- time = CalculateTimeCubic(initialCollvar).Item1;
- var1 = resultcal.Item2;
- var2 = resultcal.Item3;
- var3 = resultcal.Item4;
- }
- if (time > limit)
- {
- if (type == "circle")
- {
- initialCollvar = initialCollvar - initialCollvarStep / 2;
- Tuple<double, double, double, double> resultcal = CalculateTimeCircle(initialCollvar);
- time = resultcal.Item1;
- var1 = resultcal.Item2;
- var2 = resultcal.Item3;
- var3 = resultcal.Item4;
- }
- else if (type == "parabola")
- {
- initialCollvar = initialCollvar - initialCollvarStep / 2;
- Tuple<double, double, double, double> resultcal = CalculateTimeParabola(initialCollvar);
- time = resultcal.Item1;
- var1 = resultcal.Item2;
- var2 = resultcal.Item3;
- var3 = resultcal.Item4;
- }
- else if (type == "cubic")
- {
- initialCollvar = initialCollvar - initialCollvarStep / 2;
- Tuple<double, double, double, double> resultcal = CalculateTimeCubic(initialCollvar);
- time = resultcal.Item1;
- var1 = resultcal.Item2;
- var2 = resultcal.Item3;
- var3 = resultcal.Item4;
- }
- }
- Console.WriteLine("Finished");
- return Tuple.Create(initialCollvar, time, var1, var2, var3, var4);
- }
- Tuple<double, double, double, double, double, double> resultTuple = jumpingDecend();
- var stop = Process.GetCurrentProcess().TotalProcessorTime;
- Console.WriteLine("");
- Console.WriteLine("time taken " + resultTuple.Item2 + " seconds");
- Console.WriteLine("At variable = " + resultTuple.Item1);
- Console.WriteLine("h = " + resultTuple.Item4);
- Console.WriteLine("k = " + resultTuple.Item5);
- Console.WriteLine();
- // stopwatch.Stop();
- Console.WriteLine("");
- Console.WriteLine("Calculation Time is " + (stop - start).TotalMilliseconds);
- Console.WriteLine("Cal amt" + i);
- // Console.WriteLine("Starting control group in 5 seconds");
- Console.WriteLine(strg);
- Thread.Sleep(20000);
- start = Process.GetCurrentProcess().TotalProcessorTime;
- double theta = NewtonsMethod(startPoint[0] / startPoint[1],1,1e-20,2000);
- double D = 2 * startPoint[1] / (1 - Math.Cos(theta * 2));
- Console.WriteLine(D);
- double t = Math.Sqrt(2*D/gravitationalA)*theta;
- Console.WriteLine(t);
- stop = Process.GetCurrentProcess().TotalProcessorTime;
- Console.WriteLine("Calculation Time is " + (stop - start).TotalMilliseconds);
- strg = "";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement