MATLAB Beginner递归函数 [英] MATLAB Beginner recursive functions

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

问题描述

在matlab函数的语法上有点困难;

Just having a little difficulty with the syntax of matlab functions;

function f = fact(x)
    if x == 1
        return
    else
        f = 1 - x*(fact(x-1))
    end
end

在命令窗口中使用参数10调用此函数时,出现错误

When calling this function in the command window with the argument 10 I receive the error

Undefined function 'fact' for input arguments of type 'double'.

Error in recursion (line 6)
    f = 1 - x*(fact(x-1))

我环顾四周,第一个解决方案围绕m文件的路径进行,这似乎不成问题,因为同一目录中的其他文件运行良好,

I've had a look around and solutions for the first revolve around the pathing of the m-file which doesn't seem to be a problem as other files in the same directory run fine,

第二个我不确定为什么在第6行出现错误,我想这与变量和函数名有关.

The second I'm not sure why the error in line 6 occurs, my guess is it has something to do with the variable and function names.

作为附带的问题,这两个end语句是否都必要?

As a side question, are both these end statements necessary?

谢谢!

推荐答案

最明显的错误是您的函数文件名.您在代码中定义了一个名为fact的函数,但已将文件命名为recursion.确保您的函数名称文件名均被都称为 fact.

The most obvious error is your function filename. You have a function called fact defined in your code but you named your file recursion. Make sure that both your function name and the filename are both called fact.

如果将文件命名为recursion,然后将代码中定义的函数名称命名为fact,这将是您尝试调用代码时发生的情况:

If you were to name your file as recursion, then make the function name defined in your code as fact, this is what would happen if you tried calling your code:

>> f = recursion(10);

Undefined function 'fact' for input arguments of type 'double'.

Error in recursion (line 6)
    f = 1 - x*(fact(x-1));

...看起来很熟悉吗?

... look familiar?

因此,请确保您的文件名和函数名称相同.实际上,在MATLAB编辑器中,它会自动给您一个错误,指出这两者都不相同.

As such make sure your filename and your function name are named the same. In fact, in the MATLAB editor, it should automatically give you an error saying that both of these are not the same.

您的代码中还有另一个错误.基本情况定义不正确.永远记住,当您编写递归算法时,该函数最终将要返回……这就是您遇到基本情况的时候.我们可以在这里看到x = 1的时间.当x = 1时,应该为f分配输出.您只是退出该函数,因此当x变为1时,您的代码将吐出一个错误,指出该函数完成时未分配f.因此,您需要确定基本情况.我将假设您的基本情况(当x = 1时)将等于0.您显然需要更改此设置,因为我不知道您的代码实际在计算什么.基本上,您需要这样做:

There is also another error in your code. The base case is not defined properly. Always remember when you are writing recursive algorithms is that eventually the function is going to return... and that's when you hit the base case. We can see here that it is when x = 1. When x = 1, you're supposed to assign something to f which is the output. You are simply exiting the function, and so when x becomes 1, your code will spit out an error saying that f was not assigned when the function finishes. As such, you need to figure out what your base case is. I'm going to assume that your base case (when x = 1) is going to equal 0. You will obviously need to change this as I don't know what your code is actually computing. Basically, you need to do this:

function f = fact(x)
    if x == 1
        f = 0; %// Base case needs to change according to your specs
    else
        f = 1 - x*(fact(x-1))
    end
end


当我这样做时,当x = 10

>> f = fact(10);

f =

 1334961


现在运行此代码时,我没有收到错误.另外,检查工作空间中是否有任何名为fact的变量.发生这种情况时,实际上您是在变量上使用了变量 shadow ,因此它实际上是在尝试访问名为fact的变量.因此,请执行clear all;尝试清除工作区,然后重试此代码.


I don't get an error when I run this code now. Also, check to see if you have any variables named fact in your workspace. When this happens, you are in fact shadowing over your function with a variable, so it is actually trying to access the variable called fact instead. As such, try clearing your workspace by doing clear all;, then try this code again.

如果将x指定为0或负数,此功能将从不停止.因此,您需要提供一些检查并在发生这种情况时执行正确的操作.另外,您需要确保指定x可接受的输入类型.从上下文来看,x仅是正整数.正如@Glen_b指出的那样,如果您提供的任何数字都不为正整数,则该函数将永远不会停止,因为x在递归管道中永远不会等于1.

If you were to specify x to be 0 or negative, this function will never stop. As such, you need to provide some check and perform the proper action when this happens. Also, you need to make sure that you specify what type of inputs are accepted for x. Judging from the context, x are positive integers only. As what @Glen_b has noted, should you provide any number that isn't a positive integer, this function will never stop as x will never equal 1 down the recursion pipeline.

第一个end语句是结束if语句所必需的.第二个end语句不是必需的,但无论如何都是一个好习惯.但是,如果您在函数文件中定义了多个函数,则可以肯定的是,必须正确表示该函数的末尾已定义在该文件中.但是,如果您只为每个文件编写一个函数,则不需要它,但我建议您将其保留在那里,因为这是一种很好的做法.

The first end statement is required to end the if statement. The second end statement isn't required, but it's good practice anyway. However, if you have multiple functions defined inside your function file, then yes it is most definitely required to properly signify that the end of that function is defined there. However, you don't need it if you're only writing one function per file, but I would recommend keeping it there as it's good practice.

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

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