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

조회 수: 3 (최근 30일)
Sena
Sena 2023년 5월 22일
답변: Sania Nizamani 2023년 5월 22일
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