- assign to FT(i,:) or FT(:,i) so that each iteration you are recording one entry for every t value, into a 2D array (you might even want to go 3D if T is to be independent of H; OR
- assign to FT{i} instead of FT(i): cell arrays can hold multiple values for each index location; OR
- if the situation is mathematically suitable, use some kind of summarizing to transform the vector of values into a single value; for example sum() or var()
Help with for loops
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Trying to run this loop for 3 different varibles for H and T, i have it working to find the different forces of a wave for one scienario but cant figure out the loop to run the other two scienarios (H,T change). Ive attached the code below.
%%
g=9.807;
d=30;
H=[4 2 7];
T=[11 7 20];
D=1;
rho=1025;
l=length(H);
for i=[1:l]
% dispersion relation to find wave length
[ka,L] = dispersion_relation(T(i),d);
k=((2*pi)./L);
w=((2.*pi)./T(i));
H_D=H(i)./D;
PiD=(pi.*D)./L;
% Calculating Orobital Velocity
Vmax=((g.*H(i))./(2.*w)).*k;
%KC
KC=(Vmax.*T(i))./D;
%kinematic velocity
v=1.004.*10^-6;
%reynolds number
Re=(Vmax.*D)./v;
%due to wave forces on slender cylinder falling in V must calculate
%Intertia and drag force.
%assume Cm to be 1.755
%assume Cd to be 0.8
Cd=0.8; %Must change each example
Cm=1.755; %Must change this each example
t=linspace(0,22);
a=(H(i)./2);
f=(1/T(i));
y=a.*sin(2.*pi.*f.*t);
u=gradient(y);
u_dot=gradient(u);
FT(i)=-Cm.*((pi.*rho.*D^2.*w^2.*H(i))./(8.*k)).*sin(w.*t)+Cd.*(rho.*D.*w^2.*H(i)^2)./(16.*sinh(k.*d)^2).*(((sinh(2.*k.*d))./(2.*k))+d).*cos(w.*t).*abs(cos(w.*t));
Fi(i)=-Cm.*((pi.*rho.*D^2.*w^2.*H)./(8.*k)).*sin(w.*t);
Fd(i)=Cd.*(rho.*D.*w^2.*H(i)^2)./(16.*sinh(k.*d)^2).*(((sinh(2.*k.*d))./(2.*k))+d).*cos(w.*t).*abs(cos(w.*t));
subplot(3,1,1)
plot(t,Fi)
xlabel('t')
ylabel('F_inertia')
subplot(3,1,2)
plot(t,Fd)
xlabel('t')
ylabel('F_drag')
subplot(3,1,3)
plot(t,FT)
xlabel('t')
ylabel('F_Total')
end
0 Commenti
Risposte (1)
Walter Roberson
il 18 Mar 2022
l=length(H);
for i=[1:l]
% dispersion relation to find wave length
[ka,L] = dispersion_relation(T(i),d);
That always uses corresponding H and T values. If you want the vectors to be independent then use another for loop such as
for i = 1 : length(H)
for iT = 1 : length(T)
[ka,L] = dispersion_relation(T(iT),d);
the other stuff
end
end
Depending exactly what you wanted to do, you might end up assigning to variables indexed at (i,iT) instead of at (i)
t=linspace(0,22);
That is a vector.
FT(i)=-Cm.*((pi.*rho.*D^2.*w^2.*H(i))./(8.*k)).*sin(w.*t)+Cd.*(rho.*D.*w^2.*H(i)^2)./(16.*sinh(k.*d)^2).*(((sinh(2.*k.*d))./(2.*k))+d).*cos(w.*t).*abs(cos(w.*t));
The right hand side uses the entire t vector in multiple places, so the right hand side is going to be a vector. But you are assigning the vector to a scalar location. You should do one of the following:
4 Commenti
Walter Roberson
il 21 Mar 2022
Fi(i, :) = -Cm.*((pi.*rho.*D^2.*w^2.*H(i))./(8.*k)).*sin(w.*t);
You were missing the (i) for the H
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!