Indexing Arrays within a for loop

36 views (last 30 days)
Christopher Jarrett
Christopher Jarrett on 18 Mar 2021
Commented: Walter Roberson on 21 Mar 2021
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 Comments
KSSV
KSSV on 18 Mar 2021
What problem you have with the give code? What is the question?
Christopher Jarrett
Christopher Jarrett on 18 Mar 2021
Using the for loop which computates the random varables R,Vs,tc,C to generate the Vc, I need to create the 100 variable long array A. I don't know why the A is coming out to be a single scalar of 173 and not an array with a bunch of different values that would correspond to the randomness of the 4 random variables.
This is part of an optimization problem where the best variables need to be sorted and selected to eventually get an A value that is as close to zero as possible. Help is appreciated.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 18 Mar 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)
ans = 1
A(i+1)
ans = 173.5047
  4 Comments
Christopher Jarrett
Christopher Jarrett on 21 Mar 2021
Right. Also, I didn't properly debug before posting this question.I tried using J for the 100 iter loop instead of i which (you pointed out) is iterating the inner loops but I still get the same problem. Just an 100 long array with 173.5 as the last value. Even after the changes, I'm still doing something wrong.
Walter Roberson
Walter Roberson on 21 Mar 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)
ans =
100
A(1:10)
ans = 1×10
0 173.5047 173.5047 173.5047 173.5047 173.5047 173.5047 173.5047 173.5047 173.5047
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)
ans = 1×10
0 4.8145595822274e-20 9.62807523078306e-20 1.44405471720216e-19 1.92519756322483e-19 2.40623608377198e-19 2.88717030146434e-19 3.36800023891774e-19 3.84872591874309e-19 4.32934736354643e-19
max(Vc)
ans =
2.62425694204567e-17
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)))
ans =
0

Sign in to comment.

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!