Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.62 KB | None | 0 0
  1. using System;
  2.  
  3. namespace NeetaAssignment
  4. {
  5.     class FunctionCalculator
  6.     {
  7.         private double Lower;
  8.         private double Upper;
  9.         private int Steps;
  10.  
  11.         // Constructor Overriding
  12.  
  13.         public FunctionCalculator()
  14.         {
  15.         }
  16.  
  17.         public FunctionCalculator(double LowerLimit, double UpperLimit, int steps)
  18.         {
  19.             this.Lower = LowerLimit;
  20.             this.Upper = UpperLimit;
  21.             this.Steps = steps;
  22.         }
  23.  
  24.         // Get and Set method for lower limit,upper limit and steps
  25.  
  26.         // get and return method for lower limit,upper limit,and steps
  27.         public double getLowerLimit()
  28.         {
  29.             return this.Lower;
  30.         }
  31.  
  32.         public double getUpperLimit()
  33.         {
  34.             return this.Upper;
  35.         }
  36.         public double getsteps()
  37.         {
  38.             return this.Steps;
  39.         }
  40.  
  41.         // Set method for lower limit,upper limit,and steps
  42.  
  43.         public void setLowerLimit(double a)
  44.         {
  45.             this.Lower = a;
  46.         }
  47.  
  48.         public void setUpperLimit(double b)
  49.         {
  50.             this.Upper = b;
  51.         }
  52.  
  53.         public void setsteps(int c)
  54.         {
  55.             this.Steps = c;
  56.         }
  57.  
  58.  
  59.         //Now write method for given function which will return value
  60.  
  61.         public virtual double Funct(double X)
  62.         {
  63.             double function;
  64.             function = 2 / (1 + 4 * X * X);
  65.             return function;
  66.         }
  67.  
  68.        
  69.         double GetStepsize ()
  70.         {
  71.             double Stepssize =  ((Upper - Lower) / Steps);
  72.             return Stepssize;
  73.         }
  74.        
  75.  
  76.         //one more method for integration as Simpson Calculation
  77.  
  78.         public double CalculateSimpson()
  79.         {
  80.            
  81.             double Stepssize = GetStepsize ();
  82.             double Simpson = 0;
  83.             for (double i = Lower; i <= Upper; i = i + Stepssize)
  84.             {
  85.                
  86.                 double a = i;
  87.                 double b = i + Stepssize;
  88.                 Simpson = Simpson + ((b - a) / 6) * (Funct(a) + 4 * Funct((a + b) / 2) + Funct(b));
  89.  
  90.              
  91.             }
  92.            
  93.             return Simpson;
  94.         }
  95.  
  96.  
  97.         public double CalculateMidpointRule()
  98.         {
  99.             double Stepssize = ((Upper - Lower) / Steps);
  100.             double MidPoint = 0;
  101.             for (double i = Lower; i <= Upper; i = i + Stepssize)
  102.             {
  103.  
  104.                 double a = i;
  105.                 double b = i + Stepssize;
  106.                 MidPoint = MidPoint + ((b-a) *  Funct((a + b) / 2));
  107.             }
  108.             return MidPoint;
  109.  
  110.         }
  111.         public double CalculateTrapeziumRule()
  112.         {
  113.             double Stepssize = ((Upper - Lower) / Steps);
  114.             double Trapezium = 0;
  115.             for (double i = Lower; i <= Upper; i = i + Stepssize)
  116.             {
  117.  
  118.                 double a = i;
  119.                 double b = i + Stepssize;
  120.                 Trapezium = Trapezium + ((b - a) / 2)*(Funct(a) + Funct(b));
  121.             }
  122.             return Trapezium;
  123.  
  124.         }
  125.        
  126.  
  127.         public void GetResult()
  128.         {
  129.             Console.WriteLine(" \n Lower Limit={0}\n upperlimit={1} \n Steps={2} \n ", Lower, Upper, Steps);
  130.         }
  131.  
  132.     }
  133.    
  134.     class Class2 : FunctionCalculator
  135.     {
  136.         public override double Funct(double X)
  137.         {
  138.             double function;
  139.             function = Math.Sin(X);
  140.             return function;
  141.         }
  142.     }
  143.    
  144.     class MainClass
  145.     {
  146.         public static void Main (string[] args)
  147.         {
  148.             FunctionCalculator c = new FunctionCalculator();
  149.             Console.WriteLine(" Enter Lower Limit");
  150.             c.setlowerlimit(Convert.ToDouble(Console.ReadLine()));
  151.             Console.WriteLine(" Enter Upper Limit");
  152.             c.setupperlimit(Convert.ToDouble(Console.ReadLine()));
  153.             Console.WriteLine(" Enter Steps");
  154.             c.setsteps(Convert.ToDouble(Console.ReadLine()));
  155.             double Simpson = c.CalculateSimpson();
  156.             c.GetResult();
  157.             Console.WriteLine("Simpson=" + Simpson);
  158.            
  159.             /*
  160.             Class2 C1 = new Class2();
  161.             Console.WriteLine(" Enter Lower Limit");
  162.             C1.setlowerlimit(Convert.ToInt32(Console.ReadLine()));
  163.             Console.WriteLine(" Enter Upper Limit");
  164.             C1.setupperlimit(Convert.ToInt32(Console.ReadLine()));
  165.             Console.WriteLine(" Enter Steps");
  166.             C1.setsteps(Convert.ToInt32(Console.ReadLine()));
  167.             double Simpson1 = C1.CalculateSimpson();
  168.             C1.GetResult();
  169.             Console.WriteLine("Simpson 1=" + Simpson1);
  170.             */
  171.         }
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement