0% found this document useful (0 votes)
2 views7 pages

CLAB SEM 2 (1)

The document contains solutions to five mathematical problems involving differential equations and numerical methods. Problem 1 uses Euler's method to solve a differential equation, while Problem 2 employs the Runge-Kutta 4th order method. Problems 3 and 4 involve integration using Simpson's 1/3 method and analyzing a 1D harmonic oscillator's properties, respectively.

Uploaded by

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

CLAB SEM 2 (1)

The document contains solutions to five mathematical problems involving differential equations and numerical methods. Problem 1 uses Euler's method to solve a differential equation, while Problem 2 employs the Runge-Kutta 4th order method. Problems 3 and 4 involve integration using Simpson's 1/3 method and analyzing a 1D harmonic oscillator's properties, respectively.

Uploaded by

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

𝒅𝒚

Problem-1: Solve using Euler Method 𝟓 𝒅𝒙 = 𝟑𝒙𝟐 𝒚 , 𝒘𝒉𝒆𝒓𝒆 𝒚(𝟎) = 𝟏 for interval 𝟎 ≤ 𝒙 ≤ 𝟎. 𝟑 𝒘𝒊𝒕𝒉 𝒉 =
𝟎. 𝟏

CODE:

clc;

clear;

% Euler's Method for dy/dx = (3/5) * x^3 * y

% Given

h = 0.1; % Step size

x = 0:h:0.3; % Interval from 0 to 0.3

y = zeros(size(x)); % Initialize y

y(1) = 1; % Initial condition y(0) = 1

% Function f(x, y)

f = @(x, y) (3/5) * x^3 * y;

% Euler's Method loop

for i = 1:length(x)-1

y(i+1) = y(i) + h * f(x(i), y(i));

end

% Display results

disp('x y');

disp([x' y']);

% Plot

plot(x, y, 'o-');

xlabel('x');

ylabel('y');

title('Euler Method');

grid on;

OUTPUT:

x y

0 1.0000
0.1000 1.0000

0.2000 1.0001

0.3000 1.0005

GRAPH:

𝒅𝒚
Problem-2. Solve the differential equation 𝒅𝒙 = 𝒙 + 𝒚, with the initial condition 𝒚(𝟎) = 𝟏 using the using
Runge kutta 4th order method in octave. Use a step size of the 𝒉 = 𝟎. 𝟏 and compute the solution from 𝒙 =
𝟎 𝒕𝒐 𝒙 = 𝟏.

Answer/Code:

clear;

% define the differential equation as a function.

f= @(x,y) x+y;

% initial condition.

x_0=0;

y_0=1;

h=0.1;

n=10;

% initial vectors/ array.

x = zeros(1, n+1);

y = zeros(1, n+1);
% Set initial values

x(1) = x_0;

y(1) = y_0;

% Runge-Kutta 4th Order Method

for i = 1:n

k1 = h * f(x(i), y(i));

k2 = h * f(x(i) + h/2, y(i) + k1/2);

k3 = h * f(x(i) + h/2, y(i) + k2/2);

k4 = h * f(x(i) + h, y(i) + k3);

y(i+1) = y(i) + (1/6)*(k1 + 2*k2 + 2*k3 + k4);

x(i+1) = x(i) + h;

endfor

% Display the results

disp(' x y');

disp([x' y']);

% Plot the result

plot(x, y, '-o');

xlabel('x');

ylabel('y');

title('Runge-Kutta 4th Order Method for dy/dx = x + y');

grid on;

OUTPUT: RK41

x y

0 1.0000

0.1000 1.1103

0.2000 1.2428

0.3000 1.3997

0.4000 1.5836
0.5000 1.7974

0.6000 2.0442

0.7000 2.3275

0.8000 2.6511

0.9000 3.0192

1.0000 3.4366

GRAPH:

Problem-3. Integration using Simpson 1/3 Method. (i) ∫ 𝒙𝟑 𝒅𝒙 where the limit(1,3).

Answer:

CODE:

clear;

%Integration using Simpson 1/3 rule.

a=1; %lower limit of the function.

b=3; %upper limit of the function.

n=100;

h = (b - a) / (n);

x = a:h:b;

y = x.^3; %function.

s_odd=4*sum(y(2:2:end-1));
s_even=2*sum(y(3:2:end-2));

integration=(h/3)*(y(1)+y(end)+s_odd+s_even);

disp("Value of this Integration using Simpson 1/3 rule-")

disp(integration)

OUTPUT:

Value of this Integration using Simpson 1/3 rule-

20.000

Problem-4: An 1D hermonic oscillator assumed ℏ = 𝟏 , 𝝎 = 𝟏, 𝒌 = 𝟏. There plot partition function vs


temperature, average energy vs temperature and Cv vs temperature.

CODE:

clc;

clear;

% Define constants

h_bar = 1; % Reduced Planck's constant (assuming normalized units)

w = 1; % Angular frequency (assuming normalized units)

k = 1; % Boltzmann constant

n_max = 100; % Maximum quantum number

% Define temperature range

T = 1:200;

% Initialize partition function and average energy arrays

Z = zeros(size(T));

avg_E = zeros(size(T));

sq_avg_E = zeros(size(T));

C_v= zeros(size(T)); % initial vectors of C_v

% Loop over temperature values

for i = 1:length(T)

B = 1 / (k * T(i));

E = (0:n_max) + 0.5; % Energy levels


E_m=E.^2;

exp_term = exp(-B * E * h_bar * w);

Z(i) = sum(exp_term);

avg_E(i) = sum(E .* exp_term) / Z(i); % Corrected element-wise multiplication and division

sq_avg_E(i) = sum(E_m .* exp_term) / Z(i); % square average of energy function.

C_v(i)=[sq_avg_E(i) - (avg_E(i))^2 ]*k*B;

endfor

% Plot the partition function vs temperature

plot(T, Z);

xlabel('Temperature (T)');

ylabel('Partition Function (Z) & Average Energy');

title('Partition Function, Average Energy vs Temperature');

grid on;

hold on;

% Plot the average energy vs temperature

plot(T, avg_E, 'r-');

hold on;

plot(T, C_v, 'b-');

legend('Partition Function (Z)', 'Average Energy (\langleE\rangle)' , 'C_v');

hold off;

OUTPUT GRAPH:
Problem-5:

You might also like