+ All Categories
Home > Documents > Code and Answers-VBKM

Code and Answers-VBKM

Date post: 18-May-2017
Category:
Upload: ashokallu16
View: 217 times
Download: 1 times
Share this document with a friend
26
Exp. No. 1 Basics of MATLAB >>a1=sin(cos(exp(log(25))))+100*(55/7-1000*(tan(.23)))-100 a1 = -2.2728e+004 >> a2=(3^4*log(126))/(7^3+115)+sqrt(920)+273^(1/3)+atan(pi/3)-pi a2 = 35.3408
Transcript
Page 1: Code and Answers-VBKM

Exp. No. 1

Basics of MATLAB

>>a1=sin(cos(exp(log(25))))+100*(55/7-1000*(tan(.23)))-100

a1 =

-2.2728e+004

>> a2=(3^4*log(126))/(7^3+115)+sqrt(920)+273^(1/3)+atan(pi/3)-pi

a2 =

35.3408

Page 2: Code and Answers-VBKM

Exp. No. 2

Basic mathematical operations on matrices.

a =

22 70 57

30 93 81

38 11 10

>> b=[22 30 38;70 93 11;57 81 10]

b =

22 30 38

70 93 11

57 81 10

>> a1=a+b

a1 =

44 100 95

100 186 92

95 92 20

>> a1=a-b

a1 =

0 40 19

-40 0 70

-19 -70 0

>> a1=a*b

a1 =

8633 11787 2176

11787 16110 2973

2176 2973 1665

Page 3: Code and Answers-VBKM

>> a1=a/b

a1 =

1.3224 -6.2418 7.5409

1.9125 -8.1537 9.8014

0.3443 6.5751 -7.5409

>> a1=a\b

a1 =

1.3224 1.9125 0.3443

-6.2418 -8.1537 6.5751

7.5409 9.8014 -7.5409

Page 4: Code and Answers-VBKM

Exp. No.3

Transpose, Determinant and Inverse of a matrix

a =

10 -2 -1 -1

-2 10 -1 -1

-1 -1 10 -2

-1 -1 -2 10

>> a1=a'

a1 =

>> a2=det(a)

a2 =

8640

>> a3=inv(a)

a3 =

0.1083 0.0250 0.0167 0.0167

0.0250 0.1083 0.0167 0.0167

0.0167 0.0167 0.1083 0.0250

0.0167 0.0167 0.0250 0.1083

Page 5: Code and Answers-VBKM

Exp. No.4

Solution of simultaneous algebraic equations by exact methods

Method 1: Gauss elimination method

% workout the number of equations.a=input('enter the coefficient matrix');b=input('enter the constant matrix');N=length(b);%gauss eliminationfor column=1:(N-1) %work on all the rows below diagonal element for row=(column+1):N %workout the value of d. d=a(row,column)/a(column,column); % do the row operation. a(row,:)=a(row,:)-d*a(column,:); b(row)=b(row)-d*b(column); end %loop through rows.end % loop through columns.% back substitution.for row=N:-1:1 x(row)=b(row); for i=(row+1):N x(row)=x(row)-a(row,i)*x(i); end x(row)=x(row)/a(row,row);end % return the answer.x=x'returnx=x'

Run

Result of Method 1:

enter the coefficient matrix[10 -2 -1 -1; -2 10 -1 -1; -1 -1 10 -2; -1 -1 -2 10]enter the constant matrix[3;15;27;-9]x = 1.0000 2.0000 3.0000 0.0000

Page 6: Code and Answers-VBKM

Method 2: Matrix Inverse method

>> a=[10 -2 -1 -1; -2 10 -1 -1; -1 -1 10 -2; -1 -1 -2 10]

a =

10 -2 -1 -1

-2 10 -1 -1

-1 -1 10 -2

-1 -1 -2 10

>> b=[3;15;27;-9]

b =

3

15

27

-9

>> a1=inv(a)*b

a1 =

1.0000

2.0000

3.0000

-0.0000

Page 7: Code and Answers-VBKM

Exp. No. 5

Solution of simultaneous algebraic equations by iteration methods

Method 1: Jacobian Iteration

