我如何在Matlab中将值传递给子函数?输出错误? [英] How do you pass in a value to a subfunction in Matlab I am getting output errors?

查看:195
本文介绍了我如何在Matlab中将值传递给子函数?输出错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我在Matlab中的代码,行和=(h/2)*(f(a)+ f(b))+ h; Matlab说,当我尝试调用f(x)函数时,我必须有很多输出.是f(x)函数的问题

Below is my code in Matlab I am having trouble with the line sum = (h/2) * (f(a) + f(b)) + h; Matlab says I have to many outputs when I try to call the f(x) function. Is my problem with the f(x) function

function Trapezoid_Uniform(a,b,n)
    h = (b - a)/n;
    sum = (h/2) * (f(a) + f(b)) + h;

    for i = 1:n-1
        x = a + i*h;
        sum = sum + f(x);
    end

    sum = sum*h;
    disp(sum);
end

function f(z)
    f = exp(z);
end

推荐答案

您需要在函数中指定返回的变量.例如,在C ++中,有一个明确的return语句-MATLAB如何知道需要返回什么?您在签名中指定它,即在本例中为f_of_z.

You need to specify the returned variable in your function. For e.g., In C++ there's an explicit return statement - how does MATLAB know what needs to be returned? You specify it in the signature, i.e. in this case f_of_z.

function f_of_z = f(z)

f_of_z = exp(z);

end

这篇关于我如何在Matlab中将值传递给子函数?输出错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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