有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java中不使用内置函数计算e^x

我是一名Java初学者,目前正在阅读《如何像计算机科学家一样思考》入门书。我在迭代一章中遇到了一个问题。谁能给我指一下正确的方向吗

当我使用数学时。exp,我得到的答案与代码得到的答案完全不同。 注意,这不是家庭作业

问题是:

One way to calculate ex is to use the infinite series expansion ex = 1 + x + x2 /2! + x3/3! + x4/4! +... If the loop variable is named i, then the ith term is xi/i!.

  1. Write a method called myexp that adds up the first n terms of this series.

下面是代码:

public class InfiniteExpansion {
    public static void main(String[] args){

        Scanner infinite = new Scanner(System.in);
        System.out.println("what is the  value of X?");
        double x = infinite.nextDouble();
        System.out.println("what is the power?");
        int power = infinite.nextInt();



        System.out.println(Math.exp(power));//for comparison

        System.out.println("the final value of series is: "+myExp(x, power));
    }

    public static double myExp(double myX, double myPower){

        double firstResult = myX;
        double denom = 1;
        double sum =myX;

        for(int count =1;count<myPower;count++){

            firstResult = firstResult*myX;//handles the numerator

            denom = denom*(denom+1);//handles the denominator

            firstResult = firstResult/denom;//handles the segment

            sum =sum+firstResult;// adds up the different segments
        }

        return (sum+1);//gets the final result
    }

}

共 (4) 个答案

  1. # 1 楼答案

    这里有几个错误:

    1. firstResult应该从1开始,这样它就变成了1+x+x^2,而不是1+x^2+x^3
    2. 正如timctran所说,你没有以正确的方式计算阶乘

    总而言之,您可以将操作简化为:

    firstResult = firstResult * myX / (count+1);
    sum += firstResult;
    

    编辑:
    -我运行了代码,看到了数学。exp(power)是打印出来的,而不是数学。exp(x)
    -我的第一项是错误的,因为sum被初始化为myX

  2. # 2 楼答案

    赋值denom = denom*(denom+1)将给出如下序列:1, 1*2=2, 2*3=6, 6*7=42, 42*43=... 但是你想要denom = denom*count

    一般来说,我们只想打印第一个n阶乘,从1!1!, 2!, 3!, ..., n!开始。在第k项,我们取第k-1项并乘以k。这将是在前一项上递归计算k!。具体例子:4!3!46!5!6

    在代码中,我们有

    var n = 7;
    var a = 1;
    for (int i = 1; i <= n; i++ ) {
        a = a*i; // Here's the recursion mentioned above.
        System.out.println(i+'! is '+a);
    }
    

    试着运行上面的程序,比较一下运行下面的程序会得到什么结果:

    var n = 7;
    var a = 1;
    for (int i = 1; i <= n; i++ ) {
        a = a*(a+1);
        System.out.println('Is '+i+'! equal to '+a+'?');
    }
    
  3. # 3 楼答案

    为什么要把它复杂化?我尝试了一个解决方案,结果如下:

    //One way to calculate ex is to use the infinite series expansion 
    //ex = 1 + x + x2 /2! + x3/3! + x4/4! +... 
    //If the loop variable is named i, then the ith term is xi/i!.
    //
    //Write a method called myexp that adds up the first n terms of this series.
    import java.util.Scanner;
    
    public class InfiniteExpansion2 {
    
        public static void main(String[] args) {
    
            Scanner infinite = new Scanner(System.in);
            System.out.println("what is the  value of X?");
            double x = infinite.nextDouble();
            System.out.println("what is the value of I?");  // !
            int power = infinite.nextInt();
    
            System.out.println(Math.exp(power));//for comparison
    
            System.out.println("the final value of series is: " + myCalc(x, power));
        }
    
        public static double fac(double myI) {
            if (myI > 1) {
                return myI * fac(myI - 1);
            } else {
                return 1;
            }
        }
    
        public static double exp(double myX, double myE) {
            double result;
            if (myE == 0) {
                result = 1;
            } else {
                result = myX;
            }
            for (int i = 1; i < myE; i++) {
                result *= myX;
            }
            return result;
        }
    
        public static double myCalc(double myX, double myI) {
            double sum = 0;
            for (int i = 0; i <= myI; i++) { // x^0 is 1
                sum += (exp(myX, i) / fac(i));
            }
            return sum;
        }
    
    }
    

    如果你想像工程师一样思考,我会这样做:

    • 保持简单
    • 把它打碎
    • 紧紧抓住这项任务(比如我把var命名为myI,而不是myPower——首先,对我来说似乎更清楚——这样你就不会感到困惑)

    希望你喜欢

  4. # 4 楼答案

    我尝试了一个解决方案,结果如下:

    public class Fact {
    
        public int facto(int n){
                if(n==0)
                    return 1;
                else
                    return n*facto(n-1);
            }
        }
    
    }
    
    import java.util.Scanner;
    
    public class Ex {
    
    public static void main(String[] args){
    
            Fact myexp=new Fact();
    
            Scanner input=new Scanner(System.in);
            int n=1;
    
            double e=1,i=0,x;
            int j=1;
            System.out.println("Enter n: ");
            n=input.nextInt();
            System.out.println("Enter x: ");
            x=input.nextDouble();
            while(j<=n)
            {
                int a=myexp.facto(j);
                double y=Math.pow(x,j)/(double)a;
    
                i=i+y;
                ++j;
            }       
    
            e=e+i;
            System.out.println("e^x= "+ e);
    
        }
    }