从函数中找到不动点

时间:2015-09-29 04:32:19

标签: java

这是我们在Java类中给出的算法。目标是通过迭代返回一个固定点。

INPUT initial approximation p0; tolerance TOL; maximum number of iterations N0.
OUTPUT approximate solution p or message of failure.

Step 1 Set i=1.
Step 2 While i <= N0 do Steps 3-6.
Step 3 Set p=g(p0). (Compute pi.)
Step 4 If |p-p0| OUTPUT (p); (The procedure was successful.)
STOP.
Step 5 Set i=i+1.
Step 6 Set p0=p. (Update p0.)
Step 7 OUTPUT ('The method failed after N0 iterations, N0=', N0);
(The procedure was unsuccessful.)
STOP.

但问题是代码在C:

#include <stdio.h>
#include <math.h>

double f(double x)
{
return x*x*x*x-3*x*x-3;  //change equation for each problem
}

double g(double x)
{
return pow(3*x*x+3,.25);
}

int main()
{
    double p, p0, Tol;
    int i=1;
    int No;

    printf("Enter approximate p: ");
    scanf ("%lf", &p0);

    printf("Desired Tolerance: ");
    scanf ("%lf", &Tol);

    printf("Maximum Iterations: ");
    scanf ("%d", &No);


    while (i<=No)
    {
        p = g(p0);

        if((fabs(p-p0))<Tol)
        {
            //printf("%lf", &p);
            break;
        }
        printf("Iteration %d: Current value = %lf\n", i, p);

        i++;  //i=i+1
        p0=p;

        if (i>No)
        {
        printf("Method Failed after %d", No);
        printf(" iterations");
        }

    }

}

请帮我用Java转换它,因为我不懂C ++中的代码。

更新:这是我完成的事情:

import java.util.Scanner;

公共类HelloWorld {

public static void main(String args[]){
    Scanner console = new Scanner(System.in);
    double p, p0, Tol;
    int i = 1;
    int No;

    System.out.println("Enter approximate p: ");
    p0 = console.nextDouble();

    System.out.println("Desired tolerance: ");
    Tol = console.nextDouble();

    System.out.println("Maximum Iterations: ");
    No = console.nextInt();

    while(i<=No){
        p = g(p0);

        if((f(p-p0)) < Tol){
            System.out.println("P: " + p);
            break;
        }
        System.out.println("Iteration: Current Value = " + i + " " + p);

        i++;
        p0 = p;

        if(i>No){
            System.out.println("Method Failed after: " + No);
            System.out.println("");
        }
    }
}
public static double f(double x){
    return x*x*x*x-3*x*x-3;
}
public static double g(double x){
    return Math.pow(3*x*x+3, .25);
}

}

1 个答案:

答案 0 :(得分:0)

首先让我尝试解释原始代码中发生了什么,这样你就可以想到Java中的等价物。

// These are similar to java's import declarations.
#include <stdio.h>
#include <math.h>

/* This is a global function, there are no equivalents in java. However,
 * assuming you want to continue working in the static scope of the main
 * function you should make these into static functions */
double f(double x)
{
return x*x*x*x-3*x*x-3;  //change equation for each problem
}

double g(double x)
{
return pow(3*x*x+3,.25);
}

// This is your main function. In java this looks like:
// public static void main(String[] args){}
int main()
{
    double p, p0, Tol;
    int i=1;
    int No;

    // This is a print statement (System.out.printf())
    printf("Enter approximate p: ");
    // This taken in input of type double (signified by %lf)
    // and stores it into p0
    scanf ("%lf", &p0);

    printf("Desired Tolerance: ");
    scanf ("%lf", &Tol);

    printf("Maximum Iterations: ");
    scanf ("%d", &No);


    while (i<=No)
    {
        p = g(p0);
        // fabs gets the absolute value of the passed in value
        // in java this is done my Math.abs()
        if((fabs(p-p0))<Tol)
        {
            //printf("%lf", &p);
            break;
        }
        printf("Iteration %d: Current value = %lf\n", i, p);

        i++;  //i=i+1
        p0=p;

        if (i>No)
        {
        printf("Method Failed after %d", No);
        printf(" iterations");
        }

    }

}

现在为Java。在您阅读以下内容之前,请记住尝试从中学习,而不是将其交给您的教授。他可能期望不同的格式和/或语法。如果你没有学到这一点,那么你也会妨碍自己。

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double p, p0, tol;
        int i=1;
        int no;

        System.out.println("Enter approximate p: ");
        p0 = scanner.nextDouble();

        System.out.println("Desired Tolerance: ");
        tol = scanner.nextDouble();

        System.out.println("Maximum Iterations: ");
        no = scanner.nextInt();

        while (i<=no){
            p = g(p0);

            if(Math.abs(p-p0) < tol)
                break;

            System.out.printf("Iteration %d: Current value %f\n", i, p);

            i++;
            p0 = p;

            if(i>no)
                System.out.printf("Method failed after %d iterations\n", no);
        }
    }
    static double g(double x){
        return Math.pow(3*x*x+3, .25);
    }
    static double f(double x){
        return x*x*x*x-3*x*x-3;
    }
}