Help with for loops

조회 수: 8 (최근 30일)
William Harris
William Harris 2022년 3월 18일
댓글: Walter Roberson 2022년 3월 21일
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

답변 (1개)

Walter Roberson
Walter Roberson 2022년 3월 18일
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
William Harris
William Harris 2022년 3월 18일
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 2022년 3월 21일
Fi(i, :) = -Cm.*((pi.*rho.*D^2.*w^2.*H(i))./(8.*k)).*sin(w.*t);
You were missing the (i) for the H

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by