MATLAB函数(解决错误) [英] MATLAB Function (Solving an Error)

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

问题描述

我有一个文件,其中包含以下代码:

I have one file with the following code:

function fx=ff(x)
fx=x;

我还有另一个文件,其代码如下:

I have another file with the following code:

function g = LaplaceTransform(s,N)
g = ff(x)*exp(-s*x);

a=0; 
b=1;

If=0;
h=(b-a)/N;
If=If+g(a)*h/2+g(b)*h/2;
for i=1:(N-1)
    If=If+g(a+h*i)*h;
end;
If

每当我运行第二个文件时,都会出现以下错误:

Whenever I run the second file, I get the following error:

未定义的函数或变量'x'.

Undefined function or variable 'x'.

我想做的是使用梯形近似将函数g集成在0和1之间.但是,我不确定如何处理x,这显然会引起问题,如错误所示.

What I am trying to do is integrate the function g between 0 and 1 using trapezoidal approximations. However, I am unsure how to deal with x and that is clearly causing problems as can be seen with the error.

任何帮助都会很棒.谢谢.

Any help would be great. Thanks.

推荐答案

看起来像您要尝试的操作是在变量g中创建一个函数.也就是说,您希望第一行表示

Looks like what you're trying to do is create a function in the variable g. That is, you want the first line to mean,

g(x)成为这样计算的函数:ff(x)*exp(-s*x)",

"Let g(x) be a function that is calculated like this: ff(x)*exp(-s*x)",

而不是

计算ff(x)*exp(-s*x)的值并将结果放入g".

"calculate the value of ff(x)*exp(-s*x) and put the result in g".

解决方案

您可以为此创建一个子功能

Solution

You can create a subfunction for this

function result = g(x)
  result = ff(x) * exp(-s * x);
end

或者您可以创建一个匿名函数

Or you can create an anonymous function

g = @(x) ff(x) * exp(-s * x);

然后,您可以使用g(a)g(b)等来计算所需的内容.

Then you can use g(a), g(b), etc to calculate what you want.

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

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