Nothing Special   »   [go: up one dir, main page]

All Lab Task

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

UNIVERSITY OF ENGINEERING AND TECHNOLOGY,

PESHAWAR PAKISTAN
Main Campus

CS-311 Web Programming


Practical Task 01
Submitted By
Name: SAJJAD KHAN
Registration No. 21PWBCS0832
Semester: [BS CS 6th
Section: A

Submitted To
Sir Anwar Shan

DEPARTMENT OF COMPUTER SCIENCE & IT


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, PESHAWAR, PAKISTAN
LAB TASK 04
Q1:
clc;
clear all;

A = [5 2 5 -3 1 2; 3 32 5 -12 2 1; 2 3 25 -2 5 2; 1 12 5 -42 2 5; 4 6 5 4 30
8; 5 6 7 3 8 42];
b = [50; 60; 40; 35; 41; 24];
X1 = inv(A) * b;

maxn = 20;
n = length(b);
tol = 1e-2;
p=[0 0 0 0]';

% p = zeros(n, 1);

% Direct method using MATLAB's mldivide operator


X_direct = A \ b;
disp('Direct Method Solution:');
disp(X_direct);

R = sum(abs(A), 2);
D = abs(diag(A));
W = R - D;
check = D >= W;
disp('Check for Diagonal Dominance:');
disp(check);
if all(check)
disp('Matrix is diagonally dominant.');

% Jacobi iteration
for k = 1:maxn
for j = 1:n
x(j) = (b(j) - A(j, [1:j - 1, j + 1:n]) * p([1:j - 1, j + 1:n])) /
A(j, j);
end
err = abs(norm(x' - p));
p = x';
X(:, k) = p;
if err < tol
break;
end
end
X
else
disp('Matrix is not diagonally dominant.');
end

OUTPUT:
Q2:
% Gauss- Sediel iterarion Ax=b
clc, clear all
A = [5 2 5 -3 1 2; 3 32 5 -12 2 1; 2 3 25 -2 5 2; 1 12 5 -42 2 5; 4 6 5 4 30
8; 5 6 7 3 8 42];
b = [50; 60; 40; 35; 41; 24];

% X1=inv(A)*b
maxn=20;
n=length(b);
tol=10^(-2);
p=[0 0 0 0]';

% Direct method using MATLAB's mldivide operator


X_direct = A \ b;
disp('Direct Method Solution:');
disp(X_direct);

R=sum(abs(A),2);
D=abs(diag(A));
W=R-D;
check=D>=W
DD=all(check);
% pause
if DD==1
for k=1:maxn
for j=1:n
if j==1
x(1)=(b(1)-A(1,2:n)*p(2:n))/A(1,1);
elseif j==n
x(n)=(b(n)-A(n,1:n-1)*x(1:n-1)')/A(n,n);
else
x(j)=(b(j)-A(j,1:j-1)*x(1:j-1)'-A(j,j+1:n)*p(j+1:n))/A(j,j);
end

end
err=abs(norm(x'-p));
p=x';
X(:,k)=p

pause
if err<tol,break
end % a11x1+a12x2 p= intial values, x=updated values
% pause
end
X
else
disp('matrix is not diaganolly dominant')
end

OUTPUT:
Lab Task 05
Q1: Trapezoidal Rule

clc, clear all, close all


f = @(x) x.^3 + x.^2 - 2.*x; % Define the function
% check if n= 10
n = 10; % Number of trapeziums
a = 0; % Lower limit
b = 3; % Upper limit
h = (b - a) / n; % Step size

% Trapezoidal Rule
sum = 0;
for i = 1:n-1
sum = sum + f(a + i*h);
end
TR = (h/2) * (f(a) + 2*sum + f(b));
fprintf('Result using trapezoidal rule with n=10 is: %0.8f\n', TR)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% check if n= 20
n = 20; % Number of trapeziums
h = (b - a) / n; % Step size

% Trapezoidal Rule
sum = 0;
for i = 1:n-1
sum = sum + f(a + i*h);
end
TR = (h/2) * (f(a) + 2*sum + f(b));
fprintf('Result using trapezoidal rule with n=20 is: %0.8f\n', TR)

OUTPUT:

Q2: Simpson’s 1/3 Rule


clc, clear all, close all

f = @(x) sin(x) + 2 * cos(x); % Define the function properly

% For n = 10
n = 10; % Number of intervals, must be even for Simpson's rule
a = 0; % Lower limit
% b = 1.571428571428571; % Upper limit
b = pi/2; % Upper limit
h = (b - a) / n; % Step size

% Simpson's 1/3 Rule


oddsum = 0;
for j = 1:2:n-1
oddsum = oddsum + f(a + j * h);
end
evensum = 0;
for k = 2:2:n-2
evensum = evensum + f(a + k * h);
end
sim13 = (h / 3) * (f(a) + 4 * oddsum + 2 * evensum + f(b));
fprintf('Result using Simpson 1/3rd rule with n=10 is: %0.8f\n', sim13)

% Repeat the process for n = 20


n = 20;
h = (b - a) / n; % Recalculate step size for new n

% Simpson's 1/3 Rule


oddsum = 0;
for j = 1:2:n-1
oddsum = oddsum + f(a + j * h);
end
evensum = 0;
for k = 2:2:n-2
evensum = evensum + f(a + k * h);
end
sim13 = (h / 3) * (f(a) + 4 * oddsum + 2 * evensum + f(b));
fprintf('Result using Simpson 1/3rd rule with n=20 is: %0.8f\n', sim13)

OUTPUT:

Q3: Simpson’s 3/8 Rule


clc, clear all, close all
f = @(x) sin(x).^2 + cos(x); % Define the function properly

% For n = 10
n = 10; % Number of intervals
a = 0; % Lower limit
b = pi; % Upper limit
h = (b - a) / n; % Step size

% Simpson's 3/8 Rule


sum3 = 0;
for l = 3:3:n-1
sum3 = sum3 + f(a + l * h);
end
sumall = 0;
for m = 1:n-1
sumall = sumall + f(a + m * h);
end
sumnot3 = sumall - sum3;
sim38 = (3 * h / 8) * (f(a) + 3 * sumnot3 + 2 * sum3 + f(b));
fprintf('Result using Simpson 3/8th rule with n = 10 is: %0.8f\n', sim38)

% Repeat the process for n = 20


n = 20;
h = (b - a) / n; % Recalculate step size for new n

% Simpson's 3/8 Rule


sum3 = 0;
for l = 3:3:n-1
sum3 = sum3 + f(a + l * h);
end
sumall = 0;
for m = 1:n-1
sumall = sumall + f(a + m * h);
end
sumnot3 = sumall - sum3;
sim38 = (3 * h / 8) * (f(a) + 3 * sumnot3 + 2 * sum3 + f(b));
fprintf('Result using Simpson 3/8th rule with n = 20 is: %0.8f\n', sim38)

OUTPUT:

Lab task 06:


Q1:
%%%%%%%%%%%%%%%%%%%%%%%%%% E1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
syms y(x)
eqn1 = diff(y, x) + y*tan(x) == y^3 * sec(x)^4;
sol1 = dsolve(eqn1);
sol1final = simplify(sol1)

% Displaying Problem 1
disp('Problem 1:')
disp('Given ODE:')
disp(eqn1)
disp('Solution:')
disp(sol1final)

%%%%%%%%%%%%%%%%%%%%%%%%%% E2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
syms x y(x)
equ2=y^4 * diff(y, x) + diff(y, x) +x^2 + 1 == 0;
sol2=dsolve(equ2);
sol2final = simplify(sol2)

% Displaying Problem 2
disp('Problem 2:')
disp('Given ODE:')
disp(equ2)
disp('Solution:')
disp(sol2final)

%%%%%%%%%%%%%%%%%%%%%%%%%% E3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
syms y(x)
equ3=diff(y, x, 2) + 3*diff(y, x) + y == exp(x) -3;
sol3=dsolve(equ3);
sol3final = simplify(sol3)

% Displaying Problem 3
disp('Problem 3:')
disp('Given ODE:')
disp(equ3)
disp('Solution:')
disp(sol3final)

%%%%%%%%%%%%%%%%%%%%%%%%%% E4 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
syms y(x)
equ4=diff(y, x, 4) - y == 2* exp(2*x) + 3*exp(x);
sol4=dsolve(equ4);
sol4final = simplify(sol4)

% Displaying Problem 4
disp('Problem 4:')
disp('Given ODE:')
disp(equ4)
disp('Solution:')
disp(sol4final)

%%%%%%%%%%%%%%%%%%%%%%%%%% E5 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
syms x y(x)
Dy = diff(y, x);
eqn5 = diff(y, x, 2) * (x + 3) * Dy - y == 0;
cond5 = [y(0) == 1, Dy(0) == 2];
gsol5 = simplify(dsolve(eqn5))
psol5 = simplify(dsolve(eqn5, cond5))

% Displaying Problem 5
disp('Problem 5:')
disp('Given ODE:')
disp(eqn5)
disp('General Solution:')
disp(gsol5)
disp('Particular Solution with Initial Conditions:')
disp(psol5)

%%%%%%%%%%%%%%%%%%%%%%%%%% E6 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
syms y(x)
Dy = diff(y, x);
D2y = diff(y, x, 2);
D3y = diff(y, x, 3);
D4y = diff(y, x, 4);
eqn6 = D4y == -sin(x) + cos(x);
cond6 = [y(0) == 0, Dy(0) == -1, D2y(0) == -1, D3y(0) == 7];
gsol6 = simplify(dsolve(eqn6))
psol6 = simplify(dsolve(eqn6, cond6))

% Displaying Problem 6
disp('Problem 6:')
disp('Given ODE:')
disp(eqn6)
disp('General Solution:')
disp(gsol6)
disp('Particular Solution with Initial Conditions:')
disp(psol6)

%%%%%%%%%%%%%%%%%%%%%%%%%% E7 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
syms x y(x)
Dy = diff(y, x);
D2y = diff(y, x, 2);
eqn7 = D2y == 2 - 6*x;
cond7 = [y(0) == 1, Dy(0) == 4];
gsol7 = simplify(dsolve(eqn7))
psol7 = simplify(dsolve(eqn7, cond7))

% Displaying Problem 7
disp('Problem 7:')
disp('Given ODE:')
disp(eqn7)
disp('General Solution:')
disp(gsol7)
disp('Particular Solution with Initial Conditions:')
disp(psol7)

OUTPUT:

sol1final =

1/((C3 - 2*tan(x))/cos(x)^2)^(1/2)
-1/((C3 - 2*tan(x))/cos(x)^2)^(1/2)

Problem 1:

Given ODE:

D(y)(x) + tan(x)*y(x) == y(x)^3/cos(x)^4

symbolic function inputs: x

Solution:

1/((C3 - 2*tan(x))/cos(x)^2)^(1/2)

-1/((C3 - 2*tan(x))/cos(x)^2)^(1/2)

Warning: Explicit solution could not be found; implicit solution returned.

> In dsolve at 201

In problem12 at 17

sol2final =

RootOf(z^5 + 5*z + (5*x*(x^2 + 3))/3 - 5*C6, z)

Problem 2:

Given ODE:

D(y)(x)*y(x)^4 + D(y)(x) + x^2 + 1 == 0

symbolic function inputs: x

Solution:

RootOf(z^5 + 5*z + (5*x*(x^2 + 3))/3 - 5*C6, z)

sol3final =

exp(x)/10 - exp(x*(5^(1/2)/2 - 3/2))*(exp(-(x*(5^(1/2) - 3))/2)*((9*5^(1/2))/10 + 3/2) - exp(-(x*(5^(1/2) -


5))/2)*(5^(1/2)/10 + 1/10)) + (9*5^(1/2))/10 - (5^(1/2)*exp(x))/10 + C9*exp(x*(5^(1/2)/2 - 3/2)) + C10*exp(-
x*(5^(1/2)/2 + 3/2)) - 3/2

Problem 3:

Given ODE:

3*D(y)(x) + D(D(y))(x) + y(x) == exp(x) – 3

symbolic function inputs: x

Solution:

exp(x)/10 - exp(x*(5^(1/2)/2 - 3/2))*(exp(-(x*(5^(1/2) - 3))/2)*((9*5^(1/2))/10 + 3/2) - exp(-(x*(5^(1/2) -


5))/2)*(5^(1/2)/10 + 1/10)) + (9*5^(1/2))/10 - (5^(1/2)*exp(x))/10 + C9*exp(x*(5^(1/2)/2 - 3/2)) + C10*exp(-
x*(5^(1/2)/2 + 3/2)) - 3/2
sol4final =

(2*exp(2*x))/15 - (9*exp(x))/8 + C12*cos(x) + C15*exp(x) + C13*sin(x) + (3*x*exp(x))/4 + C14*exp(-x)

Problem 4:

Given ODE:

D(D(D(D(y))))(x) - y(x) == 2*exp(2*x) + 3*exp(x)

symbolic function inputs: x

Solution:

(2*exp(2*x))/15 - (9*exp(x))/8 + C12*cos(x) + C15*exp(x) + C13*sin(x) + (3*x*exp(x))/4 + C14*exp(-x)

Warning: Explicit solution could not be found.

> In dsolve at 194

In problem12 at 58

gsol5 =

[ empty sym ]

Warning: Explicit solution could not be found.

> In dsolve at 194

In problem12 at 59

psol5 =

[ empty sym ]

Problem 5:

Given ODE:

D(y)(x)*D(D(y))(x)*(x + 3) - y(x) == 0

symbolic function inputs: x

General Solution:

[ empty sym ]

Particular Solution with Initial Conditions:

[ empty sym ]

gsol6 =

C26 + cos(x) - sin(x) + C25*x + (C23*x^3)/6 + (C24*x^2)/2

psol6 =

cos(x) - sin(x) + x^3 - 1

Problem 6:
Given ODE:

D(D(D(D(y))))(x) == cos(x) - sin(x)

symbolic function inputs: x

General Solution:

C26 + cos(x) - sin(x) + C25*x + (C23*x^3)/6 + (C24*x^2)/2

Particular Solution with Initial Conditions:

cos(x) - sin(x) + x^3 - 1

gsol7 =

C34 + x*(- x^2 + x + C33)

psol7 =

x*(- x^2 + x + 4) + 1

Problem 7:

Given ODE:

D(D(y))(x) == 2 - 6*x

symbolic function inputs: x

General Solution:

C34 + x*(- x^2 + x + C33)

Particular Solution with Initial Conditions:

x*(- x^2 + x + 4) + 1

You might also like