give me invalid use of operator at U, why?

1 view (last 30 days)
oun alsharif
oun alsharif on 13 Oct 2021
Answered: Walter Roberson on 13 Oct 2021
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-');

Answers (1)

Walter Roberson
Walter Roberson on 13 Oct 2021
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)';

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!