Convert for loop to recursive function

This is a home problem. I am having trouble converting the following into a recursive function:

public class Integrate { public static double integrate(int a, int b, int steps) { double sum=0; double delta = 1.0 * (b - a)/steps; double x = a; double f = 0.5*x*x + 3*x + 5; for (int i = 0; i< steps; i++) { x = x + delta; double fr = 0.5*x*x + 3*x + 5; double area = f * delta + 0.5*(fr - f)*delta; sum += area; f = fr; } return sum; } public static void main(String [] args) { int a, b, step; a = Integer.parseInt(args[0]); b = Integer.parseInt(args[1]); step = Integer.parseInt(args[2]); System.out.format("Integral is %f\n", integrate(a,b,step)); } } 

This is what I have so far, but the result does not match the source code. I can’t understand what’s wrong.

 public class Integrate { public static double integrate(int a, int b, int steps) { double sum=0; int i=0; sum = rintegrate(a, b, steps, i, sum); return sum; } public static double rintegrate(int a, int b, int steps, int i, double sum) { double delta = 1.0 * (b - a)/steps; double x = a; double f = 0.5*x*x + 3*x + 5; if (i<steps) { x = x + delta; double fr = 0.5*x*x + 3*x + 5; double area = f * delta + 0.5*(fr - f)*delta; sum += area; f = fr; i++; rintegrate(a, b, steps, i, sum); } return sum; } public static void main(String[] args) { int a, b, step; a = Integer.parseInt(args[0]); b = Integer.parseInt(args[1]); step = Integer.parseInt(args[2]); System.out.format("Integral is %f\n", integrate(a,b,step)); } } 
+4
source share
3 answers

I will not fully analyze the problem, but here are some observations that I have

  if (i<steps) { x = x + delta; double fr = 0.5*x*x + 3*x + 5; double area = f * delta + 0.5*(fr - f)*delta; sum += area; f = fr; i++; rintegrate(a, b, steps, i, sum); } return sum; 

everything between sum += area; and return sum; is redundant.

  • you set f to fr , but after that you don't even use f . if you want f be different next time, maybe you can pass it as a parameter to your recursive function.
  • you call rintegrate(...) recursively, but you do nothing with the value it returns. you can use this value.

You should consider recursion how to use a smaller version of the problem to solve the problem itself.

Here is the code for your problem, assuming you have a function: segment , which simply calculates the size of the first segment given by a , and delta

 rintegrate(a, b, steps) { if(steps <= 1) { delta = ba; return segment(a, delta) } else { delta = (ba)/steps return segment(a, delta) + rintegrate(a+delta, b, steps-1) } } 
+3
source

Working version

Just copy the paste and you will get the same result as your original method.

  public static void main(String[] args) { int a = 1, b = 10, step = 1000; double delta = 1.0 * (b - a) / step; double sum = integrate(a, b, step, 0, 0, 0, delta); double test = working(a, b, step); System.out.println("Integral is " + sum); System.out.println("Integral is " + test); } 

Working recursive version:

  public static double integrate(double x, int b, int steps, int i, double sum, double f, double delta) { f = 0.5 * x * x + 3 * x + 5; if (i < steps) { x = x + delta; double fr = 0.5 * x * x + 3 * x + 5; double area = f * delta + 0.5 * (fr - f) * delta; return integrate(x, b, steps, i + 1, sum + area, fr, delta); } return sum; } 

Source iterative method

 public static double working(int a, int b, int steps) { double sum = 0; double delta = 1.0 * (b - a) / steps; double x = a; double f = 0.5 * x * x + 3 * x + 5; for (int i = 0; i < steps; i++) { x = x + delta; double fr = 0.5 * x * x + 3 * x + 5; double area = f * delta + 0.5 * (fr - f) * delta; sum += area; f = fr; } return sum; } 
+1
source

Is this what you want;)

 public class Integrate{ /** * @param args */ public static void main(String[] args) { int a, b, step; a = Integer.parseInt(args[0]); b = Integer.parseInt(args[1]); step = Integer.parseInt(args[2]); System.out.format("Integral is %f\n", adaptiveSimpsons(a, b, step)); } private static double f(double i) { return (0.5 * i * i + 3 * i + 5); } static double adaptiveSimpsons(double a, double b, // interval [a,b] int maxRecursionDepth) { // recursion cap double c = (a + b) / 2, h = b - a; double fa = f(a), fb = f(b), fc = f(c); double S = (h / 6) * (fa + 4 * fc + fb); return adaptiveSimpsonsAux(a, b, S, fa, fb, fc, maxRecursionDepth); } private static double adaptiveSimpsonsAux(double a, double b, double S, double fa, double fb, double fc, int bottom) { double c = (a + b) / 2, h = b - a; double d = (a + c) / 2, e = (c + b) / 2; double fd = f(d), fe = f(e); double Sleft = (h / 12) * (fa + 4 * fd + fc); double Sright = (h / 12) * (fc + 4 * fe + fb); double S2 = Sleft + Sright; if (bottom <= 0) return S2 + (S2 - S) / 15; return adaptiveSimpsonsAux(a, c, Sleft, fa, fc, fd, bottom - 1) + adaptiveSimpsonsAux(c, b, Sright, fc, fb, fe, bottom - 1); } } 

Tested and working

Translated C code specified here

0
source

Source: https://habr.com/ru/post/1480529/


All Articles