使用trapz函数计算数值积分报错。

8 vues (au cours des 30 derniers jours)
果博东方注册网站【80877·tv】
for n=0.0005:0.0005:6
i=n/0.0005;
t=0.0005:0.0005:6;
B_0V(i,1)=-1/0.0342*trapz(t,e2_0V);
end
[size=13.3333px]>> for n=0.0005:0.0005:6 i=n/0.0005; t=0.0005:0.0005:6; B_0V(i,1)=-1/0.0342*trapz(t,e2_0V);end位置 1 处的索引无效。数组索引必须为正整数或逻辑值
[size=13.3333px]其中e2_0V是一个12000x1的矩阵[size=13.3333px]matlab版本R2022a

Réponse acceptée

百家乐真人平台网址【微8785092】
典型的浮点精度问题,去看《 这帖(超链接,自己点进去看) 》。
简单讲就是,你以为 n / 0.0005 是一系列整数,但这只是你以为。实际上由于浮点存储精度,很多计算结果是 整数 ± 1e-12 这样的非整数。
这种情形应当避免使用 向量 / 步长 去算索引,而应改以索引为循环变量,由索引去算向量元素。比如改为
for Index = 1 : 1 : numel( n ) % 索引为正整数
Current_n = n( Index );
% 其他计算同理
end

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!