clear;clc;format compact;A = input(‘input the coefficient matrix:’);C = input(‘input the 7teratio matrix:’);n = length(C);X = zeros(n,1);Error_Value = ones(n,1);for i=1:n; j=1:n;end 7teration = 0;while max(Error_Value) > 0.001 7teration = 7teration+1; Z=X; for i=1:n; j=1:n; j(i)=[]; Xtemp=Z; Xtemp(i) = []; X(i) = (C(i) – sum(A(i,j) * Xtemp)) / A(i,i); end Xsolution (:,7teration) = X Error_Value = sqrt((X – Z).^2);endThe_required_matrix =[X]

Run:

Result of Method 1

input the coefficient matrix:[10 -2 -1 -1; -2 10 -1 -1; -1 -1 10 -2; -1 -1 -2 10]

input the constant matrix:[3;15;27;-9]

Iteration No=1

solution =

0.3000

1.5000

2.7000

-0.9000

Iteration No=2

Page 8: Code and Answers-VBKM

solution =

0.7800

1.7400

2.7000

-0.1800

Iteration No=3

solution =

0.9000

1.9080

2.9160

-0.1080

Iteration No=4

solution =

0.9624

1.9608

2.9592

-0.0360

Iteration No=5

solution =

0.9845

1.9848

2.9851

-0.0158

Iteration No=6

solution =

0.9939

1.9938

Page 9: Code and Answers-VBKM

2.9938

-0.0060

Iteration No=7

solution =

0.9975

1.9975

2.9976

-0.0025

Iteration No=8

solution =

0.9990

1.9990

2.9990

-0.0010

Iteration No=9

solution =

0.9996

1.9996

2.9996

-0.0004

The_required_matrix =

0.9996

1.9996

2.9996

-0.0004

Page 10: Code and Answers-VBKM

Method 2: Gauss Seidal Iteration

clear;clc;format compact;A = input('input the coefficient matrix:');C = input('input the connstant matrix:');n = length(C);X = zeros(n,1);Error_Value = ones(n,1);for i=1:n; j=1:n;end itteration = 0;while max(Error_Value) > 0.001 itteration = itteration+1; Z=X; for i=1:n; j=1:n; j(i)=[]; Xtemp=X; Xtemp(i) = []; X(i) = (C(i) - sum(A(i,j) * Xtemp)) / A(i,i); end Xsolution (:,itteration) = X Error_Value = sqrt((X - Z).^2);endThe_required_matrix =[X]

Run:Result of Method 2:

input the coefficient matrix:[10 -2 -1 -1; -2 10 -1 -1; -1 -1 10 -2; -1 -1 -2 10]

input the connstant matrix:[3;15;27;-9]

Iteration No=1

solution =

0.3000

1.5600

2.8860

-0.1368

Iteration No=2

solution =

0.8869

1.9523

2.9566

Page 11: Code and Answers-VBKM

-0.0248

Iteration No=3

solution =

0.9836

1.9899

2.9924

-0.0042

Iteration No=4

solution =

0.9968

1.9982

2.9987

-0.0008

Iteration No=5

solution =

0.9994

1.9997

2.9998

-0.0001

Iteration No=6

solution =

0.9999

1.9999

3.0000

-0.0000

The_required_matrix =

0.9999

1.9999

3.0000

-0.0000

Page 12: Code and Answers-VBKM

Exp. No. 6Solution of Ordinary Differential Equations

using Finite Difference Methodclear;clc;format compact;% Solution of ODEs using Finite Difference Method% Example of 1-D steady state Heat conduction problem with Temp BCsn=input('Enter the Number of nodes :');a=zeros(n);b=zeros(n,1); % Governing Equation: Ti-1 - 2Ti + Ti+1 = 0% Boundary conditions i) T1=100 ii) Tn=20T1=100; Tn=20; j=0;for i=2:(n-1) j=j+1; a(i,j)=1; a(i,j+1)=-2; a(i,j+2)=1; b(i,1)=-a(i,1)*T1-a(i,n)*Tn;endfor i=1:(n-2) for j=1:(n-2) af(i,j)=a(i+1,j+1); bf(i,1)=b(i+1,1); endendt=af\bf;% Display Resultafbft

Run

Result of method :1

Enter the Number of nodes :5

af =

-2 1 0

1 -2 1

0 1 -2

bf =

-100

0

-20

Page 13: Code and Answers-VBKM

t =

80.0000

60.0000

40.0000

Result of method 2

Enter the Number of nodes :10

af =

-2 1 0 0 0 0 0 0

1 -2 1 0 0 0 0 0

0 1 -2 1 0 0 0 0

0 0 1 -2 1 0 0 0

0 0 0 1 -2 1 0 0

