0% found this document useful (0 votes)
4 views10 pages

Matlab Part

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 10

Matlab Part:

QUESTION NO.8
Calculate the molar volume and density of CO2 at 50 bar
and 450 K (176.85°C) using ideal gas law and the
Redlich-Kwong (RK) equation of state. Under these
conditions, the molar volume given in Perry’s Handbook
is 0.7065 m3 /kmol (p. 2-241, 8th ed.). Cubic form of the
Redlich-Kwong equation:

Code & Result:


% Given data
P = 50e5; % Pressure in Pa (50 bar)
T = 450; % Temperature in K
R = 8.314; % Gas constant in m^3·Pa/(mol·K)

% Critical properties of CO2


Pc = 73.8e5; % Critical pressure in Pa
Tc = 304.2; % Critical temperature in K

% Redlich-Kwong parameters
a = 0.42748 * (R^2 * Tc^2.5) / Pc;
b = 0.08664 * R * Tc / Pc;

% Solve using Ideal Gas Law


V_ideal = R * T / P; % Ideal gas molar volume in m^3/mol
rho_ideal = 1 / V_ideal; % Ideal gas density in mol/m^3

% Solve using Redlich-Kwong EOS (Cubic form: v^3 - A*v^2 + B*v - C = 0)


alpha = sqrt(Tc / T); % Temperature-dependent factor

% Coefficients of the cubic equation


A = R * T / P;
B = (a * alpha / P - b * R * T / P - b^2);
C = -a * alpha * b / P;
% Density from RK equation
rho_RK = 1 / V_RK; % Density in mol/m^

% Display results
disp('Results:');
disp(['Ideal Gas Molar Volume (m^3/mol): ', num2str(V_ideal)]);
disp(['Ideal Gas Density (mol/m^3): ', num2str(rho_ideal)]);
disp(['RK Molar Volume (m^3/mol): ', num2str(V_RK)]);
disp(['RK Density (mol/m^3): ', num2str(rho_RK)]);
QUESTION NO.9
Solve the following ODE:
y(0) = 0.1, use step size of 0.1

Code & Result:


clc
% Define the ODE: dy/dt = 30y - 40y^2
% Initial condition: y(0) = 0.1

h = 0.1; % Step size


t_end = 1; % Final time (you can change this value)
t = 0:h:t_end; % Time vector
y = zeros(1, length(t)); % Initialize y array
y(1) = 0.1; % Initial condition

% Euler's method to solve the ODE


for i = 1:length(t)-1
y(i+1) = y(i) + h * (30*y(i) - 40*y(i)^2); % Euler method update
end

% Display the results


disp('Time values:');
disp(t);
disp('Numerical solutions (y):');
disp(y);

Write script file using for loop to:

a) Compare (calculate the percent error at each point) the results with
exact solution which is as follows:

b) Repeat the solution with step size of 0.05 and again compare your
results with exact solution by calculating percent error at each point.
c) Repeat the solution with step size of 0.025 and compare your results
with exact solution.
d) Solve the same problem using ode45 built in command.
Code:
clc
% Define the exact solution
exact_solution = @(t) (3*exp(30*t)/26 + 4*exp(30*t));

% Time interval and initial condition


t_start = 0;
t_end = 1;
y0 = exact_solution(t_start); % Initial condition y(0)

% Step sizes for parts a, b, c


step_sizes = [0.1, 0.05, 0.025];

% Loop over each step size for parts a, b, c


for k = 1:length(step_sizes)
h = step_sizes(k);
t = t_start:h:t_end; % Time points
y_exact = exact_solution(t); % Exact solution

% Numerical solution using Euler's method


y_num = zeros(size(t));
y_num(1) = y0; % Initial condition
for i = 2:length(t)
y_num(i) = y_num(i-1) + h * (30 * y_num(i-1)); % dy/dt = 30*y
end

% Calculate percent error


percent_error = abs((y_exact - y_num) ./ y_exact) * 100;

% Display results in a table


fprintf('Results for step size h = %.3f:\n', h);
results = table(t', y_exact', y_num', percent_error', ...
'VariableNames', {'Time', 'Exact', 'Numerical', 'PercentError'});
disp(results);
end

% Part d: Using ode45


fprintf('Results using ode45:\n');
odefun = @(t, y) 30 * y; % Define the ODE dy/dt = 30*y
[t_ode45, y_ode45] = ode45(odefun, [t_start t_end], y0); % Solve with ode45
y_exact_ode45 = exact_solution(t_ode45); % Exact solution at ode45 time
points

