I am trying to import excel windspeed data and to multiply each table variables with the vectors within the for loop, how to change for table variables into vector form?
2 views (last 30 days)
Show older comments
clc; clear all; close all;
% Input data;
g = 9.81;
fs = 0.000003;
w_data = readtable("LakeTahoe_v2_wind_velocity_test.xlsx","ReadVariableNames",true);
%w_data only shows 11.2, 8.0, and 8.1
fi=0.001;
fb=0.05;
rdd=0.005;
ev=20.0;
huo=10.0;
hbo=10.0;
Dx=200.0;
Dt=10.0;
nx=50.0;
nt=103680.0;
% Initial conditions;
for k=1:nx-1
hu(k)=huo;
hb(k)=hbo;
hun(k)=huo;
hbn(k)=hbo;
end
for k1=1:nx
zb(k1)=0;
uu(k1)=0;
ub(k1)=0;
uun(k1)=0;
ubn(k1)=0;
end
%I thought about adding another for loop here to run the main program below with the three different answers above
for i=1:nt
% Continuity equation for the upper fluid layer;
for j=2:nx-2
qul(j)=uu(j)*(hu(j)+hu(j-1))/2;
qur(j)=uu(j+1)*(hu(j)+hu(j+1))/2;
hun(j)=hu(j)-(Dt/Dx)*(qur(j)-qul(j));
end
% Continuity equation for the lower fluid layer;
for j=2:nx-2
qbl(j)=ub(j)*(hb(j)+hb(j-1))/2;
qbr(j)=ub(j+1)*(hb(j)+hb(j+1))/2;
hbn(j)=hb(j)-(Dt/Dx)*(qbr(j)-qbl(j));
end
% Updating of layer depth values;
for j=2:nx-2
hu(j)=hun(j);
hb(j)=hbn(j);
end
% Force equilibrium equation for the upper layer;
for j=3:nx-2
hum(j)=(hu(j)+hu(j-1))/2;
ts(j)=fs*w.*abs(w)/hum(j); %wind velocity is here
ti(j)=fi*(uu(j)-ub(j))*abs(uu(j)-ub(j))/hum(j);
ed(j)=ev*(uu(j-1)-2*uu(j)+uu(j+1))/Dx^2;
uun(j)=uu(j)-Dt*g*(hu(j)+hb(j)+zb(j)-hu(j-1)-hb(j-1)-zb(j-1))/Dx+Dt*(ed(j)+ts(j)-ti(j));
end
% Force equilibrium equation for the lower layer;
for j=3:nx-2
hbm(j)=(hb(j)+hb(j-1))/2;
ti(j)=fi*(uu(j)-ub(j))*abs(uu(j)-ub(j))/hbm(j);
tb(j)=fb*ub(j)*abs(ub(j))/hbm(j);
ed(j)=ev*(ub(j-1)-2*ub(j)+ub(j+1))/Dx^2;
ubn(j)=ub(j)-Dt*g*((hu(j)+hb(j)+zb(j)-hu(j-1)-hb(j-1)-zb(j-1))/Dx-rdd*(hu(j)-hu(j-1))/Dx)+Dt*(ed(j)+ti(j)-tb(j));
end
% Updating of fluid velocity values;
for j=3:nx-2
uu(j)=uun(j);
ub(j)=ubn(j);
Hbfinal(j)=hbn(j);
Htotal(j)=hun(j)+hbn(j);
end
Eu=0;
El=0;
for j=3:nx-2
Eu=Eu+(hu(j)+hu(j-1))/2*uu(j)^2;
El=El+(hb(j)+hb(j-1))/2*ub(j)^2;
end
Hupper(i)=hu(nx-2);
Hlower(i)=hb(nx-2);
end
1 Comment
KSSV
on 23 May 2023
The below lines:
for k=1:nx-1
hu(k)=huo;
hb(k)=hbo;
hun(k)=huo;
hbn(k)=hbo;
end
for k1=1:nx
zb(k1)=0;
uu(k1)=0;
ub(k1)=0;
uun(k1)=0;
ubn(k1)=0;
end
can be simply replaced by:
hu=huo*ones(1,nx-1);
hb=hbo*ones(1,nx-1);;
hun=huo*ones(1,nx-1);;
hbn=hbo*ones(1,nx-1);;
zb=zeros(1,nx) ;
uu=zeros(1,nx) ;
ub=zeros(1,nx) ;
uun=zeros(1,nx) ;
ubn=zeros(1,nx) ;
Answers (1)
Sugandhi
on 7 Jun 2023
Hi,
I understand that you want to convert table variables to vector form.
In MATLAB, you can convert a table variable to a vector using the table2array and reshape functions. Here's an example:
% Create a sample table
T = table(rand(3,1), rand(3,1), rand(3,1), 'VariableNames', {'Var1', 'Var2', 'Var3'})
% Convert table to a numerical array
A = table2array(T)
% Reshape array to column vector
v = reshape(A, numel(A), 1)
In the code above, the `table` function is used to create a sample table with 3 rows and 3 variables. The `table2array` function is used to convert the table to a numerical array. The `reshape` function is then used to convert the array into a column vector.
For more understanding, kindly go through following links:
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!