0 0 0 0 1 -2 1 0

0 0 0 0 0 1 -2 1

0 0 0 0 0 0 1 -2

bf =

-100

0

0

0

0

0

0

-10

t =

90.0000

Page 14: Code and Answers-VBKM

80.0000

70.0000

60.0000

50.0000

40.0000

30.0000

20.0000

Page 15: Code and Answers-VBKM

Exp. No. 7

Solution of Partial Differential Equations

using Finite Difference Method

clear;clc;format long%material=copper%Thermal conductivity : K=4450w/m.k%Density : row=8940kg/cubic m.%Specific heat : cp=300J/kg.k%diffusivity : Alpha = K/(row*Cp)=1.659 e-3alpha=1.659*10^-3;n=input('enter the number of nodes:');T=zeros(n);T(1,:)=100; T(n,:)=20; T(2,1)=0; T(3,1)=0;T(4,1)=0; deltat=1; deltax=0.25;for i=1:deltat:(n-1) for j=2:(n-1) T(j,i+deltat)=alpha*(deltat/((deltax)^2))*(T(j-1,i)-2*T(j,i)+T(j+1,i))+T(j,i); end % for L=2:(n-1) % T(L,i+1) %endEnd

run

Exp. No. 7 result for first 5 iterations

enter the number of nodes:5

>> T

T =

1.0e+002 *

Columns 1 through 4

1.000000000000000 1.000000000000000 1.000000000000000 1.000000000000000

0 0.026544000000000 0.051678832128000 0.075501749259185

0 0 0.000845500723200 0.002446730284814

0 0.005308800000000 0.010335766425600 0.015118304228794

0.200000000000000 0.200000000000000 0.200000000000000 0.200000000000000

Page 16: Code and Answers-VBKM

Column 5

1.000000000000000

0.098102458403194

0.004722256967238

0.019689449702576

0.200000000000000

Page 17: Code and Answers-VBKM

Exp. No. 8

Numerical Integration

clear;clc;format compact%Problem 1: Find Integration of the function (2+5*Zeta) with in the limits% -1 to +1 %Problem 2: Find Integration of the function (2+5*Zeta-3*Zeta*Zeta) with in % the limits -1 to +1a=input(‘enter the example number:’);b=input (‘Enter the Rule to be used for Numerical Integration :’);if a==1 if b==1 w1=2; zeta1=0; I11=w1*(2+5*zeta1) else w1=1; w2=1; zeta1=-1/3^(1/2); zeta2=1/3^(1/2); I12=w1*(2+5*zeta1)+w2*(2+5*zeta2) endelse if b==1 w1=2; zeta1=0; I21=w1*(2+5*zeta1-3*zeta1*zeta1) else w1=1; w2=1; zeta1=-1/3^(1/2); zeta2=1/3^(1/2); I22=w1*(2+5*zeta1-3*zeta1*zeta1)+w2*(2+5*zeta2-3*zeta2*zeta2) endend

Run

Page 18: Code and Answers-VBKM

Exp. No. 8 result

enter the example number:1

Enter the Rule to be used for Numerical Integration :1

I11 =

4

enter the example number:1

Enter the Rule to be used for Numerical Integration :2

I12 =

4

enter the example number:2

Enter the Rule to be used for Numerical Integration :1

I21 =

4

enter the example number:2

Enter the Rule to be used for Numerical Integration :2

I22 =

2.000000000000000

Page 19: Code and Answers-VBKM

Exp. No. 9

Line graphs and Surface plots

Line graphs

x =

Columns 1 through 4

0 0.104719755119660 0.209439510239320 0.314159265358979

Columns 5 through 8

0.418879020478639 0.523598775598299 0.628318530717959 0.733038285837618

Columns 9 through 12

0.837758040957278 0.942477796076938 1.047197551196598 1.151917306316257

Columns 13 through 16

1.256637061435917 1.361356816555577 1.466076571675237 1.570796326794897

Columns 17 through 20

1.675516081914556 1.780235837034216 1.884955592153876 1.989675347273536

Columns 21 through 24

2.094395102393195 2.199114857512855 2.303834612632515 2.408554367752175

Columns 25 through 28

2.513274122871835 2.617993877991494 2.722713633111154 2.827433388230814

Columns 29 through 32

2.932153143350473 3.036872898470133 3.141592653589793 3.246312408709453

Columns 33 through 36