% Calculate percent error for ode45


percent_error_ode45 = abs((y_exact_ode45 - y_ode45) ./ y_exact_ode45) *
100;

% Display results in a table


ode45_results = table(t_ode45, y_exact_ode45, y_ode45, percent_error_ode45,
...
'VariableNames', {'Time', 'Exact', 'Numerical', 'PercentError'});
disp(ode45_results);

Results:
QUESTION NO.10
y=x4+3x2+2x+5 write a program to find its roots.
Code & Result:
% Define the coefficients of the polynomial y = x^4 + 3x^2 + 2x + 5
coefficients = [1 0 3 2 5];

% Find the roots of the polynomial


roots_of_polynomial = roots(coefficients);

% Display the roots


disp('The roots of the polynomial y = x^4 + 3x^2 + 2x + 5 are:');
disp(roots_of_polynomial);

QUESTION NO.11
Write a program to calculate x

Code & Result:


% Coefficients of the quadratic equation ax^2 + bx + c = 0
a = 2;
b = -10;
c = 12;

% Calculate the first root using the quadratic formula


x = (-b + sqrt(b^2 - 4*a*c)) / (2*a);

% Display the root


fprintf('The value is: x = %.2f\n', x);

QUESTION NO.12
. The x-y data of a system is given below. Plot the data and
properly label it withinx = 0 to x = 1.4. find area under curve
within this range as well.
Code & Result:
clc
% Given data
x = [0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6];
y = [5, 5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.7, 5.8];

% Plot the data


figure;
plot(x, y, '-o', 'LineWidth', 2, 'MarkerSize', 6);
xlabel('x');
ylabel('y');
title('Plot of x-y data');
grid on;

% Calculate the area under the curve within x = 0 to x = 1.4 using


numerical integration
% We will use the trapz function to estimate the area under the curve
area = trapz(x(x <= 1.4), y(x <= 1.4)); % Restricting x to 0 to 1.4
fprintf('The area under the curve between x = 0 and x = 1.4 is: %.4f\n',
area);

Graph:

QUESTION NO.13
A trigonometric identity is given by following relation:
Verify that the identity is correct by calculating the left and right
hand side of equations atx=pi.

Code & Result:


clc
% Given value of x
x = pi / 5;

% Left-hand side (LHS) = cos^2(x/2)


LHS = cos(x / 2)^2;

% Right-hand side (RHS) = (tan(x) + sin(x)) / (2 * tan(x))


RHS = (tan(x) + sin(x)) / (2 * tan(x));

% Display the results


fprintf('LHS = %.6f\n', LHS);
fprintf('RHS = %.6f\n', RHS);

% Check if LHS is approximately equal to RHS


if abs(LHS - RHS) < 1e-6
disp('The identity is verified: LHS = RHS');
else
disp('The identity is not verified: LHS != RHS');
end

QUESTION NO.14
Create following matrices in Matlab

a) Calculate A+B and B+A

b) Calculate A+(B+C) and (A+B)+C

c) Show that multiplication with scalar quantity is distributive (e.g.


3(A+B)=3A+3B)

Code:
% Given matrices
A = [5 2 4; 1 7 -3; 6 -10 0];
B = [11 5 -3; 0 -12 4; 2 6 1];
C = [7 14 1; 10 3 -2; 8 -5 9];

% a. Calculate A+B and B+A


sumAB = A + B;
sumBA = B + A;

% Display the results for a


fprintf('A + B:\n');
disp(sumAB);
fprintf('B + A:\n');
disp(sumBA);

% b. Calculate A + (B + C) and (A + B) + C
sumBplusC = B + C;
sumAplusBplusC = A + sumBplusC;
sumAplusB = A + B;
sumAplusBplusC2 = sumAplusB + C;

% Display the results for b


fprintf('A + (B + C):\n');
disp(sumAplusBplusC);
fprintf('(A + B) + C:\n');
disp(sumAplusBplusC2);

% c. Show that multiplication with scalar is distributive: 3*(A + B) = 3A +


3B
scalar = 3;
left_side = scalar * (A + B);
right_side = scalar * A + scalar * B;

% Display the results for c


fprintf('3 * (A + B):\n');
disp(left_side);
fprintf('3A + 3B:\n');
disp(right_side);

% Check if they are equal


if isequal(left_side, right_side)
disp('Multiplication with scalar is distributive: 3*(A + B) = 3A +
3B');
else
disp('Multiplication with scalar is not distributive.');
end

Result:

You might also like