Question

# Matlab-How can i exit the for loop when counts exceeds the max value, then continue to...

Matlab-How can i exit the for loop when counts exceeds the max value, then continue to the last loop and plot accepted values.

counts = 0;
for k = 2:length(partDiameter)
overlaps = 1;
while overlaps == 1
counts = counts + 1;
if counts >= 10000
disp('Max particles packed')
inCoord = [inCoord;[x,y]];
return;
end
x = (partDiameter(k)-L)*rand(1,1) + (L-partDiameter(k)/2);
y = (partDiameter(k)-L)*rand(1,1) + (L-partDiameter(k)/2);

% check if this x,y postion overlaps with previously packed
% partices
for m = 1:length(inCoord(:,1))
% distance between particles
realDist = sqrt((x-inCoord(m,1))^2 + (y-inCoord(m,2))^2);
overDist = partDiameter(m)/2 + partDiameter(k)/2;
if counts >= 10000
disp('Max particles packed')
inCoord = [inCoord;[x,y]];
continue;
end
if (realDist < overDist)
overlaps = 1;
break;
elseif (realDist >= overDist && m == length(inCoord(:,1)))
overlaps = 0;
inCoord = [inCoord;[x,y]];
solidFrac = sum(pi*(partDiameter(m)/2).^2)/L;

end
end
end

end
% Plot particles
figure(2)
hold on
for k = 1:length(partDiameter)
rectangle('Position',...
[inCoord(k,1)-partDiameter(k)/2, inCoord(k,2)-partDiameter(k)/2,...
partDiameter(k),partDiameter(k)],...
'Curvature', [1,1],...
'FaceColor','b',...
'EdgeColor','k')
end
xlim([0,L])
ylim([0,L])
axis square

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

Brother use break statement to get out of the loop immediately. It will exit the first for loop and will execute other remaining statements.

counts = 0;
for k = 2:length(partDiameter)
overlaps = 1;
while overlaps == 1
counts = counts + 1;
if counts >= 10000
disp('Max particles packed')
inCoord = [inCoord;[x,y]];
break;
end
x = (partDiameter(k)-L)*rand(1,1) + (L-partDiameter(k)/2);
y = (partDiameter(k)-L)*rand(1,1) + (L-partDiameter(k)/2);

% check if this x,y postion overlaps with previously packed
% partices
for m = 1:length(inCoord(:,1))
% distance between particles
realDist = sqrt((x-inCoord(m,1))^2 + (y-inCoord(m,2))^2);
overDist = partDiameter(m)/2 + partDiameter(k)/2;
if counts >= 10000
disp('Max particles packed')
inCoord = [inCoord;[x,y]];
break;
end
if (realDist < overDist)
overlaps = 1;
break;
elseif (realDist >= overDist && m == length(inCoord(:,1)))
overlaps = 0;
inCoord = [inCoord;[x,y]];
solidFrac = sum(pi*(partDiameter(m)/2).^2)/L;

end
end
end

end
% Plot particles
figure(2)
hold on
for k = 1:length(partDiameter)
rectangle('Position',...
[inCoord(k,1)-partDiameter(k)/2, inCoord(k,2)-partDiameter(k)/2,...
partDiameter(k),partDiameter(k)],...
'Curvature', [1,1],...
'FaceColor','b',...
'EdgeColor','k')
end
xlim([0,L])
ylim([0,L])
axis square

Kindly revert for any queries

Thanks.

#### Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
• ### How can I get my while loop to run until my condition in my if statement...

How can I get my while loop to run until my condition in my if statement is met? (In MATLAB) clc%clears screen clear all%clears history close all%closes all files format long %Euler's Method %Initial conditions and setup %Projectile motion x=3;%randi(3); y=3;%randi(3); figure('color','white'); plot(x,y,'bo'); hold on wind = (-10 + 20*rand(1)); fprintf('The wind speed is %2.2i\n',wind); v0= input('Initial velocity(m/s): '); a= input('Angle of projection(in degrees): '); a=a*pi/180; h=.01; cd=.1; %cdy=.1; %cdx=(rand*2-1)/2; %cdy=(rand*2-1)/2; m=1.5; g=9.8; %acceleration due to gravity in m/s^2 %td=2*v0*sin(a)/g;...

• ### Hello, i have this matlab code where i have to track a red ball from and...

Hello, i have this matlab code where i have to track a red ball from and uploaded video, and plot its direction in x-y direction My Question is how can i add x vs time plot and y vs time plot in this code. Thank you if exist('V','var') delete(V); end clear;clc; V=VideoReader('video.mp4'); figure(1); x=[];y=[]; i=1; while hasFrame(V) J=readFrame(V); figure(1);imshow(J);title(['Frame No. ',num2str(i)]); if (i>=28 && i<=132) bw=J(:,:,1)>200 & J(:,:,2)<80 & J(:,:,3)<80; bw=imfill(bw,'holes'); A=regionprops(bw,'Centroid'); x(end+1)=A.Centroid(1); y(end+1)=A.Centroid(2); hold on; plot(x,y,'*k');hold off; end i=i+1;...

