All Lab Task
All Lab Task
All Lab Task
PESHAWAR PAKISTAN
Main Campus
Submitted To
Sir Anwar Shan
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);
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]';
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
% 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:
% 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
OUTPUT:
% For n = 10
n = 10; % Number of intervals
a = 0; % Lower limit
b = pi; % Upper limit
h = (b - a) / n; % Step size
OUTPUT:
% 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:
Solution:
1/((C3 - 2*tan(x))/cos(x)^2)^(1/2)
-1/((C3 - 2*tan(x))/cos(x)^2)^(1/2)
In problem12 at 17
sol2final =
Problem 2:
Given ODE:
Solution:
sol3final =
Problem 3:
Given ODE:
Solution:
Problem 4:
Given ODE:
Solution:
In problem12 at 58
gsol5 =
[ empty sym ]
In problem12 at 59
psol5 =
[ empty sym ]
Problem 5:
Given ODE:
D(y)(x)*D(D(y))(x)*(x + 3) - y(x) == 0
General Solution:
[ empty sym ]
[ empty sym ]
gsol6 =
psol6 =
Problem 6:
Given ODE:
General Solution:
gsol7 =
psol7 =
x*(- x^2 + x + 4) + 1
Problem 7:
Given ODE:
D(D(y))(x) == 2 - 6*x
General Solution:
x*(- x^2 + x + 4) + 1