How can I fix this "Error using / Matrix dimensions must agree."
1 view (last 30 days)
Show older comments
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));
0 Comments
Accepted Answer
Dyuman Joshi
on 22 May 2023
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))
0 Comments
More Answers (1)
Sania Nizamani
on 22 May 2023
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.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!