You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java课程作业:e^x近似值计算实现遇公式使用难题

Approximating e^x with Taylor Series in Java

Hey Lisa, no worries—this is a common sticking point when first working with series approximations, but we can break it down into simple, manageable steps. Let's walk through how to implement this correctly.

Core Idea Behind the Formula

The series you're working with is the Taylor series expansion of e^x. Instead of calculating each term from scratch (which would involve recalculating factorials every time and be inefficient), we can leverage the relationship between consecutive terms:

  • The first term is 1 (when k=0: x⁰/0! = 1)
  • Each subsequent term = previous term * x / current_index (for index k ≥1, xᵏ/k! = (xᵏ⁻¹/(k-1)!) * x / k)

This way, we build each term incrementally without having to compute large factorials directly.

Step-by-Step Implementation

Here's a complete Java program that does exactly what your assignment asks for:

import java.util.Scanner;

public class ExponentialApproximation {
    // Method to calculate e^x approximation for a given n
    private static double calculateEx(double x, int n) {
        double sum = 1.0; // Start with the first term (k=0)
        double currentTerm = 1.0; // Initialize current term to the first term
        
        for (int k = 1; k <= n; k++) {
            // Update current term: previous term * x / k
            currentTerm = currentTerm * x / k;
            // Add the current term to the total sum
            sum += currentTerm;
        }
        return sum;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        // Get user input for x
        System.out.print("Enter the value of x: ");
        double x = scanner.nextDouble();
        
        // Calculate approximations for n=5, 10, 50, 100
        double n5 = calculateEx(x, 5);
        double n10 = calculateEx(x, 10);
        double n50 = calculateEx(x, 50);
        double n100 = calculateEx(x, 100);
        
        // Print results
        System.out.println("\nApproximations of e^" + x + ":");
        System.out.printf("n=5: %.10f%n", n5);
        System.out.printf("n=10: %.10f%n", n10);
        System.out.printf("n=50: %.10f%n", n50);
        System.out.printf("n=100: %.10f%n", n100);
        
        scanner.close();
    }
}

Key Explanations

  • calculateEx Method: This reusable method takes x and n as inputs, computes the sum iteratively, and returns the approximation.
  • Incremental Term Calculation: Using currentTerm = currentTerm * x / k is the most efficient way to build each term—no need to compute x^k or k! separately, which saves computation time and reduces the chance of errors with large numbers.
  • Output Formatting: printf is used to print results with 10 decimal places, making it easy to see how the approximation improves as n increases.

Example Output

If you input x=1, you'll get something like this:

Enter the value of x: 1

Approximations of e^1.0:
n=5: 2.7166666667
n=10: 2.7182818011
n=50: 2.7182818284
n=100: 2.7182818284

Notice how as n increases, the approximation gets closer to the actual value of e (~2.7182818284).

内容的提问来源于stack exchange,提问作者Lisa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:13:35