How can I fix this "Error using / Matrix dimensions must agree."
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));
Accepted Answer
More Answers (1)
Sania Nizamani
on 22 May 2023
1 vote
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.
Categories
Find more on Programming 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!