简体   繁体   English

Octave:For循环错误表示A(I)= X:X必须与I具有相同的大小

[英]Octave: For-loop error says A(I) = X: X must have the same size as I

I need to evaluate the integral from x=-1 to x=1 for the function x*e^(x)*sin(pi*x) . 我需要为函数x*e^(x)*sin(pi*x)计算从x=-1x=1的积分。 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 . 该方法可以概括为从( k=1k=n其中n=30 (weights)*(function evaluated at given roots)(weights)*(function evaluated at given roots)的总和。

In my code below, the roots are found in the column vector LegendreGaussQuadratureConstants(n).x . 在我的下面的代码中,根在列向量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 我正在进行转置,因为我想对权重进行逐元素乘法,这些权重存储在行向量LegendreGaussQuadratureConstants(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存在一些问题,并且它应该与乘以的值的大小相匹配,但这没有意义......

You said it yourself in your question 你在问题中自己说过

the roots are found in the column vector LegendreGaussQuadratureConstants(n).x . 根在列向量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 . 我正在进行转置,因为我想对权重进行逐元素乘法 ,权重存储在行向量LegendreGaussQuadratureConstants(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 您需要将这些矢量存储在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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在八度的x轴上绘制日期:“错误:__plt2vv__:向量长度必须匹配” - Ploting dates on x-axis in octave: “error: __plt2vv__: vector lengths must match” 我很难找到一个代码来估计 sin(x) 在 x=pi/4 处的二阶导数,h^-1 在八度音阶 - I struggle to find a code to estimate the second derivative of sin(x) at x=pi/4 with h^-1 in octave 如何从以八度为单位的2 x 2矩阵中减去1x2矩阵的值? - How do I subtract the values of a 1x2 matrix from a 2 x 2 matrix in octave? 为什么我在 OCTAVE 中收到 ode23s 的“x”未定义错误,但没有收到 ode23 或 ode15s 的错误? - Why do i receive an 'x' undefined error with ode23s but not with ode23 or ode15s in OCTAVE? OCTAVE-ind2rgb:X必须是索引图像 - OCTAVE - ind2rgb: X must be an indexed image 使用词干时出错(第 43 行)X 的长度必须与 Y 相同 - Error using stem (line 43) X must be same length as Y 使用词干 X 的错误必须与 Y 的长度相同 - Error using stem X must be same length as Y 倍频矢量循环:对于i = 1:size(v,1)v(i,y(i))= 1; 结束 - Octave vectorize loop: for i = 1:size(v,1) v(i,y(i)) = 1; end 尝试积分时出现八度错误:“被积函数 F 必须返回与输入大小相同的单个实值向量” - Octave error when trying to integrate: "integrand F must return a single, real-valued vector of the same size as the input" 在Octave / Matlab中使用for循环生成矢量 - Using for-loop to generate vectors in Octave/Matlab
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM