MATLAB错误消息(函数定义) [英] MATLAB Error Message (function definitions)

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

问题描述

当我在MATLAB命令窗口中键入CalculateIntegral(2,5)时,出现以下错误:

I am getting the following error when I type in CalculateIntegral(2,5) into the MATLAB Command Window:

???错误:文件:CalculateIntegral.m行:2列:1 在提示或脚本中不允许使用函数定义.

??? Error: File: CalculateIntegral.m Line: 2 Column: 1 Function definitions are not permitted at the prompt or in scripts.

我不确定如何解决此错误.谢谢.

I am not sure how to resolve this error. Thanks.

    clear all;
    function g = CalculateIntegral(s,N)
    a=0; 
    b=1;
    h=(b-a)/N;
    x = 0:h:1;
    g = ff(x).*exp(-s*x);

    % compute the exact answer of the integral
    exact_answer=antiderivative(b,s)-antiderivative(a,s);

    % compute the composite trapezoid sum
    If=0;
    for i=1:(N-1)
        If=If+g(i)*h;
    end;
    If=If+g(1)*h/2+g(N)*h/2;
    If;

推荐答案

在函数定义之前不能有clear all(并且不需要).只需删除第一行即可使您的代码正常工作. MATLAB函数必须独立存在于自己的文件中,命名为功能(在您的情况下为CalculateIntegral.m).

You can't have a clear all before your function definition (and you don't need one). Just remove that first line to make your code work. MATLAB functions need to be by themselves in their own file, named like the function (CalculateIntegral.m in your case).

这篇关于MATLAB错误消息(函数定义)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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