CLAB SEM 2 (1)
CLAB SEM 2 (1)
Problem-1: Solve using Euler Method 𝟓 𝒅𝒙 = 𝟑𝒙𝟐 𝒚 , 𝒘𝒉𝒆𝒓𝒆 𝒚(𝟎) = 𝟏 for interval 𝟎 ≤ 𝒙 ≤ 𝟎. 𝟑 𝒘𝒊𝒕𝒉 𝒉 =
𝟎. 𝟏
CODE:
clc;
clear;
% Given
y = zeros(size(x)); % Initialize y
% Function f(x, y)
for i = 1:length(x)-1
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;
f= @(x,y) x+y;
% initial condition.
x_0=0;
y_0=1;
h=0.1;
n=10;
x = zeros(1, n+1);
y = zeros(1, n+1);
% Set initial values
x(1) = x_0;
y(1) = y_0;
for i = 1:n
k1 = h * f(x(i), y(i));
x(i+1) = x(i) + h;
endfor
disp(' x y');
disp([x' y']);
plot(x, y, '-o');
xlabel('x');
ylabel('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;
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(integration)
OUTPUT:
20.000
CODE:
clc;
clear;
% Define constants
k = 1; % Boltzmann constant
T = 1:200;
Z = zeros(size(T));
avg_E = zeros(size(T));
sq_avg_E = zeros(size(T));
for i = 1:length(T)
B = 1 / (k * T(i));
Z(i) = sum(exp_term);
endfor
plot(T, Z);
xlabel('Temperature (T)');
grid on;
hold on;
hold on;
hold off;
OUTPUT GRAPH:
Problem-5: