Here is the code which I'm using. They're saying array size is not comptaible. I highlight the problem.
dt = 0.001; % time step
% Define the computational grid
L = 200;
dx = 1;
x = 0:dx:L;
y = 0:dx:L;
[X,Y] = meshgrid(x,y);
% Define the initial temperature distribution and wind vector
u0 = 38*chi2rnd(2, size(X)).*chi2rnd(14, size(X)); % initial temperature
v0 = 0.5 + rand(size(X)).*(-0.4 + 0.4.*randi([0,1], size(X))); % initial fuel distribution
w = [300, 300]; % initial wind vector
% Set up the parameters
alpha = 0.2; % rate of fuel combustion
beta = 0.1; % rate of fuel evaporation
k = 0.1; % rate of heat transfer
rho = 1.2; % density of air
Cp = 1000; % specific heat of air
Tc = 273; % reference temperature
g = [0, -9.81]; % gravitational acceleration
% Define the wind vector function
wind = @(t) w;
wind_new = @(t) [50, 400]; % new wind vector at t = 0.085
% Set up the solver
T = 0.15;
t = 0:dt:T;
U = zeros([size(X), numel(t)]);
V = zeros([size(X), numel(t)]);
U(:,:,1) = u0;
V(:,:,1) = v0;
for n = 2:numel(t)
% Compute the wind vector
if t(n) <= 0.085
W = wind(t(n));
else
W = wind_new(t(n));
end
% Compute the temperature and fuel distributions
Ux = diff(U(:,:,n-1),1,1)/dx;
Uy = diff(U(:,:,n-1),1,2)/dx;
Uxx = diff(U(:,:,n-1),2,1)/dx^2;
Uyy = diff(U(:,:,n-1),2,2)/dx^2;
Vx = diff(V(:,:,n-1),1,1)/dx;
Vy = diff(V(:,:,n-1),1,2)/dx;
% Compute the rates of change
dUdt = k*(Uxx + Uyy) - alpha*V(:,:,n-1).^2 + dot(W, cat(3,Ux, Uy), 3);
dVdt = -beta*V(:,:,n-1).*U(:,:,n-1) + rho*Cp*(Tc - U(:,:,n-1)).*sqrt(Vx.^2 + Vy.^2) ...
+ dot(W, cat(3,Vx, Vy), 3) + dot(g, cat(3,V(:,:,n-1).*Vx, V(:,:,n-1).*Vy), 3);
% Update the temperature and fuel distributions
U(:,:,n) = U(:,:,n-1) + dUdt*dt;
V(:,:,n) = V(:,:,n-1) + dVdt*dt;
end
Arrays have incompatible sizes for this operation.
% Visualize the results
figure;
subplot(1,2,1);
imagesc(U(:,:,end));
title('Temperature distribution');
colorbar

댓글 수: 4

Uxx is 199x201
Uyy is 201x199
alpha*V(:,:,n-1) is 201x201
W is 301x301
k and alpha are 1x1 scalars
Therefore, in line 52 (from your image), the following fail (working from inside-to-out)
  • cat(3,Ux, Uy) - Ux and Uy need to be the same size, you probably just need to transpose one of them
  • Uxx + Uyy - you probably just need to transpose one of them
  • k*(Uxx + Uyy) - alpha*V(:,:,n-1).^2 - the first term k*(__) and the second term alpha*V(__) need to be the same size or one can be a vector with a length equal to the size of one dimension of the other term.
