将函数f(x)除以x,用MATLAB, f(x)有另一个参数y是一个向量。

时间:2021-10-12 16:05:25

I would like to numerically integrate a vector which represents a function f(x) over the range of x specified by bounds x0 and x1 in Matlab. I would like to check that the output of the integration is correct and that it converges.

我想要对一个表示函数f(x)的向量进行数值积分,这个向量是在Matlab中,由界限x0和x1所指定的x的范围。我想检查一下积分的输出是否正确,它收敛。

There are the quad and quadl functions that serve well in identifying the required error tolerance, but they need the input argument to be a function and not the resulting vector of the function. There is also the trapz function where we can enter the two vectors x and f(x), but then it computes the integral of f(x) with respect to x depending on the spacing used by vector x. However, there is no given way using trapz to adjust the tolerance as in quad and quadl and make sure the answer is converging.

有quad和quadl函数可以很好地识别所需的错误容错,但是它们需要输入参数是函数,而不是函数的结果向量。还有trapz函数,我们可以进入两个向量x和f(x),然后计算的积分f(x)对x取决于所使用的间隔向量x。然而,没有被使用trapz调整公差四和quadl并确保答案是收敛的。

The main problem why I can't use quad and quadl functions is that f(x) is the following equation: f(x) = sum(exp(-1/2 *(x-y))), the summation is over y, where y is a vector of length n and x is an element that is given each time to the function f(x). Therefore, all elements in vector y are subtracted from element x and then the summation over y is calculated to give us the value f(x). This is done for m values of x, where m is not equal to n.

我不能用四和四函数的主要问题是,f(x)是下面的方程:f(x) = sum(exp(-1/2 *(x-y)),总和为y,其中y是长度为n的向量,x是每一次函数f(x)的一个元素。因此,向量y中的所有元素都从元素x中减去,然后求和除以y就得到了f(x)的值。这是对m值的x, m不等于n。

When I use quadl as explained in the Matlab manual, where f(x) is defined in a separate function .m file and then in the main calling file, I use Q = quadl(@f,x0,x1,tolerance,X,Y); here X is a vector of length m and Y is a vector of length L. Matlab gives an error: "??? Error using ==> minus Matrix dimensions must agree." at the line where I define the function f(x) in the .m function file. f(x) = sum(exp(-1/2 *(x-y)))

当我在Matlab手册中使用quadl时,f(x)在一个单独的函数.m文件中定义,然后在主调用文件中,我使用Q = quadl(@f,x0,x1,,x,Y);这里X是长度m的向量,Y是长度l的向量,Matlab给出了一个错误:???使用==> -矩阵维度的错误必须一致。f(x)=总和(exp(1/2 *(x - y)))

I assume the problem is that Matlab treats x and y as vectors that should be of the same length when they are subtracted from each other, whereas what's needed is to subtract the vector Y each time from a single element from the vector X.

我假设问题是,Matlab将x和y作为向量,当它们彼此相减时,应该是相同长度的,而我们需要的是,每一次从向量x中减去向量y。

Would you please recommend a way to solve this problem and successfully numerically integrate f(x) versus x with a method to control the tolerance?

请您推荐一种方法来解决这个问题,并成功地将f(x)与x结合使用一种方法来控制公差?

2 个解决方案

#1


3  

From the documentationon quad it says:

根据quad的文件,上面写着:

The function y = fun(x) should accept a vector argument x and return a vector result y, the integrand evaluated at each element of x.

函数y = fun(x)应该接受一个向量参数x,并返回一个向量结果y,并在x的每个元素上进行积分。

So every time we call the function, we need to evaluate the integrand at each given x.

所以每次我们调用这个函数时,我们需要对每个给定的x进行积分。

Also, to parameterize the function call with the constant vector Y, I recommend an anonymous function call. There's a reasonable demo here. Here's how I implemented your problem in Matlab:

另外,要用常量向量Y参数化函数调用,我建议使用一个匿名函数调用。这里有一个合理的演示。下面是我如何在Matlab中实现你的问题:

function Q = test_num_int(x0,x1,Y)
  Q = quad(@(x) myFun(x,Y),x0,x1);
end

function fx = myFun(x,Y)
  fy = zeros(size(Y));
  fx = zeros(size(x));
  for jj=1:length(fx)
    for ii=1:length(Y)
      fy(ii) = exp(-1/2 *(x(jj)-Y(ii)));
    end
    fx(jj) = sum(fy);
  end
end

Then I called the function and got the following output:

然后我调用这个函数,得到如下输出:

Y = 0:0.1:1;
x0 = 0;
x1 = 1;
Q = test_num_int(x0,x1,Y)

Q =

   11.2544

The inputs for the lower and upper bound and the constant array are obviously just dummy values, but the integral converges very quickly, almost immediately. Hope this helps!

对于下和上界和常量数组的输入显然只是虚值,但这个积分很快就收敛了。希望这可以帮助!

#2


1  

I believe the following would also work:

我相信以下几点也能奏效:

y = randn(10,1); 
func = @(x) sum(exp(-1/2 *(x-y)));
integral(func,0,1,'ArrayValued',true)

#1


3  

From the documentationon quad it says:

根据quad的文件,上面写着:

The function y = fun(x) should accept a vector argument x and return a vector result y, the integrand evaluated at each element of x.

函数y = fun(x)应该接受一个向量参数x,并返回一个向量结果y,并在x的每个元素上进行积分。

So every time we call the function, we need to evaluate the integrand at each given x.

所以每次我们调用这个函数时,我们需要对每个给定的x进行积分。

Also, to parameterize the function call with the constant vector Y, I recommend an anonymous function call. There's a reasonable demo here. Here's how I implemented your problem in Matlab:

另外,要用常量向量Y参数化函数调用,我建议使用一个匿名函数调用。这里有一个合理的演示。下面是我如何在Matlab中实现你的问题:

function Q = test_num_int(x0,x1,Y)
  Q = quad(@(x) myFun(x,Y),x0,x1);
end

function fx = myFun(x,Y)
  fy = zeros(size(Y));
  fx = zeros(size(x));
  for jj=1:length(fx)
    for ii=1:length(Y)
      fy(ii) = exp(-1/2 *(x(jj)-Y(ii)));
    end
    fx(jj) = sum(fy);
  end
end

Then I called the function and got the following output:

然后我调用这个函数,得到如下输出:

Y = 0:0.1:1;
x0 = 0;
x1 = 1;
Q = test_num_int(x0,x1,Y)

Q =

   11.2544

The inputs for the lower and upper bound and the constant array are obviously just dummy values, but the integral converges very quickly, almost immediately. Hope this helps!

对于下和上界和常量数组的输入显然只是虚值,但这个积分很快就收敛了。希望这可以帮助!

#2


1  

I believe the following would also work:

我相信以下几点也能奏效:

y = randn(10,1); 
func = @(x) sum(exp(-1/2 *(x-y)));
integral(func,0,1,'ArrayValued',true)