Calculate mean from for loop and plotting means for separate for loop

I need to plot average power against each KC value. Can anyone please help?
vector_of_KC_values=[2,4,6,8,10,12,14,16,18,20]
for column_number=1:length(vector_of_KC_values)
KC=vector_of_KC_values(column_number)
pi = acos(-1); % pi = 3.14159
Um = 1; % Fluid Velicity amplitude
D = 0.1; % cylinder diamter
L = 1; % cylinder length
m = 50; % cylinder mass
rho = 1024; % fluid density
K = 200; % spring stiffness
c = 100; % damping constant
CA = 1; % added mass coefficient
CD = 1.8; % drag coefficient
omega = 2*pi/T; % angular frequency of the flow
md = rho*pi*(D*D)/4*L; % added mass
Ap = D*L; % projected area
T=(KC*D)/Um
dt = T/40; % time step
ndt = T/dt*10; % %total step to be calculated
X(1:ndt+1) = 0; % save displacement X from step 0 to step ndt
V(1:ndt+1) = 0;
Power(1:ndt+1) = 0;
time(1:ndt+1) = (0:ndt)*dt;
for n=1:ndt
% to calculate kx1 and kv1
ta = time(n);
aw = omega*Um*cos(omega*ta); % acceleration of the fluid
Vw = Um*sin(omega*ta); % velocity of the fluid
Va = V(n);
Xa = X(n);
t1 = CA*md*aw;
t2 = 0.5*rho*CD*Ap*abs(Vw-Va)*(Vw-Va);
t3 = -K*Xa-c*Va;
kx1 = V(n);
kv1 = (t1+t2+t3)/(m+CA*md);
% to calculate kx2 and kv2
ta = time(n)+dt/2;
aw = omega*Um*cos(omega*ta); % acceleration of the fluid
Vw = Um*sin(omega*ta); % velocity of the fluid
Va = V(n)+0.5*dt*kv1;
Xa = X(n)+0.5*dt*kx1;
t1 = CA*md*aw;
t2 = 0.5*rho*CD*Ap*abs(Vw-Va)*(Vw-Va);
t3 = -K*Xa-c*Va;
kx2 = V(n)+0.5*dt*kv1;
kv2 = (t1+t2+t3)/(m+CA*md);
% to calculate kx3 and kv3
ta = time(n)+dt/2;
aw = omega*Um*cos(omega*ta); % acceleration of the fluid
Vw = Um*sin(omega*ta); % velocity of the fluid
Va = V(n)+0.5*dt*kv2;
Xa = X(n)+0.5*dt*kx2;
t1 = CA*md*aw;
t2 = 0.5*rho*CD*Ap*abs(Vw-Va)*(Vw-Va);
t3 = -K*Xa-c*Va;
kx3 = V(n)+0.5*dt*kv2;
kv3 = (t1+t2+t3)/(m+CA*md);
% to calculate kx4 and kv4
ta = time(n)+dt;
aw = omega*Um*cos(omega*ta); % acceleration of the fluid
Vw = Um*sin(omega*ta); % velocity of the fluid
Va = V(n)+0.5*dt*kv3;
Xa = X(n)+0.5*dt*kx3;
t1 = CA*md*aw;
t2 = 0.5*rho*CD*Ap*abs(Vw-Va)*(Vw-Va);
t3 = -K*Xa-c*Va;
kx4 = V(n)+dt*kv3;
kv4 = (t1+t2+t3)/(m+CA*md);
% next step values
X(n+1) = X(n)+(dt/6)*(kx1+2*kx2+2*kx3+kx4);
V(n+1) = V(n)+(dt/6)*(kv1+2*kv2+2*kv3+kv4);
Power(n+1)=c*V(n+1)^2;
end
Avg_power=mean(Power(n+1))
figure (04);
hold on
plot(KC,Avg_power,'-r');
xlabel(KC);
ylabel('Average Power (W)');
legend (Average Power for different KC values');
hold off
end

Answers (1)

There was an out-of-order statement (‘T’), no subscripting of the ‘Avg_power’ vector, and the wrong independent variable argument in the plot call. I also put the plot call outside the loops.
Try this —
vector_of_KC_values=[2,4,6,8,10,12,14,16,18,20]
vector_of_KC_values = 1×10
2 4 6 8 10 12 14 16 18 20
for column_number=1:length(vector_of_KC_values)
KC=vector_of_KC_values(column_number)
pi = acos(-1); % pi = 3.14159
Um = 1; % Fluid Velicity amplitude
D = 0.1; % cylinder diamter
L = 1; % cylinder length
m = 50; % cylinder mass
rho = 1024; % fluid density
K = 200; % spring stiffness
c = 100; % damping constant
CA = 1; % added mass coefficient
CD = 1.8; % drag coefficient
T=(KC*D)/Um
omega = 2*pi/T; % angular frequency of the flow
md = rho*pi*(D*D)/4*L; % added mass
Ap = D*L; % projected area
% T=(KC*D)/Um
dt = T/40; % time step
ndt = T/dt*10; % %total step to be calculated
X(1:ndt+1) = 0; % save displacement X from step 0 to step ndt
V(1:ndt+1) = 0;
Power(1:ndt+1) = 0;
time(1:ndt+1) = (0:ndt)*dt;
for n=1:ndt
% to calculate kx1 and kv1
ta = time(n);
aw = omega*Um*cos(omega*ta); % acceleration of the fluid
Vw = Um*sin(omega*ta); % velocity of the fluid
Va = V(n);
Xa = X(n);
t1 = CA*md*aw;
t2 = 0.5*rho*CD*Ap*abs(Vw-Va)*(Vw-Va);
t3 = -K*Xa-c*Va;
kx1 = V(n);
kv1 = (t1+t2+t3)/(m+CA*md);
% to calculate kx2 and kv2
ta = time(n)+dt/2;
aw = omega*Um*cos(omega*ta); % acceleration of the fluid
Vw = Um*sin(omega*ta); % velocity of the fluid
Va = V(n)+0.5*dt*kv1;
Xa = X(n)+0.5*dt*kx1;
t1 = CA*md*aw;
t2 = 0.5*rho*CD*Ap*abs(Vw-Va)*(Vw-Va);
t3 = -K*Xa-c*Va;
kx2 = V(n)+0.5*dt*kv1;
kv2 = (t1+t2+t3)/(m+CA*md);
% to calculate kx3 and kv3
ta = time(n)+dt/2;
aw = omega*Um*cos(omega*ta); % acceleration of the fluid
Vw = Um*sin(omega*ta); % velocity of the fluid
Va = V(n)+0.5*dt*kv2;
Xa = X(n)+0.5*dt*kx2;
t1 = CA*md*aw;
t2 = 0.5*rho*CD*Ap*abs(Vw-Va)*(Vw-Va);
t3 = -K*Xa-c*Va;
kx3 = V(n)+0.5*dt*kv2;
kv3 = (t1+t2+t3)/(m+CA*md);
% to calculate kx4 and kv4
ta = time(n)+dt;
aw = omega*Um*cos(omega*ta); % acceleration of the fluid
Vw = Um*sin(omega*ta); % velocity of the fluid
Va = V(n)+0.5*dt*kv3;
Xa = X(n)+0.5*dt*kx3;
t1 = CA*md*aw;
t2 = 0.5*rho*CD*Ap*abs(Vw-Va)*(Vw-Va);
t3 = -K*Xa-c*Va;
kx4 = V(n)+dt*kv3;
kv4 = (t1+t2+t3)/(m+CA*md);
% next step values
X(n+1) = X(n)+(dt/6)*(kx1+2*kx2+2*kx3+kx4);
V(n+1) = V(n)+(dt/6)*(kv1+2*kv2+2*kv3+kv4);
Power(n+1)=c*V(n+1)^2;
end
Avg_power(column_number)=mean(Power(n+1));
end
KC = 2
T = 0.2000
KC = 4
T = 0.4000
KC = 6
T = 0.6000
KC = 8
T = 0.8000
KC = 10
T = 1
KC = 12
T = 1.2000
KC = 14
T = 1.4000
KC = 16
T = 1.6000
KC = 18
T = 1.8000
KC = 20
T = 2
figure (04);
hold on
plot(vector_of_KC_values,Avg_power,'.-r');
xlabel(KC);
ylabel('Average Power (W)');
legend ('Average Power for different KC values', 'Location','best');
hold off
.

2 Comments

My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

Sign in to comment.

Categories

Find more on Fluid Dynamics in Help Center and File Exchange

Asked:

on 12 May 2023

Commented:

on 12 May 2023

Community Treasure Hunt

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

Start Hunting!