0% found this document useful (0 votes)
24 views19 pages

MHD Problem 1,2,3

fregreger

Uploaded by

Raushan Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views19 pages

MHD Problem 1,2,3

fregreger

Uploaded by

Raushan Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

1.

Solve the one-dimensional MHD shock tube problem using the Lax-Friedrichs
method.
Steps: 1. Define the problem with initial and boundary conditions,

2. solve using the given method ,

3. construct the MATLAB code, visualize the solution of the problem.

Problem Description:
The one-dimensional MHD shock tube problem involves a sudden transition from one state to
another across a shock wave.

 Imagine a one-dimensional tube filled with an ideal MHD fluid (a combination of a


conducting fluid and a magnetic field).
 Initially, the tube has different conditions on its left and right sides.
 At time (t = 0), a barrier separating the two regions is removed, allowing the fluid to
interact.

Initial Conditions:
Left Side:

 Density (rho): Typically higher on the left side of the discontinuity.


 Pressure ((P)): Higher on the left side.
 Velocity ((V_x)): Usually set to zero or a specified value.
 Magnetic field ((B_x)): Nonzero and aligned with the tube; often set to a constant value
across the domain.

Right Side:

 Density (rho): Typically lower on the right side of the discontinuity.


 Pressure ((P)): Lower on the right side.
 Velocity ((V_x)): Usually set to zero or a specified value.
 Magnetic Field (BxB_xBx): Nonzero and aligned with the tube; often set to a
constant value across the domain.

Wave Propagation
In the MHD shock tube problem, the initial discontinuity in density and pressure leads to the
propagation of various types of waves, each characterized by specific behaviors and interactions:

Fast Rarefaction Waves

 Regions where the fluid expands rapidly, resulting in a decrease in density and
pressure.
 These waves travel faster than the speed of sound in the medium and are influenced
by both fluid and magnetic pressures.
Slow Shock Waves

 Regions where the fluid compresses and slows down, resulting in an increase in
density and pressure.
 These waves travel slower than the fast shocks and are influenced by the interaction
between the fluid dynamics and magnetic fields.

Contact Discontinuity

 A surface separating two regions of fluid with different densities but the same
pressure and velocity.
 It moves with the fluid velocity and marks the boundary between the left and right
states in the shock tube.

Alfven Waves

 Waves associated with the variations in the magnetic field.


 These waves propagate at the Alfvén speed, which depends on the magnetic field
strength and the density of the fluid. They cause rotational motions and magnetic field
variations without significant changes in density or pressure.

Examples of MHD Shock Tubes:

 Brio & Wu Shock Tube:


o Similar to the Sod Shock Tube but with magnetic fields.
o Demonstrates most MHD waves.
 Dai & Woodward Shock Tube:
o Produces three pairs of MHD waves moving outward from the central contact
discontinuity.
o Tests all seven MHD waves.
 Einfeldt Strong Rarefaction:
o Not a shock tube but a diverging flow scenario.
o Extremely strong and fast rarefaction.
o Reveals issues in code accuracy.
 Ryu & Jones Shock Tube:
○ Contains a wealth of different shock tubes for MHD testing

Numerical Solution Using the Lax-Friedrichs Method


The Lax-Friedrichs method is a finite difference method used to solve hyperbolic partial
differential equations. It combines the central difference approximation for spatial derivatives
with a stabilizing term to ensure numerical stability. The Lax–Friedrichs method is based on the
FTCS (forward in time, centered in space) scheme.

It introduces an artificial viscosity term to stabilize the solution.


Numerical Diffusion (Artificial Viscosity)

Numerical diffusion, or artificial viscosity, is introduced by the Lax-Friedrichs method to stabilize the
solution. It smooths out sharp gradients and discontinuities, preventing non-physical oscillations.
Steps to Implement the Lax-Friedrichs Method

1. Initialize the Grid and Parameters:


o Define the spatial domain and discretize it into a number of grid points.
o Set the initial conditions for density, velocity, pressure, and magnetic field
components on the grid.
2. Compute Fluxes:
o Compute the fluxes at each grid point using the current values of the
conserved variables.
3. Update Conserved Variables:
o Apply the Lax-Friedrichs update formula to compute the new values of the
conserved variables at the next time step.
4. Iterate Over Time Steps:
o Repeat the flux computation and variable update for the desired number of
time steps.

