フィルターのクリア

Index in position 1 exceeds array bounds. Index must not exceed 1.

5 ビュー (過去 30 日間)
Thai An
Thai An 2024 年 6 月 3 日
コメント済み: Thai An 2024 年 6 月 3 日
I have a piece of code, but I don't know how to fix the error
Index in position 1 exceeds array bounds. Index must not exceed 1.
Error in Homework_8_GFDM (line 33)
h_t(j,i) = h_t(j,i-1) - (t_step/x(j)) * (Q_t(j,i-1)/A(j,i-1) - Q_t(j-1, i-1)/A(j-1,i-1));
clear; close all;
% Input parameters
b = 6.1; % Channel bottom width (m)
z = 1.5; % Side slope (H:V)
Q = 126; % Flow rate (m^3/s)
h = 5.79; % Flow depth (m)
S0 = 0.00008; % Channel bed slope
n = 0.013; % Manning's roughness coefficient
L = 5000; % Channel length (m)
t_end = 2000; % Calculation time (s)
t_step = 1; % Time step (s)
% Initial parameter calculations
A = b*h + z*h^2; % Cross-sectional area (m^2)
P = b + 2*h*sqrt(1+z^2); % Wetted perimeter (m)
R = A/P; % Hydraulic radius (m)
V = Q/A; % Flow velocity (m/s)
Fr = V/sqrt(9.81*h); % Froude number
Sf = n^2 * V^2 / R^(4/3); % Energy slope
% Transient condition calculations
x = linspace(0, L, 101);
t = 0:t_step:t_end;
h_t = zeros(length(x), length(t));
Q_t = zeros(length(x), length(t));
h_t(:,1) = h;
Q_t(:,1) = Q;
for i = 2:length(t)
for j = 2:length(x)-1
if j > 1
h_t(j,i) = h_t(j,i-1) - (t_step/x(j)) * (Q_t(j,i-1)/A(j,i-1) - Q_t(j-1,i-1)/A(j-1,i-1));
else
h_t(j,i) = h_t(j,i-1) - (t_step/x(j)) * (Q_t(j,i-1)/A(j,i-1) - 0/A(j,i-1));
end
if j < length(x)-1
Q_t(j,i) = Q_t(j,i-1) - (t_step/x(j)) * (9.81*A(j,i-1)*(h_t(j+1,i-1) - h_t(j-1,i-1))/(2*x(j)) + 9.81*A(j,i-1)*S0 - Sf(j,i-1)*Q_t(j,i-1)^2/A(j,i-1)^2);
else
Q_t(j,i) = 0; % Flow rate is zero at the end point
end
end
h_t(1,i) = h_t(2,i); % Boundary condition at upstream
Q_t(1,i) = Q_t(2,i);
h_t(end,i) = h_t(end-1,i) - (t_step/x(end)) * (Q_t(end,i-1)/A(end,i-1) - Q_t(end-1,i-1)/A(end-1,i-1)); % Boundary condition at downstream
Q_t(end,i) = 0; % Flow rate is zero at the end point
end
Index in position 1 exceeds array bounds. Index must not exceed 1.
% Plotting graphs
figure;
plot(x/1000, h_t(:,1), 'b-', x/1000, h_t(:,51), 'r-', x/1000, h_t(:,101), 'g-', x/1000, h_t(:,151), 'c-', x/1000, h_t(:,201), 'm-');
legend('t = 0 s', 't = 500 s', 't = 1000 s', 't = 1500 s', 't = 2000 s');
xlabel('Distance (km)'); ylabel('Flow depth (m)');
title('Flow depth variation along the channel');
figure;
plot(t, h_t(15,1:end), 'b-', t, h_t(25,1:end), 'r-', t, h_t(30,1:end), 'g-', t, h_t(50,1:end), 'c-');
legend('x = 1.5 km', 'x = 2.5 km', 'x = 3 km', 'x = 5 km');
xlabel('Time (s)'); ylabel('Flow depth (m)');
title('Flow depth variation over time');

回答 (1 件)

Torsten
Torsten 2024 年 6 月 3 日
移動済み: Torsten 2024 年 6 月 3 日
A is a single value, namely
A = b*h + z*h^2; % Cross-sectional area (m^2)
But in your double loop, you treat A as if it were a matrix of size [length(x),length(t)].
  1 件のコメント
Thai An
Thai An 2024 年 6 月 3 日
yes, thank you so much, because i just start simulate with matlab in two days ago

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by