140 likes | 231 Vues
Learn about Math library methods such as sqrt, pow, sin, cos along with recursion examples in Java. Explore quadratic solver, velocity calculation, error approximation, vector summation, and more.
E N D
Library MethodsandRecursion Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in
Announcements • Lab tests next week • You will get 90 minutes to solve two problems • In the remaining 90 minutes your work will be graded with the tutors’ inputs • Syllabus same as mid-term I
Math library • Math.sqrt (x) • Takes double, returns double • Math.pow (x, n) • Takes two doubles, returns double • Math.sin (x) • Takes double, returns double • Math.cos (x) • Takes double, returns double • Check out online Math library and other libraries
Roots of a quadratic class QuadraticSolver { public static void main (String arg[]) { double a=1.0, b=-2.0, c=1.0; double r1, r2; r1 = (-b+Math.sqrt(b*b-4*a*c))/(2*a); r2 = (-b-Math.sqrt(b*b-4*a*c))/(2*a); System.out.println(“Roots are: ” + r1 + “, ” + r2); } }
Velocity on inclined plane class velocityExample{ public static void main(String arg[]){ double theta=Math.PI/3.0; double g=9.81; double x=10; double velocity; velocity = Math.sqrt(2*g*Math.sin(theta)*x); System.out.println(“Final velocity: ” + velocity + “ m/s”); } }
Error in eπ class errorE{ public static void main(String arg[]){ double x = Math.PI; double approx, error; approx = 1 + x + x*x/2 + Math.pow(x, 3)/6 + Math.pow(x, 4)/24; error = Math.exp(x) – approx; System.out.println(“Error up to fourth power: ” + error); } }
Sum of vectors class vectorSum{ public static void main(String arg[]){ double v1 = 10.0; double v2 = 21.2; double angle = 120; // In degrees double resultant; resultant = Math.sqrt(v1*v1 + v2*v2 - 2*v1*v2*Math.cos(angle*Math.PI/180.0)); System.out.println(“Resultant of ” + v1 + “ and ” + v2 + “ at ” + angle + “ degrees is ” + resultant); } }
Which one is bigger? class whichOneIsBigger{ public static void main(String arg[]){ double x = Math.PI; double y = Math.E; double difference; difference = Math.exp(x) – Math.pow(x, y); System.out.println(“Difference: ” + difference); } }
Adiabatic expansion class Adiabatic{ public static void main(String arg[]){ double P1 = 10; double P2 = 15.6; double V1 = 100; double V2; double gamma = 1.66; V2 = V1*Math.pow((P1/P2), 1.0/gamma); System.out.println(“New volume: ” + V2); } }
Recursion • Recursion is a process of calling the same method from the method body: self-reference class recurExample { public static void main (String arg[]) { f (10); } public static void f (int n) { System.out.println(n); if (n > 0) { f(n-1); } } }
Recursion class AnotherExample { public static void main (String arg[]) { f (10); } public static void f (int n) { System.out.println(n); if (n > 0) { f(n-1); } System.out.println(n); } }
Recurrence relations • Recursive methods can be used to compute recurrence relations easily • Sum of the first n natural numbers satisfies the following recurrence S(n) = S(n-1) + n for n > 1; S(1) = 1
Sum of first n numbers class SumOfNumbers { public static void main (String arg[]) { int n = 10; System.out.println(“Sum of the first ” + n + “ natural numbers is ” + Sum(n)); } public static int Sum(int n) { if (n == 1) return 1; // initial condition return (Sum(n-1) + n); } }
Fibonacci series • A second order recurrence Fn = Fn-1 + Fn-2 for n > 2; F1 = F2 = 1 class Fibonacci { public static void main (String arg[]) { int n = 10; System.out.println(n + “th Fibonacci number is ” + Fibonacci(n)); } public static int Fibonacci (int n) { if ((1==n) || (2==n)) return 1; return (Fibonacci(n-1) + Fibonacci(n-2)); } // Try to compute the total number of } // method calls