Code is running continuously, but never ends
Show older comments
h_s=[1 2 5 6];
h_d = [3 4 8 6];
MA = [3.5 1.5 2.5];
D_d = 0.0254*[1.25 1.75 2.25];
D_s = [0.03175 0.04445 0.05715];
D_p = 0.0254*[2.5 3.5 4.5];
L = [0.10 0.18 0.16];
for k1 = 1:numel(h_s) % suction head m
for k2 = 1:numel(h_d) % delivery head m
for k3 = 1:numel(MA) % mechanical advantage
for k4 = 1:numel(D_d) %delivery pipe diameter
for k5 = 1:numel(D_s) %suction pipe diamter...0.0254*[1.25 1.75 2.25]
for k6 = 1:numel(D_p) %piston diameter m
for k7 = 1:numel(L)% height of cylinder stoke
y=[0.15 0.094]
%yv = linspace(min(y), max(y), 10);
v0 = 0.01;
m =64.5;
x = 0.7;
b = 0.625;
a = 0.250;
g = 9.8;
[A_d(k4,:)] = (D_d(k4)^2)*3.14*0.25;
for a1= 1:numel(A_d)
[A_s(k5,:)] = (D_s(k5)^2)*3.14*.25;
for a2= 1:numel(A_s)
l_d = 4.572;
l_s = 6.096;
[A_p(k6,:)] = ((D_p(k6))^2)*3.14*0.25;
for a3= 1:numel(A_p)
m_p = 0.2;
rho = 1000;
uk = 0.06;
uf = 0.4;
up = 0.00089;
h = 0.001;
t_w= 0.05;
[A_w(k6,:)] = t_w*D_p(k6)*3.14;
for a4= 1:numel(A_w)
[z(k3,:)]=(0.2946*MA(k3)-0.1761);
for a5= 1:numel(z)
M_tr= 2;
L_tr=0.5;
P=173.7;
R_pin=0.05;]
[M_T(k7,k3,a1,a3,a5,:)]=2*m_p*(A_d(a1)/A_p(a3))^2+(L(k7)+l_d+l_s)*rho*((A_d(a1))^2/A_p(a3))+(2*M_tr*L_tr^2/a^2)+(z(a5))^2*MA(k3)^2*m*(A_d(a1)/A_p(a3))^2;
for a6= 1:numel(M_T)
[c0(a3,a1,a6,:)]=((1-2*x)*A_p(a3)*P/(A_d(a1)*M_T(a6)));
disp(c0);
for t0 = 1:numel(c0)
[c1(k1,k2,k7,k3,a2,a1,a3,a6,:)]=(rho*g*((h_s(k1))*A_s(a2)+(h_d(k2))*A_d(a1)+(L(k7))*A_p(a3))+z(+1)*m*g*MA(k3))/M_T(a6);
for t1 = 1:numel(c1)
[c2(a3,a6,:)]=(2*rho*g*A_p(a3))/M_T(a6);
for t2 = 1:numel(c2)
[c3(a1,a6,a3,k7,:)]=(2*uf*b+25.12*up*L(k7)*h)*A_d(a1)/(h*M_T(a6)*A_p(a3));
for t3 = 1:numel(c3)
[c4(k4,k5,a1,a2,a6,:)]=(0.124*rho^0.75*up^0.25*[l_d*D_d(k4)^0.25+l_s*D_s(k5)^0.25]*(A_d(a1)/A_s(a2))^1.75)/M_T(a6);
for t4 = 1:numel(c4)
[c5(a1,a2,a3,a6,:)]=rho*[((A_d(a1))^2/A_s(a2))*[0.5+0.5+0.2+0.4+0.5]+0.5*((A_d(a1))^2/A_p(a3))+A_d(a1)*(0.5+0.4+0.2+1)+0.5*((A_d(a1))^2/A_p(a3))]/(2*M_T(a6));%please check the constants.
for t5 = 1:numel(c5)
[y,v(t0,t1,t2,t3,t4,t5,:)] = ode45((@(y,v)((c0(t0)/v^2)-(c1(t1)/v)-(c2(t2)*y/v)-c3(t3)-c4(t4)*v^0.75-c5(t5)*v)) ,y ,v0);
%disp(v,y) % displays y and x_velocity
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
%% I am not able to get output and the code is not showing any error.
%% It is going to run for long time.
%% Please, upload this into your matlab tool, then you can have idea of problem. Please, tell me how to proceed.
6 Comments
Walter Roberson
on 2 Sep 2019
The line
R_pin=0.05;]
is invalid.
Adam
on 2 Sep 2019
How long is 'continuously'? You have 19 nested for loops, unless I miscounted! That's not going to be especially performant!
You can stick a breakpoint in while code is running to see where it currently is. If you do this initially in the deepest for loop, after some amount of time you will get an idea how long it is taking. Or you could use
doc waitbar
around the outer loop to also get an idea of how long it is taking.
SUSHANT CHAVAN
on 2 Sep 2019
KALYAN ACHARJYA
on 2 Sep 2019
Edited: KALYAN ACHARJYA
on 2 Sep 2019
Adam suggested to create a waitbar
Walter Roberson
on 2 Sep 2019
Go to the MATLAB command line and give the command
doc waitbar
and read what shows up.
SUSHANT CHAVAN
on 2 Sep 2019
Answers (1)
Guillaume
on 2 Sep 2019
As others have said, you need to debug the code you've written, using the various tools that matlab offer. waitbar is one, you could also use the debugger to step through your problem line by line.
I would suspect that the code is not even doing what you meant it to do. As Adam said you have an extremely large number of nested loops and some of them don't make much sense. For example, we have
%inside the k1, k2, k3, ..., k6, k7 loops
[A_d(k4,:)] = (D_d(k4)^2)*3.14*0.25;
for a1= 1:numel(A_d)
%...
Note that this code is inside the k5, k6 and k7 loops, yet doesn't depend at all on these indices. So for a start the whole calculation will be repeated max(k5)*max(k6)*max(k7) times each time yielding the exact same result. Certainly a waste of time, but not the biggest problem.
The right hand side of the equation is always going to be scalar, so the : on the left hand side is misleading. It's equivalent to 1. We also have some unnecessary [] brackets. None of this is a problem, but it makes me wonder if something was intended all along.
The biggest problem is that A_d grows at each step of the k4 loop. When k4 is 1, A_d is only one element, then when k4 is 2, you add a new element as A_d(2), etc. So, the number of steps that the a1 loop is going to take depends on the current k4. I'm fairly certain that was not the intent.
6 Comments
SUSHANT CHAVAN
on 2 Sep 2019
Guillaume
on 2 Sep 2019
It's not clear to me how many independent variables you have. If the only independent variables are just: h_s, h_d, MA, D_d, D_s, D_p and L (so 7 variables, then you should have just 7 loops. (or just one loop with a different design). The A_d, etc., if dependent should not trigger another loop, you use the same index as used for the variable that depends to.
In addition, there's no reason to calculate A_d inside the loop, you can calculate all values at once before any loop, it's simply:
A_d = D_d .^ 2 * pi / 4; %use pi instead of 3.14, it's more accurate. Note the use of .^ since D_d is a vector
%same for A_S and probably others I haven't looked at.
If I understood, correctly what you're trying to do, the code should be:
%independent variables
h_s = [1, 2, 5, 6];
h_d = [3, 4, 8, 6];
MA = [3.5, 1.5, 2.5];
%etc. all the variables to loop over
%constants, they shouldn't be in the loops since they're constant.
v0 = 0.01;
m = 64.5;
x = 0.7;
%etc. they're peppered all over your code, it's a mess.
%dependent variables
A_d = D_d .^ 2 * pi / 4; %will be indexed by k4
A_s = D_s .^ 2 * pi / 4; %will be indexed by k5
%etc.
%preallocation of results, 7-D arrays
M_T = zeros(numel(h_s), numel(h_d), numel(MA), ...etc for all 7 variables);
c0 = M_T; %or repeat the zeros lines above
%etc. all the output variables
%the loops
for k1 = 1:numel(h_s) % suction head m
for k2 = 1:numel(h_d) % delivery head m
for k3 = 1:numel(MA) % mechanical advantage
for k4 = 1:numel(D_d) %delivery pipe diameter
for k5 = 1:numel(D_s) %suction pipe diamter...0.0254*[1.25 1.75 2.25]
for k6 = 1:numel(D_p) %piston diameter m
for k7 = 1:numel(L)% height of cylinder stoke
M_T(k1, k2, k3, k4, k5, k6, k7) = 2*m_p * (A_d(k4)/A_p(k6))^2 + (L(k7)+l_d+l_s)*rho*((A_d(k4))^2/A_p(k6))+(2*M_tr*L_tr^2/a^2)+(z(k3))^2*MA(k3)^2*m*(A_d(k4)/A_p(k6))^2;
%etc. everything uses k1 to k7
end
end
end
end
end
end
end
Another option is to ndgrid all your inputs and just use a linear index:
%create 7-d arrays:
[h_s, h_d, MA, D_d, D_s, D_p, L] = ndgrid([1, 2, 4, 5], ...suction head m
[3, 4, 8, 6], ...delivery head m
[3.5, 1.5, 2.5], ...mechanical advantage
0.0254*[1.25, 1.75, 2.25], ...delivery pipe diameter
[0.03175, 0.04445, 0.05715], ...suction pipe diameter
0.0254*[2.5, 3.5, 4.5], ...piston diameter
[0.10, 0.18, 0.16]); %stroke
%constants as before
v0 = 0.01;
m =64.5;
x = 0.7;
%etc.
%dependent variables as before:
A_d = D_d.^2 * pi/4;
%etc.
%no loop needed for M_T and other variables. Calculate everything at once as 7_d array
%need to use dotted operators when dealing with matrices
M_T = 2*m_p*(A_d./A_p).^2 + (L+l_d+l_s)*rho.*(A_d.^2./A_p) + 2*M_tr*L_tr^2/a^2 +z.^2.*MA.^2*m.*(A_d./A_p).^2;
%etc. for the c variables
%preallocate v
v = zeros(size(h_s)); %as a 7_d variable
%loop over each element using linear indexing
for k = 1:numel(v)
[~, v(k)] = ode45(@(y,v) c0(k)/v^2 - c1(k)/v - c2(k)*y/v -c3(k) - c4(k)*v^0.75 - c5(k)*v, y , v0);
end
SUSHANT CHAVAN
on 2 Sep 2019
SUSHANT CHAVAN
on 3 Sep 2019
Edited: Guillaume
on 3 Sep 2019
Guillaume
on 3 Sep 2019
"I want to select different interval for each loop"
For y? If so, simply define y inside the loops as
y = linspace(0, L(k7), 20);
However if y is a vector, then your if y < ... and if y > ... (and what happens when y = ... ?) don't make any sense.
But more importantly, I don't understand what the purpose of y is. Your ode is supposed to solve
but the y you've created is not used by the ode (it's a completely different y variable internal to the solver). If the value of c1 is supposed to change within the ode depending on the y internal to the solver then you'll have to completely change the ode design.
SUSHANT CHAVAN
on 3 Sep 2019
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!