这段代码看起来应该很简单,但由于某种原因,我一直收到这样的错误:"位置2的索引无效。数组索引必须是正整数或逻辑值。">
它指的是这些行:
Vr = (V*R)/(sqrt((R^2)+(w*L-(1/(w*C))^2)));
VR(1,i) = Vr;
我试着检查我是否输入了错误的方程,或者我是否必须将某些值保存为整数,但似乎什么都不起作用。如有任何建议,我们将不胜感激。
如果你需要,这里有完整的代码:
disp('Please enter Vo in volts, R in kiloOhms, L in miliHenries, and C in picoFarads');
input = input('Enter values as single matrix. ');
V = input(1,1);
R = input(1,2);
L = input(1,3);
C = input(1,4);
VR = zeros(1,1000);
for i = 0:1000
w=i*10*10^-9;
Vr = (V*R)/(sqrt((R^2)+(w*L-(1/(w*C))^2)));
VR(1,i) = Vr;
end
谢谢!
您的for loop
从零开始,但Matlab
是1-索引语言。
VR = zeros(1,1000);
for i = 1:1000 % <- Mistake was here, change zero to one
w=i*10*10^-9;
Vr = (V*R)/(sqrt((R^2)+(w*L-(1/(w*C))^2)));
VR(1,i) = Vr;
end