Subscript indices must either be real positive integers or logicals. how can i change? thanks!

1 回表示 (過去 30 日間)
weiying chen
weiying chen 2019 年 10 月 2 日
回答済み: Andrei Bobrov 2019 年 10 月 2 日
dt = 0.1; % time step size in seconds
i0 = 0; % potential at time 0
Vs = 10; % positive time independant source potential
L = 2; % inductor
R = 1; % resistor
N = 10; % number of time steps
i(0) = v0; %initial condition
for loop=1:N %recursive
i(loop) = i(loop-1) + Vs*dt*/L - i(loop-1)*R/L;
end
plot([0:N-1]*dt,v, ‘-k’)
xlabel( ‘Time in seconds’)
ylabel( ‘Inductor current’)
grid on hold time = [0:N-1]*dt;
i_ana = Vs/R*(1- exp(-time*(R/L));
plot(time,I_ana,’–k’)
hold
legend( ‘Numerical solution’, ‘Analytical solution’)
Subscript indices must either be real positive integers or logicals.

回答 (2 件)

Bjorn Gustavsson
Bjorn Gustavsson 2019 年 10 月 2 日
Matlab uses 1-based indexing to vectors, so your line:
i(0) = v0; %initial condition
Has to be changed to:
i(1) = v0; %initial condition
Then you have to adapt the rest of your script to account for that.
HTH

Andrei Bobrov
Andrei Bobrov 2019 年 10 月 2 日
ii(1) = v0; %initial condition
for loop=2:N %recursive
ii(loop) = ii(loop-1) + Vs*dt*/L - ii(loop-1)*R/L;
end

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by