Matlab code:
1.
% Define constants and parameters
nx = 200; % number of spatial points
nt = 400; % number of time steps
L = 1.0; % length of the tube
dx = L / nx; % spatial step size
dt = 0.001; % time step size

% Initialize variables
x = linspace(-L/2, L/2, nx);
rho = zeros(nx, 1);
u = zeros(nx, 1);
p = zeros(nx, 1);
B_x = 1.0 * ones(nx, 1);
B_y = zeros(nx, 1);
B_z = zeros(nx, 1);
E = zeros(nx, 1);

% Initial conditions
rho_L = 1.0;
u_L = 0.0;
p_L = 1.0;
rho_R = 0.125;
u_R = 0.0;
p_R = 0.1;

% Initial conditions
for i = 1:nx
if x(i) < 0
rho(i) = rho_L;
u(i) = u_L;
p(i) = p_L;
else
rho(i) = rho_R;
u(i) = u_R;
p(i) = p_R;
end
end

% Calculate initial energy


gamma = 1.4; % adiabatic index
for i = 1:nx
E(i) = p(i) / (gamma - 1) + 0.5 * rho(i) * u(i)^2 + 0.5 * (B_x(i)^2 + B_y(i)^2
+ B_z(i)^2);
end

% Time-stepping loop
for n = 1:nt
% Compute fluxes
F = zeros(nx, 7); % Flux vector
for i = 1:nx
F(i, 1) = rho(i) * u(i);
F(i, 2) = rho(i) * u(i)^2 + p(i) + (B_x(i)^2 + B_y(i)^2 + B_z(i)^2) / 2 -
B_x(i)^2;
F(i, 3) = rho(i) * u(i) * u(i);
F(i, 4) = rho(i) * u(i) * u(i);
F(i, 5) = (E(i) + p(i) + (B_x(i)^2 + B_y(i)^2 + B_z(i)^2) / 2) * u(i);
F(i, 6) = u(i) * B_y(i);
F(i, 7) = u(i) * B_z(i);
end

% Apply Lax-Friedrichs update


U = [rho, rho .* u, rho .* u, rho .* u, E, B_y, B_z];
U_new = U;
for i = 2:nx-1
U_new(i, :) = 0.5 * (U(i+1, :) + U(i-1, :)) - (dt / (2 * dx)) * (F(i+1, :)
- F(i-1, :));
end

% Update variables
rho = U_new(:, 1);
u = U_new(:, 2) ./ rho;
E = U_new(:, 5);
B_y = U_new(:, 6);
B_z = U_new(:, 7);

% Update pressure
for i = 1:nx
p(i) = (gamma - 1) * (E(i) - 0.5 * rho(i) * u(i)^2 - 0.5 * (B_x(i)^2 +
B_y(i)^2 + B_z(i)^2));
end
end

% Visualization
figure;
subplot(3, 1, 1);
plot(x, rho);
xlabel('x');
ylabel('\rho');
title('Density');

subplot(3, 1, 2);
plot(x, u);
xlabel('x');
ylabel('u');
title('Velocity');
subplot(3, 1, 3);
plot(x, p);
xlabel('x');
ylabel('p');
title('Pressure');

2.

% Parameters
nx = 200; % Number of spatial points
nt = 400; % Number of time steps
L = 1.0; % Length of the tube
dx = L / nx; % Spatial step size
dt = 0.001; % Time step size
gamma = 1.4; % Adiabatic index

% Initialize variables
x = linspace(-L/2, L/2, nx);
rho = zeros(nx, 1);
u = zeros(nx, 1);
v = zeros(nx, 1);
w = zeros(nx, 1);
p = zeros(nx, 1);
B_x = 1.0 * ones(nx, 1); % Constant magnetic field in x direction
B_y = zeros(nx, 1);
B_z = zeros(nx, 1);
E = zeros(nx, 1);

% Initial conditions
rho_L = 1.0; % Higher density on the left
u_L = 0.0; % Zero velocity on the left
p_L = 1.0; % Higher pressure on the left
B_x_L = 1.0; % Magnetic field aligned with the tube on the left

rho_R = 0.125; % Lower density on the right


u_R = 0.0; % Zero velocity on the right
p_R = 0.1; % Lower pressure on the right
B_x_R = 1.0; % Magnetic field aligned with the tube on the right

