用递归找到零点 [英] Find zero points with recursion

查看:103
本文介绍了用递归找到零点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到一个正弦函数的零点.该参数是间隔[a,b].我必须做到类似于二进制搜索.

I want to find the zero points of a sine function. The parameter is a interval [a,b]. I have to it similar to binary search.

实现一个函数,该函数在a和b之间的间隔中搜索窦函数中的空点.应该将search-interval [下限,上限]减半,直到下限和上限彼此之间的距离小于0.0001.

这是我的代码:

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

        System.out.println(zeropoint(5,8));
    }

    private static double zeropoint(double a, double b){
        double middle = (a + b)/2;

        if(Math.sin(middle) < 0){
            return zeropoint(a,middle);
        }else if(Math.sin(middle) > 0){
            return zeropoint(middle,b);
        }else{
            return middle;
        }       
    }
}

在返回返回零点(middle,b);

第一步,我只想找到间隔中的第一个零点.

In a first step I want to find just the first zero point in the interval.

有什么想法吗?

推荐答案

每个人都忽略的基本问题:

Fundamental problems that everybody has overlooked:

  • 我们并不总是想要返回结果(想象找到pi/4和3pi/4之间的正弦函数的零点,没有任何东西).
  • 在任意范围内,可能会有多个零.

很明显,需要一组(可能为空)值.

Clearly what is needed is a (possibly empty) set of values.

因此真正要求函数 的伪代码(不使用Java,因为这是家庭作业):

So pseudocode of the function really asked for (not using Java as this is homework):

Set zeropoint(double a, double b)
{
    double middle = mid point of a and b;
    if a and be less than 0.0001 apart
    {
        if (sin(a) and sin(b) are on opposite sides of 0)
        { 
            return set containing middle
        }
        else
        {
            return empty set
        }
    }
    else
    {
        return union of zeropoint(a, middle) and zeropoint(middle, b)
    }
} 

这篇关于用递归找到零点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