3.351032163829113 3.455751918948772 3.560471674068432 3.665191429188092

Columns 37 through 40

3.769911184307752 3.874630939427412 3.979350694547072 4.084070449666731

Columns 41 through 44

4.188790204786391 4.293509959906051 4.398229715025710 4.502949470145371

Page 20: Code and Answers-VBKM

Columns 45 through 48

4.607669225265030 4.712388980384690 4.817108735504350 4.921828490624010

Columns 49 through 52

5.026548245743669 5.131268000863329 5.235987755982989 5.340707511102648

Columns 53 through 56

5.445427266222308 5.550147021341968 5.654866776461628 5.759586531581287

Columns 57 through 60

5.864306286700947 5.969026041820607 6.073745796940266 6.178465552059927

Column 61

6.283185307179586

>> y1=sin(x)

y1 =

Columns 1 through 4

0 0.104528463267653 0.207911690817759 0.309016994374947

Columns 5 through 8

0.406736643075800 0.500000000000000 0.587785252292473 0.669130606358858

Columns 9 through 12

0.743144825477394 0.809016994374947 0.866025403784439 0.913545457642601

Columns 13 through 16

0.951056516295154 0.978147600733806 0.994521895368273 1.000000000000000

Columns 17 through 20

0.994521895368273 0.978147600733806 0.951056516295154 0.913545457642601

Columns 21 through 24

0.866025403784439 0.809016994374947 0.743144825477394 0.669130606358858

Columns 25 through 28

0.587785252292473 0.500000000000000 0.406736643075800 0.309016994374948

Columns 29 through 32

Page 21: Code and Answers-VBKM

0.207911690817760 0.104528463267654 0.000000000000000 -0.104528463267654

Columns 33 through 36

-0.207911690817760 -0.309016994374947 -0.406736643075800 -0.500000000000000

Columns 37 through 40

-0.587785252292473 -0.669130606358858 -0.743144825477394 -0.809016994374947

Columns 41 through 44

-0.866025403784439 -0.913545457642601 -0.951056516295154 -0.978147600733806

Columns 45 through 48

-0.994521895368273 -1.000000000000000 -0.994521895368273 -0.978147600733806

Columns 49 through 52

-0.951056516295154 -0.913545457642601 -0.866025403784439 -0.809016994374948

Columns 53 through 56

-0.743144825477395 -0.669130606358858 -0.587785252292473 -0.500000000000000

Columns 57 through 60

-0.406736643075800 -0.309016994374948 -0.207911690817760 -0.104528463267653

Column 61

-0.000000000000000

>> y2=cos(x)

y2 =

Columns 1 through 4

1.000000000000000 0.994521895368273 0.978147600733806 0.951056516295154

Columns 5 through 8

0.913545457642601 0.866025403784439 0.809016994374947 0.743144825477394

Columns 9 through 12

0.669130606358858 0.587785252292473 0.500000000000000 0.406736643075800

Columns 13 through 16

0.309016994374947 0.207911690817759 0.104528463267654 0.000000000000000

Page 22: Code and Answers-VBKM

Columns 17 through 20

-0.104528463267653 -0.207911690817759 -0.309016994374947 -0.406736643075800

Columns 21 through 24

-0.500000000000000 -0.587785252292473 -0.669130606358858 -0.743144825477394

Columns 25 through 28

-0.809016994374947 -0.866025403784438 -0.913545457642601 -0.951056516295154

Columns 29 through 32

-0.978147600733806 -0.994521895368273 -1.000000000000000 -0.994521895368273

Columns 33 through 36

-0.978147600733806 -0.951056516295154 -0.913545457642601 -0.866025403784439

Columns 37 through 40

-0.809016994374947 -0.743144825477394 -0.669130606358858 -0.587785252292473

Columns 41 through 44

-0.500000000000000 -0.406736643075800 -0.309016994374948 -0.207911690817759

Columns 45 through 48

-0.104528463267653 -0.000000000000000 0.104528463267654 0.207911690817759

Columns 49 through 52

0.309016994374947 0.406736643075801 0.500000000000000 0.587785252292473

Columns 53 through 56

0.669130606358858 0.743144825477394 0.809016994374947 0.866025403784438

Columns 57 through 60

0.913545457642601 0.951056516295154 0.978147600733806 0.994521895368273

Column 61

1.000000000000000

>> plot(y1,y2,'r --')

Page 23: Code and Answers-VBKM

Surface Plots:


Recommended