Advertisement
ivandrofly

CSMath_Exp_Quadratic

May 16th, 2015
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace SoftwareAndFinance
  6. {
  7.     class Math
  8.     {
  9.         const double PI = 3.14159265;
  10.         const double EulersNumber = 2.71828;
  11.  
  12.         // exp(x) series = 1 + x + x^2 / 2! + x^3 / 3! + x^4 / 4!
  13.         static public double MyExp1(double x)
  14.         {
  15.             double f = x;
  16.             double result = 1 + x;
  17.             double fact = 1;
  18.             int i = 0;
  19.             for (i = 2; i < 20; i++)
  20.             {
  21.                 fact *= i;
  22.                 f *= x;
  23.                 result += f / fact;
  24.             }
  25.             return result;
  26.         }
  27.  
  28.         // exp(x) series = power(2.71828, x)
  29.         public static double MyExp2(double x)
  30.         {
  31.             return System.Math.Pow(EulersNumber, x);
  32.         }
  33.  
  34.         // 6x^2 + 11x - 35 = 0 | SolveQuadratic(6, 11, -35);
  35.         // quadratic equation is a second order of polynomial equation in a single variable  
  36.         // x = [ -b +/- sqrt(b^2 - 4ac) ] / 2a
  37.         public static void SolveQuadratic(double a, double b, double c)
  38.         {
  39.             double sqrtpart = b * b - 4 * a * c;
  40.             double x, x1, x2, img;
  41.             if (sqrtpart > 0)
  42.             {
  43.                 x1 = (-b + System.Math.Sqrt(sqrtpart)) / (2 * a);
  44.                 x2 = (-b - System.Math.Sqrt(sqrtpart)) / (2 * a);
  45.                 Console.WriteLine("Two Real Solutions: {0,8:f4} or  {1,8:f4}", x1, x2);
  46.             }
  47.             else if (sqrtpart < 0)
  48.             {
  49.                 sqrtpart = -sqrtpart;
  50.                 x = -b / (2 * a);
  51.                 img = System.Math.Sqrt(sqrtpart) / (2 * a);
  52.                 Console.WriteLine("Two Imaginary Solutions: {0,8:f4} + {1,8:f4} i or {2,8:f4} + {3,8:f4} i", x, img, x, img);
  53.             }
  54.             else
  55.             {
  56.                 x = (-b + System.Math.Sqrt(sqrtpart)) / (2 * a);
  57.                 Console.WriteLine("One Real Solution: {0,8:f4}", x);
  58.             }
  59.         }
  60.  
  61.         static void Main()
  62.         {
  63.             for (double i = -2; i <= 3; i += 0.2)
  64.             {
  65.                 Console.WriteLine("{0,8:f2} = {1,8:f4}  {2,8:f4}  {3,8:f4}", i, System.Math.Exp(i), MyExp1(i), MyExp2(i));
  66.             }
  67.  
  68.             Console.WriteLine();
  69.             Console.WriteLine();
  70.  
  71.  
  72.             // 6x^2 + 11x - 35 = 0
  73.             SolveQuadratic(6, 11, -35);
  74.  
  75.             // 5x^2 + 6x + 1 = 0
  76.             SolveQuadratic(5, 6, 1);
  77.  
  78.             // 2x^2 + 4x + 2 = 0
  79.             SolveQuadratic(2, 4, 2);
  80.  
  81.             // 5x^2 + 2x + 1 = 0
  82.             SolveQuadratic(5, 2, 1);
  83.  
  84.             Console.WriteLine();
  85.             Console.WriteLine();
  86.             Console.ReadLine();
  87.         }
  88.     }
  89. }
  90. // Note: found on the Internet
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement