give me invalid use of operator at U, why?

2 次查看(过去 30 天)
Nx = 50;
x = linspace(0,1,Nx+1);
dx = 1/Nx;
xmid = 0.5*(x(1:Nx) + x(2:Nx+1));
u = 1;
tfinal = 1;
CFL = 0.5;
dt = CFL*dx/abs(u);
U = 0.75*exp(-((xmid-0.5)/0.1). 2)';
t = 0;
% Loop until t > tfinal
while (t < tfinal),
Ubc = [U(Nx); U; U(1)]; % This enforces the periodic bc
% Calculate the flux at each interface
F = 0.5* u *( Ubc(2:Nx+2) + Ubc(1:Nx+1)) ...
- 0.5*abs(u)*( Ubc(2:Nx+2) - Ubc(1:Nx+1));
% Calculate residual in each cell
R = F(2:Nx+1) - F(1:Nx);
% Forward Euler step
U = U - (dt/dx)*R;
% Increment time
50 t = t + dt;
% Plot current solution
stairs(x,[U; U(Nx)]);
axis([0, 1, -0.5, 1.5]);
grid on;
drawnow;
end
% overlay exact solution
U = 0.75*exp(-((xmid-0.5)/0.1).ˆ2)';
hold on;
stairs(x,[U; U(Nx)], 'r-');

回答(1 个)

Walter Roberson
Walter Roberson 2021-10-13
U = 0.75*exp(-((xmid-0.5)/0.1). 2)';
my guess is that you wanted
U = 0.75*exp(-((xmid-0.5)/0.1).^2)';

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by