Including codes in a for loop that would store values after each iteration

1 view (last 30 days)
Hello everyone, I am fairly new to matlab so please bear with me although its a fairly fundemental for loop question. The simulink model you see above simulates two robots that try to form a formation thus they exchange their position informations (p1,p2). They robot 1 does its calculations in "compute_u1" where it outputs an updated p1 value, similar with robot 2. Both position vectors p1 and p2 are in the form of [x-coordinate y-coordinate] therefore their sizes are (1x2). Yet since a simulation is ran with a stop time of 40 and a step time of 0.01 the final sizes of each vectors becomes a (4001x2) so, p1-->(4001x2) similarly p2-->(4001x2).
Now I have this for loop:
for n = 1:sample
thisfig = figure();
p1_0 = rand(2,1)*10-5;
p2_0 = rand(2,1)*10-5;
simOut = sim('hetero1d1b');
alldata = simOut.ScopeDataHetero1d1b;
time = alldata.time;
data = alldata.signals;
p1 = data(1).values;
p2 = data(2).values;
x1_total(:,n) = p1(:,1);
y1_total(:,n) = p1(:,2);
x2_total(:,n) = p2(:,1);
y2_total(:,n) = p2(:,2);
end;
basically I ran the simulink model simulation, import the data obtained from it and split the p1 and p2 values into x1,y1,x2,y2 values. But since I have (:,n) on them each variable stores its values after each iteration so if I ran the simulink model with sample=3 I would get x1_total with a size (4001x3).
My problem:
There are serious of calculations which I want to calculate per iteration and I want to be able to store all the values of these calculations so that I can average them out after simulating my models for sample= 3. These calculations are as follows:
alphad2 = 240; %constant value that doesnt get updated
desireddistance = sqrt((p2(:,1)-p1(:,1)).^2+(p2(:,2)-p1(:,2)).^2);
g12 = (p2(:,:)-p1(:,:))./desireddistance;
g12_star = [cosd(alphad2) sind(alphad2)]; %constant value that doesnt get updated
G12_star = ones(4001,2).* g12_star; %constant value that doesnt get updated
e12b = g12-G12_star
e12bnorm = sqrt(sum(e12b.^2,2))
How can I encorporate these equations in my for loop so that if I ran my simulation for example for n=3 I would have "desireddistance", "g12", "e12b" and "e12bnorm" values store their values per each iteration??? I tried many different versions but either I get a size error or some other error...
I know it's a pretty long one but it's a pretty important matter to me at the moment and I didn't want anyone to have misunderstandings while reading it!

Answers (1)

Ishank Agarwal
Ishank Agarwal on 3 Aug 2021
Try this:
sample = input('Enter the number of times you want to run the simulation: ');
g12 = zeros(sample, 1); % preallocation of memory as per sample size
e12b = zeros(sample, 1); % preallocation of memory as per sample size
e12bnorm = zeros(sample, 1); % preallocation of memory as per sample size
alphad2 = 240; %constant value that doesnt get updated
g12_star = [cosd(alphad2) sind(alphad2)]; %constant value that doesnt get updated
G12_star = ones(4001,2).* g12_star; %constant value that doesnt get updated
desireddistance = sqrt((p2(:,1)-p1(:,1)).^2+(p2(:,2)-p1(:,2)).^2);
for i = 1:sample
g12(i, 1) = (p2(:,:)-p1(:,:))./desireddistance;
e12b(i, 1) = g12-G12_star
e12bnorm(i, 1) = sqrt(sum(e12b.^2,2))
end
Thanks

Categories

Find more on Simulink in Help Center and File Exchange

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!