To investigate run this command from the command window and then run your code. It will pause in debug mode when it reaches the error. Investigate how your variables are computed, what size should they be, and what is preventing them from being that size.
dbstop if error
To turn this off,
dbclear if error
Zamer Chaudhary
Zamer Chaudhary 2023년 3월 1일
I didn't undersatnd sir. Can you please rewrite my code with correction?
Zamer Chaudhary
Zamer Chaudhary 2023년 3월 1일
dt = 0.001; % time step
% Define the computational grid
L = 200;
dx = 1;
x = 0:dx:L;
y = 0:dx:L;
[X,Y] = meshgrid(x,y);
% Define the initial temperature distribution and wind vector
u0 = 38*chi2rnd(2, size(X)).* chi2rnd(14, size(X)); % initial temperature
v0 = 0.5 + rand(size(X)).*(-0.4 + 0.4.*randi([0,1], size(X))); % initial fuel distribution
w = [300, 300]; % initial wind vector
% Set up the parameters
alpha = 0.2; % rate of fuel combustion
beta = 0.1; % rate of fuel evaporation
k = 0.1; % rate of heat transfer
rho = 1.2; % density of air
Cp = 1000; % specific heat of air
Tc = 273; % reference temperature
g = [0, -9.81]; % gravitational acceleration
% Define the wind vector function
wind = @(t) w;
wind_new = @(t) [50, 400]; % new wind vector at t = 0.085
% Set up the solver
T = 0.15;
t = 0:dt:T;
U = zeros([size(X), numel(t)]);
V = zeros([size(X), numel(t)]);
U(:,:,1) = u0;
V(:,:,1) = v0;
for n = 2:numel(t)
% Compute the wind vector
if t(n) <= 0.085
W = wind(t(n));
else
W = wind_new(t(n));
end
% Compute the temperature and fuel distributions
Ux = diff(U(:,:,n-1),1,2)/dx; % size 199x201
Uy = diff(U(:,:,n-1),1,1)/dx; % size 201x199
Uxx = diff(U(:,:,n-1),2,2)/dx^2; % size 199x201
Uyy = diff(U(:,:,n-1),2,1)/dx^2; % size 201x199
Vx = diff(V(:,:,n-1),1,2)/dx; % size 199x201
Vy = diff(V(:,:,n-1),1,1)/dx; % size 201x199
% Compute the rates of change
dUdt = k*(Uxx + Uyy) - alpha*V(:,:,n-1).^2 + dot(W, cat(3,Ux, Uy), 3); % size 200x200x1
dVdt = -beta*V(:,:,n-1).*U(:,:,n-1) + rho*Cp*(Tc - U(:,:,n-1)).*sqrt(Vx.^2 + Vy.^2) ...
+ dot(W, cat(3,Vx, Vy), 3) + dot(g, cat(3,V(:,:,n-1).*Vx, V(:,:,n-1).*Vy), 3); % size 200x200x1
% Update the temperature and fuel distributions
U(:,:,n) = U(:,:,n-1) + dUdt*dt; % size 200x200x1
V(:,:,n) = V(:,:,n-1) + dVdt*dt; % size 200x200x1
end
I have made changings but, still error
Adam Danz
Adam Danz 2023년 3월 1일
No, I don't do other people's work for them but I'd be truly happy to help you learn to debug your code. Start with a single term, let's say Uxx + Uyy, and figure out why that's not working. ONe is 199x201 and the other is 201x199, they need to be the same size. You can transpose one using the transpose operator: x'
After you fix that term, move to the next term - what size should V be? If you get stuck, leave a comment and I can suggest next steps.

댓글을 달려면 로그인하십시오.

답변 (2개)

Steven Lord
Steven Lord 2023년 3월 1일

0 개 추천

For these types of errors, where data isn't the size you expected it to be, I'd use error breakpoints. Set an error breakpoint using the approaches described on that documentation page then run your code. When the error occurs MATLAB will enter debug mode (the prompt will change to K>>) and you can look at the sizes, types, etc. of the data at that point in your code. You could experiment with what happens, for example, if you transpose one of your variables then rerun the line of code where the error occurred.
Once you've finished investigating, exit debug mode by typing:
continue

댓글 수: 1

Tanveer Hussain
Tanveer Hussain 2023년 3월 16일
This person likes copy paste, he does not know how to debug the code.

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2023년 3월 16일

0 개 추천

You are using diff() thinking that you are getting derivatives. However you are taking diff() of numeric matrices and diff() of numeric matrices is difference not derivative. In the case of a numeric vector diff(X) is X(2:end)-X(1:end-1)
Taking numeric differences shortens the dimension in the direction the difference is being taken. With you taking differences along rows and along columns, the arrays have one fewer rows for one call, and one fewer column for the other. You cannot simply transpose a matrix to get the calculation to fit.
You should switch to using gradient() instead of diff()

카테고리

도움말 센터File Exchange에서 Labels and Styling에 대해 자세히 알아보기

제품

릴리스

R2021a

태그

질문:

2023년 3월 1일

답변:

2023년 3월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by