why do i get error "Index exceeds matrix dimensions" in this code?

p=1;
v_d(1)=0;
f_p=[];
t = 0:0.000001:1;
f=2.5;
v = 1*sin(2*pi*t*f);
d = 9*10^(-9);
j=2;
u_v=30*10^-15;
r_on=0.1*10^3;
r_off=16*10^3;
r_i=11000;
w(1)=((r_off-r_i)/(r_off-r_on))*d;
x(1)=w(1)/d;
m(1)=r_on*(w(1)/d)+r_off*(1-w(1)/d);
% f_p(1)= 1 - (2*x-1)^(2*p); %Joglekar window
% f_p(1)=j*(1 - ((x-0.5)^2+0.75)^p); %Prodromakis window
for index=2:length(t)
i(index)=v(index)/m(index-1);
v_d(index)=(u_v*r_on*i(index)*f_p(index-1))/d;
w(index)=v_d(index)*(t(index)-t(index-1))+w(index-1);
x(index)=w(index)/d;
% f_p(index)=1 - (2*x(index)-1)^(2*p); %Joglekar window
% f_p(index)=j*(1 - ((x(index)-0.5)^2+0.75)^p); %Prodromakis window
m(index)=r_on*(w(index)/d)+r_off*(1-w(index)/d);
if m(index)<r_on
m(index)=r_on;
end
if m(index)>r_off
m(index)=r_off;
end
x(index)=w(index)/d;
end
Index exceeds matrix dimensions.

1 个评论

v_d(index)=(u_v*r_on*i(index)*f_p(index-1))/d;
% ^^^ this is empty, any non-empty index will throw that error.

请先登录,再进行评论。

回答(1 个)

From my understanding of the question you are getting index error due to f_p(index-1). Since initially you have defined f_p as empty array and you have commented both the updating part of f_p (for Joglekar/Prodromakis Window) you are getting index error. You have to enable atleast one updating part of f_p i.e. Joglekar/Prodromakis Window. Check the following code when I use Joglekar Window:
p=1;
v_d(1)=0;
f_p=[];
t = 0:0.000001:1;
f=2.5;
v = 1*sin(2*pi*t*f);
d = 9*10^(-9);
j=2;
u_v=30*10^-15;
r_on=0.1*10^3;
r_off=16*10^3;
r_i=11000;
w(1)=((r_off-r_i)/(r_off-r_on))*d;
x(1)=w(1)/d;
m(1)=r_on*(w(1)/d)+r_off*(1-w(1)/d);
f_p(1)= 1 - (2*x-1)^(2*p); %Joglekar window (Uncomment this line)
%f_p(1)=j*(1 - ((x-0.5)^2+0.75)^p); %Prodromakis window
for index=2:length(t)
i(index)=v(index)/m(index-1);
v_d(index)=(u_v*r_on*i(index)*f_p(index-1))/d;
w(index)=v_d(index)*(t(index)-t(index-1))+w(index-1);
x(index)=w(index)/d;
f_p(index)=1 - (2*x(index)-1)^(2*p); %Joglekar window (Uncomment this line)
% f_p(index)=j*(1 - ((x(index)-0.5)^2+0.75)^p); %Prodromakis window
m(index)=r_on*(w(index)/d)+r_off*(1-w(index)/d);
if m(index)<r_on
m(index)=r_on;
end
if m(index)>r_off
m(index)=r_off;
end
x(index)=w(index)/d;
end

类别

帮助中心File Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by