Getting error: Error using .* Matrix dimensions must agree. Please help. The code the given below.
情報
この質問は閉じられています。 編集または回答するには再度開いてください。
古いコメントを表示
Nx = 100;Ny = 20;Nt = 4000;
x = 0:10/Nx:10; y = -1:2/Ny:1; t = 0:100/Nt:100;
Dx = 10/Nx;Dy = 2/Ny;Dt = 10/Nt;
a = 0.001; Xd = a*Dt/(Dx^2); Yd = a*Dt/(Dy^2); c = Dt/(2*Dx);
T = zeros(Ny+1,Nx+1,Nt+1);
T(1,1:end,:) = 0;
for m = 1:Ny+1
T(m,1,:) = (1 - (y(m).*y(m))).^2;
end
maxiter = 500;
for k = 1:maxiter
Tlast = T; % saving the last guess
T(:,:,1) = Tlast(:,:,end); % initialize the scalar field at t = 0 to the last guess
for i = 2:Nt+1
T(2:end-1,2:end-1,i) = (1-(2*Xd)-(2*Yd))*T(2:end-1,2:end-1,i-1) + Xd*(T(2:end-1,3:end,i-1) + T(2:end-1,1:end-2,i-1)) - (c*1.5*(1 - (y(2:end).^2)).*(T(2:end-1,3:end,i-1) - T(2:end-1,1:end-2,i-1)) + Yd*T(3:end,2:end-1,i-1) + Yd*T(1:end-2,2:end-1,i-1);
T(end,1:end,i) = (4*T(end-1,1:end,i) - T(end-2,1:end,i))/3; % Top Wall (dT/dy = 0)
T(1:end,end,i) = (4*T(1:end,end-1,i) - T(1:end,end-2,i))/3; % Outlet (dT/dx = 0)
end
err(k) = max(abs(T(:)-Tlast(:))); % finding the residual value between two successive iterations
if err(k) < 1E-04
break; % stopping the solution is the residual value is small
end
end
The problem, I guess, lies in the formula for T(2:end-1,2:end-1,i).
6 件のコメント
In your giant line (seems to be missing a paren at the end in the above code?):
T(2:end-1,2:end-1,i) = (1-(2*Xd)-(2*Yd))*T(2:end-1,2:end-1,i-1) + Xd*(T(2:end-1,3:end,i-1) + T(2:end-1,1:end-2,i-1)) - (c*1.5*(1 - (y(2:end).^2)).*(T(2:end-1,3:end,i-1) - T(2:end-1,1:end-2,i-1)) + Yd*T(3:end,2:end-1,i-1) + Yd*T(1:end-2,2:end-1,i-1);
... each 'block' of math (for k=1) is producing a [19x99] double array, except this one:
c*1.5*(1 - (y(2:end).^2))
which produces a [1x20] double, as y(2:end) is a [1x20] and c is a scalar.
Since I don't know the logic of what you're aiming for, I can't guess at what would properly fix that, but look around there and perhaps repost with the paren fixed on that line?
Walter Roberson
2012 年 12 月 3 日
Justin is pointing out that you are trying to use .* to multiply a row vector by a 2D matrix. You might need to repmat() the row vector to make it the same size as the 2D matrix.
Shashank
2012 年 12 月 3 日
Shashank
2012 年 12 月 3 日
Shashank
2012 年 12 月 3 日
回答 (0 件)
この質問は閉じられています。
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!