How can I fix this "Error using / Matrix dimensions must agree."

1 次查看(过去 30 天)
Matlab gives me an error that says "error using / matrix dimensions must agree." "error in Q_bc = (1/n) * (B/ (B + 2*h_bc)).*(h_bc.^(2/3)).*(S.^(1/2))"
how can I fix this can you help me? thank you.
% Step 1: Define the parameters and initial conditions.
B = 200; % River width (m)
n = 0.035; % Manning coefficient
S = 10^(-4); % Bottom slope
Q_eq = 800; % Equilibrium flow rate (m3/s)
h_eq = 4; % Equilibrium water depth (m)
h_basin = 6.4; % Basin water level to start filling (m)
A_basin = 5108; % Basin surface area (m2)
x_upstream = 450; % Location of boundary condition (km)
T = 6; % Period of sinusoidal wave (days)
h_wave = 4; % Height of sinusoidal wave (m)
% Step 2: Set up the numerical grid.
L = 400000; % Length of the river (m)
dx = 1000; % Grid spacing (m)
dt = 3600*24; % Time step (s)
x = 0:dx:L; % Spatial grid
t = 0:dt:T*24*3600; % Time grid
nx = length(x);
nt = length(t);
h = zeros(nx, nt); % Matrix to store water depths
h(:, 1) = h_eq; % Set initial condition as equilibrium depth
% Step 3: Apply the boundary condition.
h_bc = h_eq + h_wave*sin(2*pi*t/T); % Water level at the boundary
% Convert the boundary water levels to discharges using Manning equation
Q_bc = (1/n) * (B/ (B + 2*h_bc)).*(h_bc.^(2/3)).*(S^(1/2));

采纳的回答

Dyuman Joshi
Dyuman Joshi 2023-5-22
I assume you want to do element-wise division, use "./" for that
% Step 1: Define the parameters and initial conditions.
B = 200; % River width (m)
n = 0.035; % Manning coefficient
S = 10^(-4); % Bottom slope
Q_eq = 800; % Equilibrium flow rate (m3/s)
h_eq = 4; % Equilibrium water depth (m)
h_basin = 6.4; % Basin water level to start filling (m)
A_basin = 5108; % Basin surface area (m2)
x_upstream = 450; % Location of boundary condition (km)
T = 6; % Period of sinusoidal wave (days)
h_wave = 4; % Height of sinusoidal wave (m)
% Step 2: Set up the numerical grid.
L = 400000; % Length of the river (m)
dx = 1000; % Grid spacing (m)
dt = 3600*24; % Time step (s)
x = 0:dx:L; % Spatial grid
t = 0:dt:T*24*3600; % Time grid
nx = length(x);
nt = length(t);
h = zeros(nx, nt); % Matrix to store water depths
h(:, 1) = h_eq; % Set initial condition as equilibrium depth
% Step 3: Apply the boundary condition.
h_bc = h_eq + h_wave*sin(2*pi*t/T); % Water level at the boundary
% Convert the boundary water levels to discharges using Manning equation
% v here
Q_bc = (1/n) * (B./ (B + 2*h_bc)).*(h_bc.^(2/3)).*(S^(1/2))
Q_bc = 1×7
0.6923 0.6923 0.6923 0.6923 0.6923 0.6923 0.6923

更多回答(1 个)

Sania Nizamani
Sania Nizamani 2023-5-22
The last line should read as follows:
Q_bc = (1/n) * (B./ (B + 2*h_bc)).*(h_bc.^(2/3)).*(S^(1/2));
You had missed the dot there. You must have typed (B./ instead of (B/
I hope it helps.

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by