• ### function [Rx] = matrixTransform(R) % MOD10TRANSFORM is a function which will visualise the effect of your...

function [Rx] = matrixTransform(R) % MOD10TRANSFORM is a function which will visualise the effect of your % 2-by-2 transformation matrix R on a set of grid points. It will also plot % the eigenvectors of the matrix R if there are two real eigenvectors. close all; minX = -10; maxX = 10; minY = -10; maxY = 10; n = 10; pad = 5; corners = [minX, maxX, minX, maxX; maxY, maxY, minY, minY]; corners = [corners, R*corners]; corners =...

• ### Hello. I need help modifying this MatLab code. This is a basic slider-crank animation. the current...

Hello. I need help modifying this MatLab code. This is a basic slider-crank animation. the current program stops the animation and then has the crank move backward. I need the crank to instead to move in full 360 degrees circular motion. Exactly like how a slide-crank mechanism works in real life. thank you and here is the code. %%Code Begins Here%% L1=0.2; L2=0.45; W=0.5; tt=linspace(0,15); for i=1:length(tt)     x2=@(t) L2+L1*sin(W*t);     y2=0;     X2=x2(tt(i));     Y2=y2;     sol= fsolve(@(x,x2,y2) root2d(x,X2,Y2),...

• ### How can I make these equations into 6 first order equations to input them in MATLAB...

How can I make these equations into 6 first order equations to input them in MATLAB as : function yp = ivpsys_fun_oscillator(t, y) % IVPSYS_FUN evaluates the right-hand-side of the ODE's. % Inputs are current time and current values of y. Outputs are values of y'. % Call format: yp = ivpsys_fun_oscillator(t, y) global m k l %% Define ODE for IVP % Reduce high order derivative to first order % y1 -> x1, y2 -> x2, y3 -> x3...

• ### I need this program in MATLAB with different or another code. INPUT: x = [0 1...

I need this program in MATLAB with different or another code. INPUT: x = [0 1 8 12 27]; y = [1 2 3 4 5]; nx = length(x); ny = length(y); n = length(x); if nx ~= ny display('Error. La cantidad de datos en x no es igual que los datos en y') end Sx = sum(x); Sxx = sum(x.^2); Sy = sum(y); Sxy = sum(x.*y); a1 = (n*Sxy - Sx*Sy)/(n*Sxx-(Sx)^2); a0 = (Sxx*Sy - Sxy*Sx)/(n*Sxx - (Sx)^2); m...

• ### help me please , By using MATLAB How can I plot the solution of linear diffrential...

help me please , By using MATLAB How can I plot the solution of linear diffrential system ? I want the code in general case We were unable to transcribe this imageclose all clear MATLAB Code: clc A= [1 0 1; 0 0 0;00-1]; % Coefficient Matrix Xo = [5 76]'; % Initial condition V, D = eig(A); % Get the Eigen values and Eigen vectors % General Solution: X=Sum of {c_i*exp(L_i*t) *V_i % where c_i are constants, L_i are...

• ### I have all of the answers to this can someone just actually explain this matlab code and the results to me so i can get a better understanding? b) (c) and (d) %% Matlab code %% clc; close all; clear...

I have all of the answers to this can someone just actually explain this matlab code and the results to me so i can get a better understanding? b) (c) and (d) %% Matlab code %% clc; close all; clear all; format long; f=@(t,y)y*(1-y); y(1)=0.01; %%%% Exact solution [t1 y1]=ode45(f,[0 9],y(1)); figure; plot(t1,y1,'*'); hold on % Eular therom M=[32 64 128]; T=9; fprintf(' M Max error \n' ); for n=1:length(M) k=T/M(n); t=0:k:T; for h=1:length(t)-1 y(h+1)=y(h)+k*f(t(h),y(h)); end plot(t,y); hold on %%%...

• ### NB! This task is required to be solved in matlab. this task also requires the use...

NB! This task is required to be solved in matlab. this task also requires the use of the function displayDualSpectrum(); which i have pasted in the bottom. the tasks that i need help with are A), B) and C). this is a multi-part question. Task - Frequency mixing We use a basic signal that can be described mathematically as follows: with this We shall then make an amplitude modulated signal: where fc is the carrier frequency. the code below specifies...

• ### please provide matlab solution too 3. Butterball recommends the following cooking times for turkeys at 325...

please provide matlab solution too 3. Butterball recommends the following cooking times for turkeys at 325 °C. size, (lbs) un-stuffed t, (h) stuffed t, (h) 2.00 2.25 6. 2.50 2.75 10 3.00 3.50 18 3.50 4.50 22 4.00 5.00 24 4.50 5.50 30 5.00 6.25 (a) Plot the recommended cooking time as a function of turkey size for un-stuffed and stuffed turkeys on the same plot. (b) For each of the two menu options, find the third-order interpolating polynomial (by...