% Set initial conditions


for i = 1:nx
if x(i) < 0
rho(i) = rho_L;
u(i) = u_L;
p(i) = p_L;
B_x(i) = B_x_L;
else
rho(i) = rho_R;
u(i) = u_R;
p(i) = p_R;
B_x(i) = B_x_R;
end
end

% Calculate initial energy


for i = 1:nx
E(i) = p(i) / (gamma - 1) + 0.5 * rho(i) * (u(i)^2 + v(i)^2 + w(i)^2) + 0.5 *
(B_x(i)^2 + B_y(i)^2 + B_z(i)^2);
end

% Time-stepping loop
for n = 1:nt
% Compute fluxes
F = zeros(nx, 7); % Flux vector
for i = 1:nx
F(i, 1) = rho(i) * u(i);
F(i, 2) = rho(i) * u(i)^2 + p(i) + 0.5 * (B_x(i)^2 + B_y(i)^2 + B_z(i)^2)
- B_x(i)^2;
F(i, 3) = rho(i) * u(i) * v(i) - B_x(i) * B_y(i);
F(i, 4) = rho(i) * u(i) * w(i) - B_x(i) * B_z(i);
F(i, 5) = (E(i) + p(i) + 0.5 * (B_x(i)^2 + B_y(i)^2 + B_z(i)^2)) * u(i) -
B_x(i) * (u(i) * B_x(i) + v(i) * B_y(i) + w(i) * B_z(i));
F(i, 6) = u(i) * B_y(i) - v(i) * B_x(i);
F(i, 7) = u(i) * B_z(i) - w(i) * B_x(i);
end

% Apply Lax-Friedrichs update


U = [rho, rho .* u, rho .* v, rho .* w, E, B_y, B_z];
U_new = U;
for i = 2:nx-1
U_new(i, :) = 0.5 * (U(i+1, :) + U(i-1, :)) - (dt / (2 * dx)) * (F(i+1, :)
- F(i-1, :));
end

% Update variables
rho = U_new(:, 1);
u = U_new(:, 2) ./ rho;
v = U_new(:, 3) ./ rho;
w = U_new(:, 4) ./ rho;
E = U_new(:, 5);
B_y = U_new(:, 6);
B_z = U_new(:, 7);

% Update pressure
for i = 1:nx
p(i) = (gamma - 1) * (E(i) - 0.5 * rho(i) * (u(i)^2 + v(i)^2 + w(i)^2) -
0.5 * (B_x(i)^2 + B_y(i)^2 + B_z(i)^2));
end
end

% Visualization
figure;

% Density
subplot(3, 2, 1);
plot(x, rho);
xlabel('x');
ylabel('\rho');
title('Density');

% Velocity
subplot(3, 2, 2);
plot(x, u);
xlabel('x');
ylabel('u');
title('Velocity');

% Pressure
subplot(3, 2, 3);
plot(x, p);
xlabel('x');
ylabel('p');
title('Pressure');

% Magnetic field B_y


subplot(3, 2, 4);
plot(x, B_y);
xlabel('x');
ylabel('B_y');
title('Magnetic Field B_y');

% Magnetic field B_z


subplot(3, 2, 5);
plot(x, B_z);
xlabel('x');
ylabel('B_z');
title('Magnetic Field B_z');

% Energy
subplot(3, 2, 6);
plot(x, E);
xlabel('x');
ylabel('E');
title('Energy');
Stability and Accuracy:
 The Lax–Friedrichs method is first order accurate in time and first order accurate in
space.
 It exhibits second-order dissipation and third-order dispersion.
2. Two-Dimensional MHD Flow in a Channel

Solve the two-dimensional MHD flow in a channel with conducting walls using

the finite difference method.

Steps: 1. Define the problem with initial and boundary conditions,

2. solve using the given method ,

3. construct the MATLAB code, visualize the solution of the problem.


MATLAB CODE:
% Parameters
Lx = 1; % Length of the channel
Ly = 1; % Height of the channel
Nx = 50; % Number of grid points in x
Ny = 50; % Number of grid points in y
T = 1; % Total time
dt = 0.001; % Time step

% Discretization
dx = Lx / (Nx - 1);
dy = Ly / (Ny - 1);
x = linspace(0, Lx, Nx);
y = linspace(0, Ly, Ny);

