Skip to content

Instantly share code, notes, and snippets.

@biodunalfet
Created January 9, 2017 23:06
Show Gist options
  • Save biodunalfet/19d4047f6e664bb5012fcd84da283ba1 to your computer and use it in GitHub Desktop.
Save biodunalfet/19d4047f6e664bb5012fcd84da283ba1 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
import java.io.*;
import java.lang.Math;
public class quest3 {
static double Vm;
static double F;
static int N = 1000;
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
printLine("Enter Vm: ");
Vm = scanner.nextDouble();
printLine("Enter F: ");
F = scanner.nextDouble();
double upperLimit = Math.PI/2;
double lowerLimit = -Math.PI/2;
double interval = (upperLimit - lowerLimit)/N;
double integralSimps = Simpsons(upperLimit, lowerLimit, interval);
double integralTrapz = Trapezoidal(upperLimit, lowerLimit, interval);
printLine("Using Simpson's rule : " + integralSimps);
printLine("Using Trapezoidal rule : " + integralTrapz);
}
public static double Ft(double t){
return Vm * Math.sin((2 * Math.PI * F * t));
}
public static double Simpsons(double upperLimit, double lowerLimit, double interval){
int i = 0;
double sum = 0;
while (i < N){
i++;
double t = lowerLimit + (i * interval);
//even
if (i % 2 == 0){
sum += 2 * Ft(t);
}
//odd
else{
sum += 4 * Ft(t);
}
}
return (interval/3) * (sum + Ft(upperLimit) + Ft(lowerLimit));
}
public static double Trapezoidal(double upperLimit, double lowerLimit, double interval){
double sum = (Ft(upperLimit) + Ft(lowerLimit))/2;
int c = 1;
while (c < N){
double t = lowerLimit + (c * interval);
sum += Ft(t);
c++;
}
return interval * sum;
}
public static void print(String text){
System.out.print(text);
}
public static void printLine(String text){
System.out.println(text);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment