How do I fix this error? (Error using / Matrix dimensions must agree.)
조회 수: 1 (최근 30일)
이전 댓글 표시
% Non-steady state flow between two parallel plates of water
% Parameters
rho = 1000; % Density of water in kg/m^3
mu = 0.001; % Viscosity of water in Pa*s
V = 2; % Velocity of the upper plate in m/s
h = 0.1; % Distance between the two plates in m
ll=diff(u);
% Discretize the spatial domain
y = linspace(-h, h, 100);
% Initialize the velocity field
u = zeros(length(y), 1);
% Time step
dt = 0.01;
% Final time
t_final = 10;
% Boundary conditions
u(1) = 0; % No-slip condition at the bottom plate
u(end) = V; % Moving plate condition at the top plate
% Time loop
for t = 0:dt:t_final
% Calculate the diffusion term
diffusion = mu * (diff(u))/diff((y));
% Update the velocity field
u = u + dt * diffusion;
% Enforce the boundary conditions
u(1) = 0;
u(end) = V;
end
% Plot the velocity field
figure
plot(y, u, 'linewidth', 2)
xlabel('y (m)')
ylabel('u (m/s)')
title('Non-steady state flow between two parallel plates of water')
댓글 수: 2
Dyuman Joshi
2023년 12월 14일
편집: Dyuman Joshi
2023년 12월 14일
There seems to be a discrepancy in the code, see above. Is the code snippet ll = diff(u) in the right line?
It's trying to access the variable u before it has been defined.
답변 (1개)
SAI SRUJAN
2023년 12월 21일
Hi Adam,
I understand that you are facing an issue with plotting the velocity field for the non-steady state flow between two parallel plates of water.
The error message "Error using / Matrix dimensions must agree" indicates that you are trying to perform division on two matrices or arrays whose dimensions do not agree with the MATLAB '/' operator. For the operation 'A/B' to be executed in MATLAB, the matrices 'A' and 'B' need to have an identical number of columns.
In the code snippet you provided, the error is occurring in the following line:
diffusion = mu * (diff(u))/diff((y));
The 'diff(u)' function computes the difference between adjacent elements of 'u', which results in a vector that is one element shorter than 'u'. The dimensions of 'diff(u)' are 99 x 1 double whereas the dimensions of 'diff(y)' are 1 x 99 double, this mismatch of the number of columns for 'diff(u)' and 'diff(y)' is causing the error.
To fix the error, you should ensure that you are using division and that the dimensions of the vectors you are dividing match.
Finally, the line 'll=diff(u);' is before 'u' is defined and will throw an "Unrecognized function or variable 'u' " error. You should remove or comment out this line.
For a comprehensive understanding of the "/" operator and "diff" function in MATLAB, please refer to the following documentation.
I hope this helps.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Calculus에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!