Help with for loops

4 vues (au cours des 30 derniers jours)
William Harris
William Harris le 18 Mar 2022
Commenté : Walter Roberson le 21 Mar 2022
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

Réponses (1)

Walter Roberson
Walter Roberson le 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:
  1. 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
  2. assign to FT{i} instead of FT(i): cell arrays can hold multiple values for each index location; OR
  3. 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()
  4 commentaires
William Harris
William Harris le 18 Mar 2022
function [ka,L] = dispersion_relation(T,d)
%UNTITLED8 Summary of this function goes here
% Detailed explanation goes here
g=9.81;
w=2*pi./T;
if length(d)==1
d=d*ones(size(T));
end
L=T;
for i=1:length(T)
Tl = T(i);
dl = d(i);
wl=w(i);
f=@(k) wl.^2-g.*k.*tanh(k.*dl);
% deep water approx
k_DW=wl.^2/g;
%shallow water approx
k_SW=wl./sqrt(g.*dl);
kl=fzero(f,k_DW);
ka=abs(kl);
L(i)=2.*pi./ka;
end
Walter Roberson
Walter Roberson le 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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by