# give me invalid use of operator at U, why?

조회 수: 3(최근 30일)
oun alsharif 2021년 10월 13일
답변: Walter Roberson 2021년 10월 13일
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 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)';

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

### Community Treasure Hunt

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

Start Hunting!

Translated by