问题描述
我有一个文件,其中包含以下代码:
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:
我想做的是使用梯形近似将函数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,
而不是
解决方案
您可以为此创建一个子功能
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函数(解决错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!