- 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.
Array not compatible error
1 次查看(过去 30 天)
显示 更早的评论
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
% Visualize the results
figure;
subplot(1,2,1);
imagesc(U(:,:,end));
title('Temperature distribution');
colorbar
4 个评论
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
2023-3-1
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 个评论
Walter Roberson
2023-3-16
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()
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surrogate Optimization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!