Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Question# 01:

𝒙
Use 𝒖(𝒙, 𝒕) = 𝒇(𝜼), 𝜼 = , find the solution of
√𝟒𝒌𝒕

𝒖𝒕 = 𝒌𝒖𝒙𝒙, −∞<𝒙<∞, 𝒕>𝟎, subject to the conditions

𝒖(𝒙, 𝟎) = 𝟎 𝒇𝒐𝒓 𝒙 < 𝟎; 𝒖(𝒙, 𝟎) = 𝒖𝟎 𝒇𝒐𝒓 𝒙 > 𝟎.


Code:
function differentialequation01()
% Parameters
u0 = 1; % Initial condition for x > 0
k = 0.1; % Diffusion coefficient
% Spatial domain
x = linspace(-10, 10, 100);
% Time range
t = linspace(0, 1, 100);
% Solve the PDE
u = solvePDE(u0, k, x, t);
% Plot the solution
[X, T] = meshgrid(x, t);
surf(X, T, u)
xlabel('x')
ylabel('t')
zlabel('u')
end
function u = solvePDE(u0, k, x, t)
% Constants
L = 10; % Length of the domain
N = 100; % Number of terms in the series

% Initialize the solution matrix


u = zeros(length(x), length(t));
% Calculate the series solution
for n = 1:N
% Eigenvalue
lambda = (2*n-1)^2 * pi^2 / (4*L^2);
% Eigenfunction
phi = sqrt(2/L) * sin((2*n-1)*pi*x/(2*L));
% Coefficient
Cn = 8*u0/(pi*(2*n-1));
% Time-dependent solution
u_t = exp(-k*lambda*t);
% Add the contribution of each term to the solution
u = u + Cn .* u_t .* phi;
end
% Set initial conditions for x < 0
u(x < 0, 1) = 0;

% Set initial conditions for x > 0


u(x > 0, 1) = u0;
end
Question# 02
Calculate Fourier Sine transform of the function
𝒔𝒊𝒏𝒙 𝟎 ≤ 𝒙 < 𝝅
𝒇(𝒙) = {
𝟎 𝒙>𝝅
Code:
function qN2()
% Define the function
f = @(x) sin(x).*(x >= 0 & x < pi);

% Define the range of x values


x = linspace(0, pi, 100);

% Calculate the Fourier Sine Transform


F = calculateFourierSineTransform(f, x);
xlim([0.5, 1.5])
% Plot the Fourier Sine Transform
plot(x, F)
xlabel('x')
ylabel('F(s)')
title('Fourier Sine Transform')
end

function F = calculateFourierSineTransform(f, x)
% Calculate the Fourier Sine Transform
N = length(x);
F = zeros(size(x));
for n = 1:N
F(n) = integral(@(t) f(t).*sin(n.*t), 0, pi);
end
end

You might also like