I need to evaluate the integral from x=-1
to x=1
for the function x*e^(x)*sin(pi*x)
. To do so, I'm using the Gaussian Quadrature method. This method can be summarized as the summation of the (weights)*(function evaluated at given roots)
from k=1
to k=n
where n=30
.
我需要评估从x = 1积分为函数x x = 1 * e ^(x)*罪(π* x)。为此,我使用高斯求积法。这个方法可以归纳为(权重)*(给定根的函数值)从k=1到k=n,其中n=30。
In my code below, the roots are found in the column vector LegendreGaussQuadratureConstants(n).x
. I'm taking the transpose because I want to perform element by element multiplication on the weights, which are stored in the row vector LegendreGaussQuadratureConstants(n).w
在我下面的代码中,根出现在列向量勒让德鲁斯中队常数(n).x中。我取转置是因为我想对权重进行元素乘法运算,这些元素存储在行向量勒让格鲁高斯中队常数(n).w中
My code:
我的代码:
fx = @(x) x .* e.^(-x) .* sin(pi .* x);
for k = 1:50
Leg(k) = (fx(LegendreGaussQuadratureConstants(k).x))' .* LegendreGaussQuadratureConstants(k).w;
endfor
The problem is that it's giving me the error in the title, implying that there is some problem with using a scalar value of k
and that it should match up with the size of values being multiplied, but that makes no sense...
问题是它给了我标题中的错误,这意味着使用标量k有一些问题,它应该与被相乘的值的大小相匹配,但这没有意义……
1 个解决方案
#1
2
You said it yourself in your question
这是你自己提的问题
the roots are found in the column vector
LegendreGaussQuadratureConstants(n).x
. I'm taking the transpose because I want to perform element by element multiplication on the weights, which are stored in the row vectorLegendreGaussQuadratureConstants(n).w
.根是在列向量勒让德中队(n).x中发现的。我取转置是因为我想对权重进行元素乘法运算,这些元素存储在行向量勒让格鲁高斯中队常数(n).w中。
You're taking the element-wise product of two vectors and the result is also going to be a vector. However, you then try to assign it to a scalar, Leg(k)
which produces your error.
取两个向量的元素积结果也是一个向量。但是,然后尝试将其赋值给一个标量Leg(k),它会产生错误。
You either need to store these vectors in a 2D version of Leg
你需要将这些向量存储在一个2D版本的腿上。
for k = 1:50
Leg(k,:) = (fx(LegendreGaussQuadratureConstants(k).x))' .* LegendreGaussQuadratureConstants(k).w;
endfor
or perform some other operation on the element-wise multiplication result to get it to be a scalar.
或者对元素相乘的结果执行其他操作,使其成为标量。
for k = 1:50
Leg(k) = sum((fx(LegendreGaussQuadratureConstants(k).x))' .* LegendreGaussQuadratureConstants(k).w);
endfor
#1
2
You said it yourself in your question
这是你自己提的问题
the roots are found in the column vector
LegendreGaussQuadratureConstants(n).x
. I'm taking the transpose because I want to perform element by element multiplication on the weights, which are stored in the row vectorLegendreGaussQuadratureConstants(n).w
.根是在列向量勒让德中队(n).x中发现的。我取转置是因为我想对权重进行元素乘法运算,这些元素存储在行向量勒让格鲁高斯中队常数(n).w中。
You're taking the element-wise product of two vectors and the result is also going to be a vector. However, you then try to assign it to a scalar, Leg(k)
which produces your error.
取两个向量的元素积结果也是一个向量。但是,然后尝试将其赋值给一个标量Leg(k),它会产生错误。
You either need to store these vectors in a 2D version of Leg
你需要将这些向量存储在一个2D版本的腿上。
for k = 1:50
Leg(k,:) = (fx(LegendreGaussQuadratureConstants(k).x))' .* LegendreGaussQuadratureConstants(k).w;
endfor
or perform some other operation on the element-wise multiplication result to get it to be a scalar.
或者对元素相乘的结果执行其他操作,使其成为标量。
for k = 1:50
Leg(k) = sum((fx(LegendreGaussQuadratureConstants(k).x))' .* LegendreGaussQuadratureConstants(k).w);
endfor