% Physical parameters
rho = 1; % Density
mu = 0.1; % Dynamic viscosity
sigma = 1; % Electrical conductivity
eta = 0.01; % Magnetic diffusivity

% Initial conditions
u = zeros(Nx, Ny);
v = zeros(Nx, Ny);
Bx = zeros(Nx, Ny);
By = zeros(Nx, Ny);

% Add initial disturbance


u(:, Ny/2) = 1;
Bx(:, Ny/2) = 0.8;

% Time loop
for t = 0:dt:T
% Copy current state
u_old = u;
v_old = v;
Bx_old = Bx;
By_old = By;

% Update velocity field (u, v)


for i = 2:Nx-1
for j = 2:Ny-1
% Finite difference approximations
u_x = (u_old(i+1, j) - u_old(i-1, j)) / (2 * dx);
u_y = (u_old(i, j+1) - u_old(i, j-1)) / (2 * dy);
u_xx = (u_old(i+1, j) - 2 * u_old(i, j) + u_old(i-1, j)) / (dx^2);
u_yy = (u_old(i, j+1) - 2 * u_old(i, j) + u_old(i, j-1)) / (dy^2);

v_x = (v_old(i+1, j) - v_old(i-1, j)) / (2 * dx);


v_y = (v_old(i, j+1) - v_old(i, j-1)) / (2 * dy);
v_xx = (v_old(i+1, j) - 2 * v_old(i, j) + v_old(i-1, j)) / (dx^2);
v_yy = (v_old(i, j+1) - 2 * v_old(i, j) + v_old(i, j-1)) / (dy^2);

Jx = sigma * (u_old(i, j) * By_old(i, j) - v_old(i, j) * Bx_old(i,


j));
Jy = sigma * (v_old(i, j) * Bx_old(i, j) - u_old(i, j) * By_old(i,
j));
u(i, j) = u_old(i, j) + dt * ( ...
- u_old(i, j) * u_x - v_old(i, j) * u_y ...
+ mu * (u_xx + u_yy) / rho ...
+ Jx);

v(i, j) = v_old(i, j) + dt * ( ...


- u_old(i, j) * v_x - v_old(i, j) * v_y ...
+ mu * (v_xx + v_yy) / rho ...
+ Jy);
end
end

% Update magnetic field (Bx, By)


for i = 2:Nx-1
for j = 2:Ny-1
Bx_x = (Bx_old(i+1, j) - Bx_old(i-1, j)) / (2 * dx);
Bx_y = (Bx_old(i, j+1) - Bx_old(i, j-1)) / (2 * dy);
Bx_xx = (Bx_old(i+1, j) - 2 * Bx_old(i, j) + Bx_old(i-1, j)) / (dx^2);
Bx_yy = (Bx_old(i, j+1) - 2 * Bx_old(i, j) + Bx_old(i, j-1)) / (dy^2);

By_x = (By_old(i+1, j) - By_old(i-1, j)) / (2 * dx);


By_y = (By_old(i, j+1) - By_old(i, j-1)) / (2 * dy);
By_xx = (By_old(i+1, j) - 2 * By_old(i, j) + By_old(i-1, j)) / (dx^2);
By_yy = (By_old(i, j+1) - 2 * By_old(i, j) + By_old(i, j-1)) / (dy^2);

Bx(i, j) = Bx_old(i, j) + dt * ( ...


eta * (Bx_xx + Bx_yy) ...
- (By_x * u_old(i, j) - By_y * v_old(i, j)));

By(i, j) = By_old(i, j) + dt * ( ...


eta * (By_xx + By_yy) ...
+ (Bx_x * v_old(i, j) - Bx_y * u_old(i, j)));
end
end

% Apply boundary conditions


u(:, 1) = 0; u(:, end) = 0; % No-slip at y = 0, y = Ly
v(:, 1) = 0; v(:, end) = 0;
Bx(:, 1) = Bx(:, 2); Bx(:, end) = Bx(:, end-1);
By(:, 1) = 0; By(:, end) = 0;

% Visualize
if mod(t, 0.1) == 0
subplot(2,1,1);
quiver(x, y, u', v');
title('Velocity Field');
xlabel('x');
ylabel('y');

subplot(2,1,2);
quiver(x, y, Bx', By');
title('Magnetic Field');
xlabel('x');
ylabel('y');

