give me invalid use of operator at U, why?

2 ビュー (過去 30 日間)
oun alsharif
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
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 Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by