Hadiya
Hadiya
STATEMENT
Learn the basics of MATLAB.
CODE
1.
clc;clear all
h=0.25;
A=1:h:2;
B=A+1
2.
FOR 291 position:
clc;clear all
h=0.0025;
A=1:h:2;
B=A+1
B=A(291)
COMMAND WINDOW
1.
B =
2.
Columns 386 through 396
2.9625 2.9650 2.9675 2.9700 2.9725 2.9750 2.9775 2.9800 2.9825 2.9850 2.9875
B=
1.7250
PRACTICAL NO :02
STATEMENT
Learn matrix formation and solution of systems of linear equations.
Determinant of a matrix
Inverse of a matrix
Adjoint of a matrix
Transpose of a matrix
Traces of a matrix
Egon value and vector
Solution of system of linear equations
CODE:
clc; clear all
A=[1 6 ;6 -5];
A1=[1 6 5; 2 5 -4; 2 4 9]
A2=[1 6; 6 7; 2 7; -9 4];
size=(A2);
length=(A2);
D=det(A1);
I=inv(A1);
I=rats(I)
Tr=transpose(A1);
T=trace(A1);
[eig_vall eig_vec]=eig(A1)
A=[3 2 -1; 6 -4 3; 4 -2 3]
B=[1; 2; 11]
X=inv(A)*(B)
X=A\B
COMMAND WINDOW
A1 = 1 6 5
2 5 -4
2 4 9
eig_vall =
eig_vec =
A= 3 2 -1
6 -4 3
4 -2 3
B= 1
2
11
X=
-0.2353
4.2647
6.8235
X=
-0.2353
4.2647
6.8235
PRACTICAL NO :03
STATEMENT
1. Plot the following function, x ∈[0.1] , h= 0.01
2
3 x −2 x+1
(i) Y(x)= 4 x +1
2x + 3y =7
3x + 2y =13
4. Plot the following
CODE:
QUESTION NO 01
i)
clc;clear all
h=0.01;
x=0: h: 1;
y= (3*x.^2-2*x+1). /(4*x+1)
plot(x,y,'k*')
ii)
clc;clear all
h=0.01;
x=0:h:1;
y=x.^2.*exp(2*x)+log(2*x+3);
plot(x,y,'g*')
QUESTION NO 02
(i)
clc;clear all
h=pi./24;
x=-2*pi: h: 2*pi;
y=(sin(x)).^2+cos(2*x);
plot(x,y,'b')
(ii)
clc;clear all
h=pi/24;
x=-2*pi:h:2*pi;
y=x.^2.*sin(x)+3*(cos(2.*x))
plot(x,y,'g*')
QUESTION NO 03
Clc;clear all
x= 0: h: 1
y = (7-2*x)./3
Plot(x,y,’g*’)
Hold on
y1=(13-3*x)./2
Plot(x,y1,’b*’)
QUESTION NO 04
(i)
clc; clear all
n=50;
h=1/(n-1);
x=0: h:1;y=x;
[X,Y]=meshgrid(x,y);
Z=X.*Y.*(X-1).*(Y-1);
surf(X,Y,Z)
(ii)
clc;clear all
h=0.1;
x=0: h: 1; y=x;
[X,Y]=meshgrid(x,y);
Z=exp(X.*Y).*cos(X.*Y);
surf(X,Y,Z)
COMMAND WINDOW
Question 1 (i)
y=
Columns 1 through 12
1.0000 0.9426 0.8900 0.8417 0.7972 0.7562 0.7184 0.6834 0.6509 0.6208
0.5929 0.5669
Columns 13 through 24
0.5427 0.5202 0.4992 0.4797 0.4615 0.4445 0.4286 0.4138 0.4000 0.3871
0.3751 0.3639
Columns 73 through 84
0.2874 0.2905 0.2936 0.2969 0.3002 0.3036 0.3071 0.3106 0.3143 0.3180
0.3218 0.3256
Columns 85 through 96
0.3295 0.3335 0.3376 0.3417 0.3458 0.3501 0.3543 0.3587 0.3631 0.3675
0.3720 0.3766
Question 2 (ii)
y=
Columns 1 through 8
3.0000 7.8383 11.9821 15.3996 18.0864 20.0633 21.3728 22.0749
Columns 9 through 16
22.2426 21.9568 21.3014 20.3590 19.2066 17.9127 16.5347 15.1180
Columns 73 through 80
3.0000 1.4999 -0.3998 -2.6589 -5.2168 -7.9960 -10.9045 -13.8402
Columns 81 through 88
-16.6953 -19.3607 -21.7309 -23.7082 -25.2066 -26.1545 -26.4975 -26.1994
Columns 89 through 96
-25.2426 -23.6278 -21.3728 -18.5104 -15.0864 -11.1570 -6.7859 -2.0427
Column 97
3.0000
PRACTICAL NO :04
STATEMENT
For loop
CODE:
For loop
i)
clc;clear all
% % FOR LOOP
% % FACTORIAL OF 9
fact0=1;
n=9;
for i=1:n
fact0=fact0*i;
disp(fact0)
End
ii)
% % hilbert matrix
n=4;
h=zeros(n);
for i=1:n
for j=1:n
h(i,j)=1/(i+j-1)
end
end
h
h1=hilb(n)
iii)
% % geometric series and its sum
a=3; n=5; r=4;
x=a;
for i=2:n
x=x.*r
gs(i)=x
end
s=(a*(r.^n-1))./(r-1);
While loop
(i)
clc;clear all
% % while loop
% % calculate factorial of 11
n=11; f=n;
while n>1
n=n-1;
f=f*n;
disp(f)
End
ii)
clc;clear all
% % Mean of the data point
N=0;sum_x=0;
x=input('enter 1st value')
while x>=0
sum_x=sum_x+x;
N=N+1;
x=input('enter next value')
end
disp('the mean of the data points')
mean_x=sum_x/N
COMMAND WINDOW
for loop
i)
1
2
6
24
120
720
5040
40320
362880
ii)
h=
1 0 0 0 h=
0 0 0 0 1.0000 0.5000 0.3333 0.2500
0 0 0 0 0.5000 0.3333 0.2500 0
0 0 0 0 0 0 0 0
0 0 0 0
h=
1.0000 0.5000 0 0 h=
0 0 0 0 1.0000 0.5000 0.3333 0.2500
0 0 0 0 0.5000 0.3333 0.2500 0.2000
0 0 0 0 0 0 0 0
0 0 0 0
h=
1.0000 0.5000 0.3333 0 h=
0 0 0 0 1.0000 0.5000 0.3333 0.2500
0 0 0 0 0.5000 0.3333 0.2500 0.2000
0 0 0 0 0.3333 0 0 0
h= 0 0 0 0
1.0000 0.5000 0.3333 0.2500
0 0 0 0 h=
0 0 0 0 1.0000 0.5000 0.3333 0.2500
0 0 0 0 0.5000 0.3333 0.2500 0.2000
h= 0.3333 0.2500 0 0
1.0000 0.5000 0.3333 0.2500 0 0 0 0
0.5000 0 0 0
0 0 0 0 h=
0 0 0 0 1.0000 0.5000 0.3333 0.2500
h= 0.5000 0.3333 0.2500 0.2000
0.3333 0.2500 0.2000 0
1.0000 0.5000 0.3333 0.2500 0 0 0 0
0.5000 0.3333 0 0
0 0 0 0
0 0 0 0 h=
iii)
x= gs =
12 0 12 48
x=
gs = 192
0 12 gs =
0 12 48 192
x= x=
48 768
gs =
0 12 48 192 768
While loop
i)
110
990
7920
55440
332640
1663200
6652800
19958400
39916800
39916800
ii)
STATEMENT
Solve the equation by using newton raphson method with a stopping criteria of 0.01% and
sketch the numerical solution of the equation graphically.
CODE:
NRM_Values =
1.4877
1.1915
1.1215
1.1183
1.1183
1.1183
1.1183
1.1183
1.1183
1.1183
f= @(x)exp(x)*sin(x)-3*x^2+1
PRACTICAL NO :06
STATEMENT
Solve the equation by using Secant method with a stopping criteria of 0.01% and sketch the
numerical solution of the equation graphically.
CODE:
clc; clear all
f=@(x) exp(x)*sin(x)-3*x^2+1;
x0=2.6; x1=4.5; n=12; % x0 and x1 are initial Values
epi=10^(-4); % Stopping Criteria
For i=1:n
x(i)=(x0*f(x1)-x1*f(x0))/(f(x1)-f(x0)); % Secant Method Formula
if abs(x(i)-x0) <epi
break
end
x0=x1; % Initial Condition for next iteration
x1=x(i); % Initial Condition for next iteration
end
Secant_Values=x'
plot(Secant_Values,'ko-')
title('Numerical Solution of Equation by Secant Method')
xlabel('Number of Iterations')
Ylabel('Values of Secant Method')
legend('Secant Values')
Grid minor
COMMAND WINDOW
Secant_Values =
2.4269
2.2884
1.7752
1.4772
1.2403
1.1428
1.1201
1.1183
1.1183
1.1183
PRACTICAL NO :07
STATEMENT
Solve the system of linear equation by using Jacobi method with a stopping criteria of 0.01% and
also compare the solution with actual results
CODE:
clc; clear all
A=[9 2 -3; 4 -8 2; 5 -3 10];
B=[5; 9; 15];
IV=[0;0;0]; %% Initial Values
n=4;
epi=0.00001;
N=length(B);
X=zeros(N,1);
for j = 1:n %% n = no. of iterations
for i = 1:N %% N = no. of unknowns
X(i)=B(i)/A(i,i)-(A(i,[1:i-1,i+1:N])*IV([1:i-1,i+1:N]))/A(i,i);
end
disp(‘Values of X by Jacobbi method are’)
fprintf(‘teration no. %d\n’,j);
X
if abs(X-IV) < epi
break
end
IV=X;
end
COMMAND WINDOW
X=
0.5556
-1.1250
1.5000
X=
1.3056
-0.4722
0.8847
X=
0.9554
-0.2510
0.7056
X=
0.8465
-0.4709
0.9470
PRACTICAL NO :08
STATEMENT
Solve the system of linear equation by using Gauss-Seidel method with a stopping criteria of
0.01% and also compare the solution with actual results
CODE:
Y=
0.5556 Y=
-0.8472 0.9570
0.9681 -0.4202 0.8954
X=
1.0665
-0.3497
0.8618
Y=
1.0665
-0.3497
0.8618
X=
0.9205
-0.4493
0.9049
Y=
0.9205
-0.4493
0.9049
STATEMENT
Evaluate the integral
By using (a) Trapezoidal rule, (b) Simpson 1/3rd rule, and (c) Simpson 3/8th rule, also compare your
results with actual solution of the integral. Taking no. of sub-intervals n=6
CODE:
clc; clear all;
a=0; b=1; n=6; h=(b-a)/n;
x=0:h:1;
y=x.^2.*exp(-x.^5);
y0=y(1); y1=y(2); y2=y(3); y3=y(4); y4=y(5); y5=y(6); y6=y(7);
%% Actual Solution
f=@(x) x.^2.*exp(-x.^5);
Actual=integral(f,0,1);
%% Trapezoidal Rule
Trapez=h/2*((y0+y(end))+2*(y1+y2+y3+y4+y(end-1)))
Error_Trapez=abs(Trapez-Actual)
%% Simpson 1/3rd Rule
Simp13=h/3*((y0+y6)+4*(y1+y3+y5)+2*(y2+y4));
Error_Simp13=abs(Simp13-Actual)
%% Simpson 3/8th Rule
Simp38=3*h/8*((y0+y6)+3*(y1+y2+y4+y5)+2*y3);
Error_Simp38=abs(Simp38-Actual)
%% Compare Results
Comp_res=[Actual Actual Actual;
Trapez Simp13 Simp38;
Error_Trapez Error_Simp13 Error_Simp38]
Min_Err=min([Error_Trapez Error_Simp13 Error_Simp38])
COMMAND WINDOW
Trapez = 3.1826e-04
0.2365
Comp_res =
Error_Trapez = 0.2391 0.2391 0.2391
0.0026 0.2365 0.2393 0.2394
0.0026
Error_Simp13 =
2.0413e-04 Min_Err =
Error_Simp38 = 2.0413e-04
0.0002 0.0003