Indexing Arrays within a for loop
34 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Please help. I am trying to index values of the Vc array at a specific point that correspond to the potion in the t array. The t and Vc arrays are a (size) long and will be remade everytime for 100 iterations to fill up the A array.
%% Creating Initial Population
A = zeros(1,100);
R = zeros(1,100);
C = zeros(1,100);
Vs = zeros(1,100);
tc = zeros(1,100);
dt=0.01;
t=0:dt:10;
n=size(t);
Vc=zeros(1,n(2));
Vc(1)=0;
A(1)=0;
for i = 1:1:100
R= 20000 + (50000-20000)*rand(1);
C= 0.0001 +(0.001-0.0001)*rand(1);
Vs= 5 +(20-5)*rand(1);
tc= 1+(8-2)*rand(1);
dt=0.01;
t=0:dt:10;
n=size(t);
Vc=zeros(1,n(2));
Vc(1)=0;
A(1)=0;
%% 4 Euler's Charging and Discharging
limit = round(tc/dt);
for i=2:limit+1
Vc(i)=Vc(i-1)+(eps/(R*C)-Vc(i-1)/(R*C))*dt;
end
%Discharge
for i=(limit+2):n(2)
Vc(i)=Vc(i-1)+(-Vc(i-1)/(R*C))*dt;
end
%% create below
%% the index values of the Vc array at a specific index that corresponds to the t array
%% the t and Vc arrays are fixed numbers long and will be remade everytime for 100 iterations in order to fill up the A Array
A(i+1) = ( Vc(t=0)-0)^2 + (Vc(t=2)-3.27)^2 + (Vc(t=4)-5.79)^2 + (Vc(t=6)-7.70)^2 + (Vc(t=8)-6.64)^2 + (Vc(t=10)-5.09)^2;
end
2 Kommentare
Antworten (1)
Walter Roberson
am 18 Mär. 2021
%% Creating Initial Population
A = zeros(1,100);
R = zeros(1,100);
C = zeros(1,100);
Vs = zeros(1,100);
tc = zeros(1,100);
dt=0.01;
t=0:dt:10;
n=size(t);
Vc=zeros(1,n(2));
Vc(1)=0;
A(1)=0;
for i = 1:1:100
R= 20000 + (50000-20000)*rand(1);
C= 0.0001 +(0.001-0.0001)*rand(1);
Vs= 5 +(20-5)*rand(1);
tc= 1+(8-2)*rand(1);
dt=0.01;
t=0:dt:10;
n=size(t);
Vc=zeros(1,n(2));
Vc(1)=0;
A(1)=0;
%% 4 Euler's Charging and Discharging
limit = round(tc/dt);
for i=2:limit+1
Vc(i)=Vc(i-1)+(eps/(R*C)-Vc(i-1)/(R*C))*dt;
end
%Discharge
for i=(limit+2):n(2)
Vc(i)=Vc(i-1)+(-Vc(i-1)/(R*C))*dt;
end
%% create below
%% the index values of the Vc array at a specific index that corresponds to the t array
%% the t and Vc arrays are fixed numbers long and will be remade everytime for 100 iterations in order to fill up the A Array
A(i+1) = ( Vc(t==0)-0)^2 + (Vc(t==2)-3.27)^2 + (Vc(t==4)-5.79)^2 + (Vc(t==6)-7.70)^2 + (Vc(t==8)-6.64)^2 + (Vc(t==10)-5.09)^2;
end
nnz(A)
A(i+1)
4 Kommentare
Walter Roberson
am 21 Mär. 2021
format long g
%% Creating Initial Population
A = zeros(1,100);
R = zeros(1,100);
C = zeros(1,100);
Vs = zeros(1,100);
tc = zeros(1,100);
dt=0.01;
t=0:dt:10;
n=size(t);
Vc=zeros(1,n(2));
Vc(1)=0;
A(1)=0;
for i = 1:1:100
R= 20000 + (50000-20000)*rand(1);
C= 0.0001 +(0.001-0.0001)*rand(1);
Vs= 5 +(20-5)*rand(1);
tc= 1+(8-2)*rand(1);
dt=0.01;
t=0:dt:10;
n=size(t);
Vc=zeros(1,n(2));
Vc(1)=0;
A(1)=0;
%% 4 Euler's Charging and Discharging
limit = round(tc/dt);
for j=2:limit+1
Vc(j)=Vc(j-1)+(eps/(R*C)-Vc(j-1)/(R*C))*dt;
end
%Discharge
for j=(limit+2):n(2)
Vc(j)=Vc(j-1)+(-Vc(j-1)/(R*C))*dt;
end
%% create below
%% the index values of the Vc array at a specific index that corresponds to the t array
%% the t and Vc arrays are fixed numbers long and will be remade everytime for 100 iterations in order to fill up the A Array
A(i+1) = ( Vc(t==0)-0)^2 + (Vc(t==2)-3.27)^2 + (Vc(t==4)-5.79)^2 + (Vc(t==6)-7.70)^2 + (Vc(t==8)-6.64)^2 + (Vc(t==10)-5.09)^2;
end
nnz(A)
A(1:10)
With the simple fix of the loop variable names, you no longer get the 173.whatever only as the last entry: you now get it as all entries except the first.
Vc(1:10)
max(Vc)
Observe that your Vc values are tiny. With them being so tiny, when you do the (Vc(t==2)-3.27) and so on, they are round-off error compared to the value you are subtracting, so they are effectively all 0, and therefore all of the results will be the same to within round-off error.
max(diff(A(2:end)))
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!