drawnow;
end
end
% Define domain parameters
L = 1.0; % Channel width (example value )
H = 1.0; % Channel height (example value )
Nx = 50; % Number of grid points in x-direction
Ny = 50; % Number of grid points in y-direction
dx = L / (Nx - 1); % Grid spacing in x
dy = H / (Ny - 1); % Grid spacing in y

% Define physical properties


rho = 1.0; % Density (example value )
mu = 0.01; % Dynamic viscosity (example value )
eta = 0.01; % Magnetic diffusivity (example value )

% Define boundary conditions (functions for velocity and magnetic field)


u_wall = @(y) 1.0; % Example: constant velocity at walls
v_wall = @(x) 0.0; % Example: no slip condition
B_wall = @(x, y) sin(pi*x).*sin(pi*y); % Example: some initial magnetic field
distribution

% Define initial conditions (functions for u and B)


u_init = @(x, y) 0.0; % Initial velocity field
B_init = @(x, y) sin(pi*x).*sin(pi*y); % Initial magnetic field

% Initialize solution arrays


u = zeros(Ny, Nx);
v = zeros(Ny, Nx);
B = zeros(Ny, Nx);

% Set initial conditions


for j = 1:Ny
for i = 1:Nx
u(j, i) = u_init((i-1)*dx, (j-1)*dy);
B(j, i) = B_init((i-1)*dx, (j-1)*dy);
end
end

% Time-stepping parameters
dt = 0.001; % Time step size (example value )
iterations = 100; % Number of iterations

% Discretize and solve governing equations (loop through iterations)


for n = 1:iterations
% Copy old values (for iterative update)
u_old = u;
v_old = v;
B_old = B;

% Discretize momentum equation


% Example using central difference for diffusion and upwind for convection
for j = 2:Ny-1
for i = 2:Nx-1
% x-momentum equation
u(j, i) = u_old(j, i) + dt * ( ...
- u_old(j, i) * (u_old(j, i+1) - u_old(j, i-1)) / (2*dx) ...
- v_old(j, i) * (u_old(j+1, i) - u_old(j-1, i)) / (2*dy) ...
+ mu * ((u_old(j, i+1) - 2*u_old(j, i) + u_old(j, i-1)) / dx^2 ...
+ (u_old(j+1, i) - 2*u_old(j, i) + u_old(j-1, i)) / dy^2) / rho );

% y-momentum equation
v(j, i) = v_old(j, i) + dt * ( ...
- u_old(j, i) * (v_old(j, i+1) - v_old(j, i-1)) / (2*dx) ...
- v_old(j, i) * (v_old(j+1, i) - v_old(j-1, i)) / (2*dy) ...
+ mu * ((v_old(j, i+1) - 2*v_old(j, i) + v_old(j, i-1)) / dx^2 ...
+ (v_old(j+1, i) - 2*v_old(j, i) + v_old(j-1, i)) / dy^2) / rho );
end
end

% Apply boundary conditions for velocity


for j = 1:Ny
u(j, 1) = u_wall((j-1)*dy); % Left wall
u(j, Nx) = u_wall((j-1)*dy); % Right wall
end
for i = 1:Nx
v(1, i) = v_wall((i-1)*dx); % Bottom wall
v(Ny, i) = v_wall((i-1)*dx); % Top wall
end

% Discretize magnetic induction equation


for j = 2:Ny-1
for i = 2:Nx-1
B(j, i) = B_old(j, i) + dt * ( ...
- u_old(j, i) * (B_old(j, i+1) - B_old(j, i-1)) / (2*dx) ...
- v_old(j, i) * (B_old(j+1, i) - B_old(j-1, i)) / (2*dy) ...
+ eta * ((B_old(j, i+1) - 2*B_old(j, i) + B_old(j, i-1)) / dx^2
...
+ (B_old(j+1, i) - 2*B_old(j, i) + B_old(j-1, i)) / dy^2) );
end
end

% Apply boundary conditions for magnetic field


for j = 1:Ny
for i = 1:Nx
B(j, i) = B_wall((i-1)*dx, (j-1)*dy);
end
end
end

% Generate grid for plotting


[x, y] = meshgrid(0:dx:L, 0:dy:H);

