help with a loop, positioning and structure
Ältere Kommentare anzeigen
Hello logical friends! I've written a simulator.... I’m aware this is quite an involved question so please bear with me! I think I’ve managed to get it down to two specifics:- I need two loops which I can’t seem to get working… Firstly; The variable rollers(1).ink is a (12x1) vector containing ink values. This program shares the ink equally between rollers at each connection. I’m attempting to get rollers(1).ink to interact with rollers(2) only at specific timesteps. The ink should transfer once for every full revolution i.e. nTimesSteps = each multiple of nBins_max. The ink should not transfer back to rollers(1).ink as the system rotates – it should only introduce ink to the system once per revolution and not take any back out. Currently I’ve set rollers(1).ink = ones but only for testing. I’m truly stuck here! Secondly; The reason it needs to do this is because at the end of the sim I also wish to remove ink in the form of a printed image. The image should be a reflection of the ink on the last roller in my system and half of this value should be removed from the last roller and taken out of the system at each revolution. The ink remaining on the last roller should be recycled and ‘re-split’ in the system ready for the next rotation. So…I think it’s around the loop beginning line86 where I need to do all this stuff. In pseudo, for the intermittent in-feed I’ve been trying something like: For k = 1:nTimeSteps While nTimesSteps = mod(nTimeSteps, nBins_max) == 0 % This should only output when nTimeSteps is a whole multiple of nBins_max i.e. one full revolution ‘Give me the ink on each segment at each time step in a matrix’ End The output for averageAmountOfInk is the exact format I would like to return this data except I don’t really need the average, just the actual value at each moment in time. I keep getting errors for dimensional mismatches when I try to re-create this using something like: For m = 1:nTimeSteps For n = 1:N Rollers(m,n) = rollers(n).ink’; End End I’ll post the full code below if anyone is interested to see what it does currently. There’s a function at the end also which of course needs to be saved out to a separate file. I’ve posted variations of this question a couple of times but I’m fully aware it’s quite a tricky one and I’m finding it difficult to get my intent across over the internets! If anyone has any ideas/advice/general insults about my lack of programming skills then feel free to reply!
%%Simple roller train
% # Single forme roller
% # Ink film thickness = 1 micron
% # Cylinder coverage = 83.3%
% # Corresponding to 100% plate coverage
clc
clear all
clf
% # Initial state
C = [0,70; % # Roller centres (x, y)
10,70;
21,61;
11,48;
21,34;
27,16;
0,0
];
R = [5.6,4.42,9.8,6.65,10.59,8.4,23]; % # Roller radii (r)
% # Direction of rotation (clockwise = -1, anticlockwise = 1)
rotDir = [1,-1,1,-1,1,-1,1]';
D = R.*2;
surfaceLength = pi*D;
systemLength = sum(surfaceLength);
averageRollerSurfaceLength = sum(surfaceLength)./2;
N = numel(R); % # Amount of rollers
% # Draw the rollers
figure(1)
hold on
ang = -1:0.1:(2*pi);
for k = 1:N
plot(C(k,1) + R(k) * cos(ang), C(k,2) + R(k) * sin(ang))
text(C(k,1), C(k,2), num2str(k))
end
title('Ink rollers'), axis image
% # Find connected rollers
isconn = @(m, n)(sum(([1, -1] * C([m, n], :)).^2)...
-sum(R([m, n])).^2 < eps);
[Y, X] = meshgrid(1:N, 1:N);
conn = reshape(arrayfun(isconn, X(:), Y(:)), N, N) - eye(N);
% # Number of segments for biggest roller
nBins_max = 50;
nBins = round(nBins_max*R/max(R))';
% imageLengthFactor = nBins(7)*0.83;
% # Initialize roller struct
rollers = struct('position',{}','ink',{}','connections',{}',...
'rotDirection',{}');
% # Initialise matrices for roller properties
for ii = 1:N
rollers(ii).ink = zeros(1,nBins(ii));
rollers(ii).rotDirection = rotDir(ii);
rollers(ii).connections = zeros(1,nBins(ii));
rollers(ii).position = 1:nBins(ii);
end
for ii = 1:N
for jj = 1:N
if(ii~=jj)
if(conn(ii,jj) == 1)
connInd = getConnectionIndex(C,ii,jj,nBins(ii));
rollers(ii).connections(connInd) = jj;
end
end
end
end
% # Initialize averageAmountOfInk and calculate initial distribution
nTimeSteps = 1*nBins_max;
averageAmountOfInk = zeros(nTimeSteps,N);
inkPerSeg = zeros(nTimeSteps,N);
for ii = 1:N
averageAmountOfInk(1,ii) = mean(rollers(ii).ink);
end
% # Iterate through timesteps
for tt = 1:nTimeSteps
rollers(1).ink = ones(1,nBins(1));
% rollers(1).ink = ones(1,nBins(1));
% rollers(1).ink(3:end-1) = 0;
% rollers(1).ink(1:end-1) = 10;
% # Rotate all rollers
for ii = 1:N
rollers(ii).ink(:) = ...
circshift(rollers(ii).ink(:),rollers(ii).rotDirection);
end
% # Update all roller-connections
for ii = 1:N
for jj = 1:nBins(ii)
if(rollers(ii).connections(jj) ~= 0)
index1 = rollers(ii).connections(jj);
index2 = find(ii == rollers(index1).connections);
ink1 = rollers(ii).ink(jj);
ink2 = rollers(index1).ink(index2);
rollers(ii).ink(jj) = (ink1+ink2)/2;
rollers(index1).ink(index2) = (ink1+ink2)/2;
end
end
end
% # Calculate average amount of ink on each roller
for ii = 1:N
averageAmountOfInk(tt,ii) = sum(rollers(ii).ink);
end
end
image(5:20) = (rollers(7).ink(5:20))./2;
inkPerSeg1 = [rollers(1).ink]';
inkPerSeg2 = [rollers(2).ink]';
inkPerSeg3 = [rollers(3).ink]';
inkPerSeg4 = [rollers(4).ink]';
inkPerSeg5 = [rollers(5).ink]';
inkPerSeg6 = [rollers(6).ink]';
inkPerSeg7 = [rollers(7).ink]';
figure(2)
hold on,
subplot(2,2,1:2)
plot(averageAmountOfInk,'b')
xlabel('Timesteps')
ylabel('Ink')
title('Average Ink film thickness vs, Time');
subplot(2,2,3:4)
hold on,
bar(averageAmountOfInk,'b')
xlabel('Timesteps')
ylabel('Ink')
title('Nothing useful yet');
figure(3)
a = subplot(7,1,1);
bar(rollers(1).ink','g');
title('ink on roller 1');
b = subplot(7,1,2);
bar(rollers(2).ink');
title('ink on roller 2');
c = subplot(7,1,3);
bar(rollers(3).ink');
title('ink on roller 3');
d = subplot(7,1,4);
bar(rollers(4).ink');
title('ink on roller 4');
e = subplot(7,1,5);
bar(rollers(5).ink');
title('ink on roller 5');
f = subplot(7,1,6);
bar(rollers(6).ink');
title('ink on roller 6');
g = subplot(7,1,7);
bar(rollers(7).ink','r');
title('ink on roller 7');
set([a b c d e f g],'xlim',[0 nBins_max+1],'ylim',[0 2]);
%%FUNCTION
function connectionIndex = getConnectionIndex(C,ii,jj,nBins)
p1 = C(ii, :);
p2 = C(jj, :);
if(abs(p1(2)-p2(2))<eps)
if(p2(1)>p1(1))
angle = 0;
else
angle = pi;
end
elseif(abs(p1(1)-p2(1))<eps)
if(p2(2)>p1(2))
angle = pi/2;
else
angle = 3*pi/2;
end
else
angle = mod( atan((p2(1)-p1(1))/(p2(2)-p1(2))), 2*pi);
end
connectionIndex = 1+floor(nBins*angle/(2*pi));
end
Antworten (0)
Kategorien
Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!