2012-01-10 2 views
0

함수를 만들려는데 그 중 하나는 시리즈 {x[i], i = 0, 1, ..., inf}입니다. 예를 들어수학 시리즈를 표현하는 방법

:이 시리즈로 x_m = 1/(1+x_(m-1)), m=1,2....inf

, 나는 그것이 내가 정의하는 몇 가지 조건을 만족하는 경우, [i]는 모든 X를 확인하려면 (내가있는 N이 만족 것이다 모른다). 예를 들어

:

x_0 < 5 경우에, 나는 x_0의 값을 반환합니다.

그렇지 않은 경우 - x_1에 대해 확인하겠습니다. x_1<5 인 경우 x_1의 값을 반환합니다.

등등 ..

참고 : 계산에 x_0의 가치를 알고 x_1 필요.

Java에서이 시리즈를 쉽게 represnt 할 수 있습니까?

+8

. 이 시리즈를 통해 무엇을하고 싶으신 지 알려 주시면 어떻게 표현할 지 알려 드릴 수 있습니다. –

+0

@ JasonS : 질문을 편집합니다. 명백합니까? –

답변

2

다음은 수행하려는 것을 나타낼 수있는 방법입니다. 그러나 내가 제공 한 합계 함수는 분명히하지 않습니다.

하나의 경우, 순서는 반복이며 여기서는 재귀 적으로 구현됩니다. 당신은 그것을 암기하고 싶을 것이다. SeriesUtil에 그 작업을 추가 할 것을 제안합니다.

더 중요한 것은, 분명히 상 한계로 $ \ infinity $를 가질 수 없을 것입니다. 분명히 알다시피, 이것은 사소한 문제입니다. 즉, 알려진 솔루션을 가지고 GeometricSeries과 같은 Series을 구현하는 다양한 추상 클래스를 가질 수 있으며 주어진 계열 유형에 적합한 전략을 사용할 수 있습니다.

interface Series { 
    double x(int m); 
} 

class ExampleSeries implements Series { 
    public double x(int m) { 
     if (m == 0) { 
      return 0; 
     } 

     return 1/(1 + x(m - 1)); 
    } 
} 

class SeriesUtil { 
    public static double sum(Series series, int lowerBound, int upperBound) { 
     int sum = 0; 
     for (int i = lowerBound; i <= upperBound) { 
      sum += x(i); 
     } 

     return sum; 
    } 
} 
2

이 부분적으로 만 대답 : 수학자로서

, 난 당신이 아마 좀 더 어쩌면 방법 double getElement(int m)과 함께 일련의 위한 인터페이스를 정의 할 말 것입니다. 그런 다음 계열에 따라 구현 클래스가 다를 수 있습니다. FormulaSeries 또는 RecursiveSeries 일 수 있습니다. 여기서 계열은 일부 재귀 수식으로 주어집니다 (예제에서와 같이). 이 클래스는 나중에 사용하기 위해 중간 값을 내부적으로 저장할 수 있습니다 (메모).

목표에 따라 부분 시리즈 ( )를 구현할 수 있습니다. 여기서 한정된 수의 알려진 값만이 목록이나 배열에 저장 될 수 있습니다.

RecursiveSeries에 초점을 맞춘 경우 수식이 실제로 계산하기가 어려우면 계산 된 값을 내부적으로 고정 값 Map에 저장하고 요소가 여러 번 필요할 수 있습니다. 알아 내기가 너무 어렵지 않아야합니다.

+0

우리는 같은 줄을 생각하고있는 것처럼 보입니다. – Ray

0

물론, 레이 & Paxinum 당신이 필요로하는 가이드를 제공하고 있습니다. 당신이해야 할 일은 사건에 맞게 조정하는 것뿐입니다.

그러나 학업 목적으로 강사가 상세한 스크립트를 볼 필요가있는 경우 을 고려하십시오. array 대신 ArrayList가 사용되므로 후자와 달리 제한 사항을 다시 정의 할 수 있습니다. 다음 코드를 복사하여 붙여넣고 작동 방법을 볼 수 있습니다. 필요한 경우 사양으로 수정하십시오. 나는 또한 의견을 제공했다. 그들은 가능한 한 많은 코드를 설명했다. 건배!

import java.util.ArrayList; 
import java.util.Scanner; 

public class SeriesTest 
{ 

// This internal class "Index", will represent an index Object that holds a 
// value and returns the value when needed 
    class Index 
    { 
     private double idxElement; 

     private void setIdxElement(double value) 
     { 
      idxElement = value; 
     } 

     private double getIdxElement() 
     { 
      return idxElement; 
     } 
    } 

// Create an Object index of type Index (i.e the class defined above) 
Index index; 

// Create an ArrayList, x_ , that will reference the index Objects. 
ArrayList<Index> x_ = new ArrayList<Index>(); 

public void calculateResult() 
{ 
    System.out.println("How many elements do you want the series to have?"); 
    Scanner input = new Scanner(System.in); 
    int NoOfelements = input.nextInt(); 

    int value = 0; 

    // create a new instance of type Index 
    index = new Index(); 

    // set the element held by this index as 0(zero) 
    // You can set this to any value depending on your Series' requirements 
    index.setIdxElement(value); 

    /*add the index object to the ArrayList 
    *This represents your x_m 
    *Note - This references the index Object that holds the value = 0)*/ 
    x_.add(index); 

    /* To do the same thing for the rest of the elements using 
    * your specified equation ---> x_m = 1/[ 1 + x_(m-1) ]*/ 
    for (int m = 1; m < NoOfelements; m++) 
    { 
     index = new Index(); 
     // sets the value of the element referenced by the index object 
     // This represents your x_m = 1/[ 1 + x_(m-1) ] 
     index.setIdxElement(1/(1 + x_.get(m - 1).getIdxElement())); 

     // adds the index object to the ArrayList 
     x_.add(index); 
    } 
} 

public void displayResults() 
{ 
    // displays the series as referenced by the the ArrayList 
    // Note - ArrayList indexes start at 0(zero) 
    System.out.println("The series is:"); 
    for (int n = 0; n < x_.size(); n++) 
    { 
     System.out.printf("%.2f, ", x_.get(n).getIdxElement()); 
    } 
} 
public static void main(String[] args) 
{ 
    SeriesTest series = new SeriesTest(); 
    series.calculateResult(); 
    series.displayResults(); 
} 

} 당신은 추상적 인 개념에 대해 얘기하고

관련 문제