% Visualize results
figure
contourf(x, y, u);
title('Velocity Magnitude');
xlabel('x');
ylabel('y');
colorbar;

figure
contourf(x, y, B);
title('Magnetic Field');
xlabel('x');
ylabel('y');
colorbar;
3. Magnetic Diffusion in a One-Dimensional Conducting Medium

Solve the one-dimensional magnetic diffusion equation in a conducting medium

using the Crank-Nicolson method.

Steps: 1. Define the problem with initial and boundary conditions,

2. solve using the given method ,

3. construct the MATLAB code, visualize the solution of the problem.


% Define parameters
eta = 1e-4; % Magnetic diffusivity (m^2/s)
L = 1; % Length of the medium (m)
N = 101; % Number of spatial points
dx = L / (N-1); % Spatial spacing (m)
T = 0.1; % Simulation time (s)

% Calculate the number of time steps based on dt


dt = 0.001; % Time step (s)
M = floor(T/dt) + 1; % Total number of time steps (including initial step)

% Define initial condition


x = linspace(0, L, N);
B_0 = sin(2*pi*x/L);

% Initialize magnetic field


B = B_0'; % Transpose to make B a column vector

% Initialize matrix for storing results


B_history = zeros(N, M);
B_history(:,1) = B;

% Create tridiagonal matrix for the Crank-Nicolson method


alpha = eta * dt / (2 * dx^2);
A = (1 + 2*alpha) * eye(N) - alpha * diag(ones(N-1, 1), 1) - alpha * diag(ones(N-
1, 1), -1);
Bmat = (1 - 2*alpha) * eye(N) + alpha * diag(ones(N-1, 1), 1) + alpha *
diag(ones(N-1, 1), -1);

% Boundary conditions (Dirichlet)


A(1, :) = 0; A(1, 1) = 1; % Left boundary
A(end, :) = 0; A(end, end) = 1; % Right boundary

% Time-stepping loop
for j = 2:M
% Apply boundary conditions
B(1) = 0; % Left boundary condition
B(end) = 0; % Right boundary condition

% Calculate right-hand side


b = Bmat * B;
b(1) = 0; % Left boundary condition
b(end) = 0; % Right boundary condition

% Solve the system


B = A \ b;

% Store the results


B_history(:, j) = B;
end

% Visualization (plot B vs x at different times)


figure;
hold on;
for i = 1:floor(M/5):M % Plot at every 1/5th time step
plot(x, B_history(:,i), 'DisplayName', ['t = ' num2str((i-1)*dt)]);
end
xlabel('Position (x)');
ylabel('Magnetic Field (B)');
title('Magnetic Diffusion in 1D Medium (Crank-Nicolson)');
legend show;
grid on;
hold off;

% Parameters
L = 10; % Length of the domain
T = 1; % Total time
nx = 50; % Number of spatial grid points
nt = 1000; % Number of time steps
eta = 1; % Magnetic diffusivity

dx = L / (nx - 1);
dt = T / nt;
alpha = eta * dt / (2 * dx^2);

% Spatial and temporal grids


x = linspace(0, L, nx);
t = linspace(0, T, nt);

% Initial condition
B = sin(pi * x / L)'; % Example initial condition

% Boundary conditions
B1 = 0; % Boundary condition at x = 0
B2 = 0; % Boundary condition at x = L

% Construct the A and B matrices


A = (1 + 2*alpha) * eye(nx-2) - alpha * diag(ones(nx-3, 1), 1) - alpha *
diag(ones(nx-3, 1), -1);
Bmat = (1 - 2*alpha) * eye(nx-2) + alpha * diag(ones(nx-3, 1), 1) + alpha *
diag(ones(nx-3, 1), -1);

% Time-stepping loop
for n = 1:nt
% Extract interior points and apply boundary conditions
B_interior = B(2:end-1); % Ensure B_interior is a column vector

% Update the boundary conditions vector for each time step


RHS = Bmat * B_interior;
RHS(1) = RHS(1) + alpha * B1;
RHS(end) = RHS(end) + alpha * B2;

% Solve the system


B_next_interior = A \ RHS;

% Update the solution


B = [B1; B_next_interior; B2];
end

% Visualization
figure;
plot(x, B, 'LineWidth', 2);
xlabel('x');
ylabel('B(x, T)');
title('Magnetic Field Distribution at Final Time T');
grid on;

You might also like