Math Proj
Math Proj
import java.lang.Math;
import java.util.Scanner;
static double x ;
if(func == 1) {
System.out.println("Please choose a function");
System.out.print("1"); System.out.println(": cos");
System.out.print("2"); System.out.println(": sin");
Scanner sc1 = new Scanner(System.in);
double func1 = sc1.nextDouble();
if(func1 == 1) {
System.out.println(Math.cos(x));
}
else if(func1 == 2) {
System.out.println(Math.sin(x));
}
else if(func == 2) {
double h = b - a;
double answ = (h/2)* (Math.exp(a) + Math.exp(b));
System.out.println("trap rule : "+answ);
double h_n = (b - a) / n;
double f_answ = h_n * (Math.exp(a) + Math.exp(b));
for (int i = 1; i < n; i++) {
double x = (a + (h * i));
f_answ = f_answ + Math.exp(x);
}
System.out.println("comp trap rule : "+ f_answ * h_n);
double S1rule = ((b-a)/n)*(Math.exp(a)+ (4
*(Math.exp(a+b)/2))+(Math.exp(b)));
System.out.print("Simpson’s 1/3 rule rule : "+ S1rule);
}
else if(func == 3) {
System.out.println("type the value inside the log");
Scanner sc3 = new Scanner(System.in);
double value1 = sc3.nextDouble();
double h = b - a;
double answ1 = (h/2)* (Math.log10(a) + Math.log10(b));
System.out.println("trap rule : "+answ1);
double h_n = (b - a) / n;
double f_answ = h_n * (Math.log10(a) + Math.log10(b));
for (int i = 1; i < n; i++) {
double x = a + h * i;
f_answ = f_answ + Math.log10(x);
}
System.out.print("comp trap rule : "+ f_answ * h_n);
}
else if(func == 4) {
System.out.println("enter the power of x");
Scanner sc4 = new Scanner(System.in);
double value2 = sc4.nextDouble();
double h = b - a;
double answ2 = (h/2)* (Math.pow(a,value2) + Math.pow(b,value2));
System.out.println("trap rule : "+answ2);
double h_n = (b - a) / n;
double f_answ = h_n * (Math.pow(a,value2) + Math.pow(b,value2));
for (int i = 1; i < n; i++) {
double x = a + h * i;
f_answ = f_answ + Math.pow(x,value2);
}
System.out.print("comp trap rule : "+ f_answ * h_n);
}
else if(func == 5) {
System.out.println("type the function under the root");
Scanner sc5 = new Scanner(System.in);
double value3 = sc5.nextDouble();
double h = b - a;
double answ3 = (float) ((h/2)* (Math.sqrt(value3*b) +
Math.sqrt(value3*a)));
System.out.println("trap rule : "+answ3);
double h_n = (b - a) / n;
double f_answ = h_n * (Math.sqrt(b) + Math.sqrt(a));
for (int i = 1; i < n; i++) {
double y = a + h * i;
f_answ = f_answ + Math.sqrt(y);
}
System.out.println("comp trap rule : "+ f_answ * h_n);