plotting for loop answers with changing variables

1 次查看(过去 30 天)
Hello
I am running into an issue with some code I am writing for a class, I have defined the mathematical aspect of the code, which gives me the answer I need for on simulation, but how would I go about plotting the same code but variables changing in step intervals? I am new to indexing and I believe that may be the answer to my question but I am unsure how to execute it in this example.
I want to re run the code for KC to start from 2 to 20 in 2 step intervals to compare results and overlay them for my power plot, and run another simulation where I keep KC constant but change one of my other constants to an interval in the same fashion and overlay results etc, the problem is I do not know if I need to have multiple for loops or just an indexing line?
%% Variables
KC = 2
pi = acos(-1);
Um = 1.6;
T = 1.8 %(KC*D)/Um;
D = 0.16;
L = 1;
m = 50;
rho = 1024;
K = 200;
c = 100;
CA = 1;
CD = 1.8;
omega = 2*pi/T;
md = 1;
Ap = D*L;
dt = T/40;
ndt = T/dt*5;
X(1:ndt+1) = 0;
V(1:ndt+1) = 0;
%% Governing equations
time(1:ndt+1) = (0:ndt)*dt;
P(1:ndt+1)= 0;
Pavg=0;
Kc=(Um*T)/D;
for n=1:ndt,
%for KC = 2:2:20,
% Calculation of kx1 and kv1
ta= time(n);
aw= omega*Um*cos(omega*ta)-(2*Um*omega*cos(omega*ta))/3;
Vw= Um*sin(omega*ta)- (Um/3)*sin(2*omega*ta);
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);
%% Calculation of kx2 and kv2
ta= time(n)+dt/2;
aw= omega*Um*cos(omega*ta)-(2*Um*omega*cos(omega*ta))/3;
Vw= Um*sin(omega*ta)- (Um/3)*sin(2*omega*ta)
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);
%% Calculation of kx3 and kv3
ta= time(n)+dt/2;
aw= omega*Um*cos(omega*ta)-(2*Um*omega*cos(omega*ta))/3;
Vw= Um*sin(omega*ta)-(Um/3)*sin(2*omega*ta);
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);
%% Calculation of kx4 and kv4
ta= time(n)+dt;
aw= omega*Um*cos(omega*ta)-(2*Um*omega*cos(omega*ta))/3; % acceleration of the fluid
Vw= Um*sin(omega*ta)- (Um/3)*sin(2*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);
%% Step Equations
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);
P(n+1)= c*(V(n)^2);
Pavg= P(n)+(1/ndt)*P(n);
end %end
%% Plot of cylinder displacement, velocity and power output
% X Displacement plot
figure (1);
plot(time(1:ndt),X(1:ndt),'-g');
xlabel('time (s)');
ylabel('X (m)');
title('X displacment');
legend ('RK method');
% V Velocity plot
figure (2);
plot(time(1:ndt),V(1:ndt),'-b');
xlabel('time (s)');
ylabel('V (m/s)');
title('V Velocity');
legend ('RK method');
% P Power plot
figure (3);
plot (time(1:ndt),P(1:ndt),'-b');
xlabel('time (s)');
ylabel('P (W)');
title('P Power');
legend ('R-K method');
hold;
After writing the code I am now confused on what to do since the second for loop I have implemented doesnt seeem to do anything and the results are the same so I am confused as to why or how to actually get it to do what I want it to?
note: I took out the second loop and replaced the variable KC with a constant to get one answer.
  3 个评论
Muamin Sulaiman
Muamin Sulaiman 2021-4-28
I renamed Kc to KC, and i am receiving different results everytime I change the variable in line 2 now.
Could I maybe vector the KC value to acheive what I want? or maybe start a for loop at line 2 and end it at the end of the original for loop?
Thanks for bringing my attention to the parameters being named differently, that helped me :)
I will have to research storing array results and plotting them, I dont believe I have done that before.
DGM
DGM 2021-4-28
Your results are already vectors, so if they're all the same length each time, just concatenate them into a 2D array.
If they're not the same length, you'd have to use a cell array or take a different approach.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by