Cse 7 softcomputing lab

24
Shri Rawatpura Sarkar Institute of Technology-II, New Raipur Department of Computer Science & Engineering INTRODUCTION TO MATLAB MATLAB is a high-performance language for technical computing. It integrates computation, visualization, and programming in an easy-to-use environment where problems and solutions are expressed in familiar mathematical notation. Typical uses include Math and computation Algorithm development Data acquisition Modeling, simulation, and prototyping Data analysis, exploration, and visualization Scientific and engineering graphics Application development, including graphical user interface building MATLAB is an interactive system whose basic data element is an array that does not require dimensioning. This allows you to solve many technical computing problems, especially those with matrix and vector formulations, in a fraction of the time it would take to write a program in a scalar noninteractive language such as C or Fortran. The name MATLAB stands for matrix laboratory. MATLAB was originally written to provide easy access to matrix software developed by the LINPACK and EISPACK projects. Today, MATLAB engines incorporate the LAPACK and BLAS SRIT-II, New Raipur / Department of CSE/Vivek Kumar Sinha/ Soft Computing Lab 1

Transcript of Cse 7 softcomputing lab

Page 1: Cse 7 softcomputing lab

Shri Rawatpura Sarkar Institute of Technology-II, New RaipurDepartment of Computer Science & Engineering

INTRODUCTION TO MATLAB

MATLAB is a high-performance language for technical computing. It integrates

computation, visualization, and programming in an easy-to-use environment where

problems and solutions are expressed in familiar mathematical notation. Typical uses

include Math and computation Algorithm development Data acquisition Modeling,

simulation, and prototyping Data analysis, exploration, and visualization Scientific and

engineering graphics Application development, including graphical user interface

building

MATLAB is an interactive system whose basic data element is an array that does not

require dimensioning. This allows you to solve many technical computing problems,

especially those with matrix and vector formulations, in a fraction of the time it would

take to write a program in a scalar noninteractive language such as C or Fortran.

The name MATLAB stands for matrix laboratory. MATLAB was originally written to

provide easy access to matrix software developed by the LINPACK and EISPACK

projects. Today, MATLAB engines incorporate the LAPACK and BLAS libraries,

embedding the state of the art in software for matrix computation.

MATLAB has evolved over a period of years with input from many users. In university

environments, it is the standard instructional tool for introductory and advanced courses

in mathematics, engineering, and science. In industry, MATLAB is the tool of choice

for high-productivity research, development, and analysis.

MATLAB features a family of add-on application-specific solutions called toolboxes.

Very important to most users of MATLAB, toolboxes allow you to learn and apply

specialized technology. Toolboxes are comprehensive collections of MATLAB

functions (M-files) that extend the MATLAB environment to solve particular classes of

problems. Areas in which toolboxes are available include signal processing, control

systems, neural networks, fuzzy logic, wavelets, simulation, and many others.

SRIT-II, New Raipur / Department of CSE/Vivek Kumar Sinha/ Soft Computing Lab 1

Page 2: Cse 7 softcomputing lab

Shri Rawatpura Sarkar Institute of Technology-II, New RaipurDepartment of Computer Science & Engineering

The MATLAB system consists of five main parts:

Development Environment. This is the set of tools and facilities that help you use

MATLAB functions and files. Many of these tools are graphical user interfaces. It

includes the MATLAB desktop and Command Window, a command history, an editor

and debugger, and browsers for viewing help, the workspace, files, and the search path.

The MATLAB Mathematical Function Library. This is a vast collection of

computational algorithms ranging from elementary functions, like sum, sine, cosine,

and complex arithmetic, to more sophisticated functions like matrix inverse, matrix

eigenvalues, Bessel functions, and fast Fourier transforms.

The MATLAB Language. This is a high-level matrix/array language with control

flow statements, functions, data structures, input/output, and object-oriented

programming features. It allows both "programming in the small" to rapidly create

quick and dirty throw-away programs, and "programming in the large" to create large

and complex application programs.

Graphics. MATLAB has extensive facilities for displaying vectors and matrices as

graphs, as well as annotating and printing these graphs. It includes high-level functions

for two-dimensional and three-dimensional data visualization, image processing,

animation, and presentation graphics. It also includes low-level functions that allow you

to fully customize the appearance of graphics as well as to build complete graphical

user interfaces on your MATLAB applications.

The MATLAB Application Program Interface (API). This is a library that allows

you to write C and Fortran programs that interact with MATLAB. It includes facilities

for calling routines from MATLAB (dynamic linking), calling MATLAB as a

computational engine, and for reading and writing MAT-files.SRIT-II, New Raipur / Department of CSE/Vivek Kumar Sinha/ Soft Computing Lab 2

Page 3: Cse 7 softcomputing lab

Shri Rawatpura Sarkar Institute of Technology-II, New RaipurDepartment of Computer Science & Engineering

EXPERIMENT NO. 1

AIM :Write MATLAB programs for the following

A) AREA = Πr2 (USING ARITHMETIC OPERATOR).

PROGRAM:

>> R=input('ENTER THE VALUE OF RADIUS:=')

ENTER THE VALUE OF RADIUS:=5

R =

5

>> AREA=pi*R^2

AREA =

78.5398

B) e= π π (150) (USING EXPONENTIAL OPERATOR).

PROGRAM:

>> e=pi^(pi*100)

e =

1.5284e+156

SRIT-II, New Raipur / Department of CSE/Vivek Kumar Sinha/ Soft Computing Lab 3

Page 4: Cse 7 softcomputing lab

Shri Rawatpura Sarkar Institute of Technology-II, New RaipurDepartment of Computer Science & Engineering

C) y = Sin2 π/3 + Cos2 π/3

(USING TRIGONOMETRY OPERATOR).

PROGRAM :

>> y=(sin (pi/3))^2+(cos(pi/3))^2

y =

1

D) y = Cos π/4 + i Sin π/4 (USING COMPLEX NUMBER).

PROGRAM :

>> y=cos (pi/4 )+ i*sin( pi/4)

y =

0.7071 + 0.7071i

E) y= log10(106) (USING LOGARITHMS OPERATOR).

PROGRAM :

>> y=log10(10^6)

y = 6

SRIT-II, New Raipur / Department of CSE/Vivek Kumar Sinha/ Soft Computing Lab 4

Page 5: Cse 7 softcomputing lab

Shri Rawatpura Sarkar Institute of Technology-II, New RaipurDepartment of Computer Science & Engineering

EXPERIMENT NO. 2

AIM:Compute y- coordinates of a STRAIGHT LINE y = mx + c, where slope of line m =0.5 , intercept c= -2 and x- coordinates : x = 0 to 10 for 0.5 increments.

PROGRAM :

>> x=(0:.5:4)

x =

0 0.5000 1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000

>> c=-2

c =

-2

>> m=.5

m =

0.5000

>> y=m*x+c

y =

-2.0000 -1.7500 -1.5000 -1.2500 -1.0000 -0.7500 -0.5000 -0.2500 0

SRIT-II, New Raipur / Department of CSE/Vivek Kumar Sinha/ Soft Computing Lab 5

Page 6: Cse 7 softcomputing lab

Shri Rawatpura Sarkar Institute of Technology-II, New RaipurDepartment of Computer Science & Engineering

EXPERIMENT NO. 3

AIM:Create following vectors t with 10 elements 1 to 10.

A) x = t sin(t) [ A MULTIPLE VECTORS]

PROGRAM :

>> t=1:10

t =

1 2 3 4 5 6 7 8 9 10

>> x=t .* sin(t)

x =

Columns 1 through 9

0.8415 1.8186 0.4234 -3.0272 -4.7946 -1.6765 4.5989 7.9149 3.7091

Column 10

-5.4402

B) y = (t-1) / (t+1) [ A DIVIDE VECTORS]

PROGRAM :>> t=1:10

t =

1 2 3 4 5 6 7 8 9 10

>> (t-1)./(t+1)SRIT-II, New Raipur / Department of CSE/Vivek Kumar Sinha/ Soft Computing Lab 6

Page 7: Cse 7 softcomputing lab

Shri Rawatpura Sarkar Institute of Technology-II, New RaipurDepartment of Computer Science & Engineering

ans =

0.7426

c) z = [sin(t2)/ (t2) [ A EXPONENTIAL VECTORS]

PRGRAM :

>> t=1:10

t =

1 2 3 4 5 6 7 8 9 10

>> z=sin(t.^2)./(t.^2)

z =

0.0113

SRIT-II, New Raipur / Department of CSE/Vivek Kumar Sinha/ Soft Computing Lab 7

Page 8: Cse 7 softcomputing lab

Shri Rawatpura Sarkar Institute of Technology-II, New RaipurDepartment of Computer Science & Engineering

EXPERIMENT NO. 4

AIM : PLOT y = Sin x where 0 ≤x≤2π.

PROGRAM :

>> x = linspace (0, 2*pi )

x =

Columns 1 through 9

0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077

Columns 10 through 18

0.5712 0.6347 0.6981 0.7616 0.8251 0.8885 0.9520 1.0155 1.0789

Columns 19 through 27

1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501

Columns 28 through 36

1.7136 1.7771 1.8405 1.9040 1.9675 2.0309 2.0944 2.1579 2.2213

Columns 37 through 45

2.2848 2.3483 2.4117 2.4752 2.5387 2.6021 2.6656 2.7291 2.7925

Columns 46 through 54

2.8560 2.9195 2.9829 3.0464 3.1099 3.1733 3.2368 3.3003 3.3637

Columns 55 through 63

SRIT-II, New Raipur / Department of CSE/Vivek Kumar Sinha/ Soft Computing Lab 8

Page 9: Cse 7 softcomputing lab

Shri Rawatpura Sarkar Institute of Technology-II, New RaipurDepartment of Computer Science & Engineering

3.4272 3.4907 3.5541 3.6176 3.6811 3.7445 3.8080 3.8715 3.9349

Columns 64 through 72

3.9984 4.0619 4.1253 4.1888 4.2523 4.3157 4.3792 4.4427 4.5061

Columns 73 through 81

4.5696 4.6331 4.6965 4.7600 4.8235 4.8869 4.9504 5.0139 5.0773

Columns 82 through 90

5.1408 5.2043 5.2677 5.3312 5.3947 5.4581 5.5216 5.5851 5.6485

Columns 91 through 99

5.7120 5.7755 5.8389 5.9024 5.9659 6.0293 6.0928 6.1563 6.2197

Column 100

6.2832

SRIT-II, New Raipur / Department of CSE/Vivek Kumar Sinha/ Soft Computing Lab 9

Page 10: Cse 7 softcomputing lab

Shri Rawatpura Sarkar Institute of Technology-II, New RaipurDepartment of Computer Science & Engineering

>>plot ( x , sin(x))

EXPERIMENT NO. 5SRIT-II, New Raipur / Department of CSE/Vivek Kumar Sinha/ Soft Computing Lab 10

Page 11: Cse 7 softcomputing lab

Shri Rawatpura Sarkar Institute of Technology-II, New RaipurDepartment of Computer Science & Engineering

AIM:Write a script file to draw a unit circle.

PROGRAM:

% circle – A script file to draw a unit circle.%------------------------------------------------------1 theta = linspace(0, 2*pi,100);2 x = cos (theta);3 y = sin (theta); Write a script file to draw a unit circle.

4 plot (x,y);5 axis(‘equal’);6 title(‘circle of unit radias’)

EXPERIMENT NO. 6SRIT-II, New Raipur / Department of CSE/Vivek Kumar Sinha/ Soft Computing Lab 11

Page 12: Cse 7 softcomputing lab

Shri Rawatpura Sarkar Institute of Technology-II, New RaipurDepartment of Computer Science & Engineering

Aim . Write a function factorial to compute the factorial n! for any integer n.

Program :

% FACTORIAL : function to compute factorial n!% call syntax :% fact = factorial (n);%---------------------------------------

n=input('enter a number :')

factorial(n)

% .....save to fh.m

>>fhenter a number :7

n =

7

ans =

5040

EXPERIMENT NO. 7SRIT-II, New Raipur / Department of CSE/Vivek Kumar Sinha/ Soft Computing Lab 12

Page 13: Cse 7 softcomputing lab

Shri Rawatpura Sarkar Institute of Technology-II, New RaipurDepartment of Computer Science & Engineering

AIM: Write a function file crossprod to compute the cross product of two vectors u and v.

PROGRAM :

% function w = crossprod(u,v);

% CROSSPROD: function to compute w =u × v for vector u & v.

% call syntax :

% w= crossprod(u,v);

% ---------------------------------------------------

If length(u)>3 / length(v)>3

error(‘ask eular.this cross prodct is beyond me.’)

end

w = [u(2)*v(3) – u(3)*v(2);u(3)*v(1) – u(1)*v(3);u(1)*v(2) – u(2)*v(1)];

Examples

a = [1 2 3]; b = [4 5 6];c = cross(a,b)

c = -3 6 -3

d = dot(a,b)

d = 32

EXPERIMENT NO.8

SRIT-II, New Raipur / Department of CSE/Vivek Kumar Sinha/ Soft Computing Lab 13

Page 14: Cse 7 softcomputing lab

Shri Rawatpura Sarkar Institute of Technology-II, New RaipurDepartment of Computer Science & Engineering

AIM : Check following linear algebra rule for three MATRIX A,B AND C of any ranks.

A) ADDITION COMMUTATIVE.B) ADDITION ASSOCIATIVE.C) MULTIPLICATION WITH A SCALAR DSTRIBUTIVE.D) MULTIPLICATION WITH A MATRIX DSTRIBUTIVE

PROGRAM :

>> A=[2,6;3,9]

A =

2 6 3 9

>> B=[1,2;3,4]

B =

1 2 3 4

>> C=[-5,5;5,3]

C =

-5 5 5 3

>> Y=[(A+B)+C]

Y =

SRIT-II, New Raipur / Department of CSE/Vivek Kumar Sinha/ Soft Computing Lab 14

Page 15: Cse 7 softcomputing lab

Shri Rawatpura Sarkar Institute of Technology-II, New RaipurDepartment of Computer Science & Engineering

-2 13 11 16

>> Z=[A+(B+C)]

Z =

-2 13 11 16

%MULTIPLICATION WITH A SCALAR DSTRIBUTIVE

>>Z=5*[A+B]

Z =

15 40 30 65

>> Z=[A*B]+[A*C]

Z =

40 56 60 84

EXPERIMENT NO.09SRIT-II, New Raipur / Department of CSE/Vivek Kumar Sinha/ Soft Computing Lab 15

Page 16: Cse 7 softcomputing lab

Shri Rawatpura Sarkar Institute of Technology-II, New RaipurDepartment of Computer Science & Engineering

AIM : Find the solution of following linear algebraic equations.x + 2 y + 3 z = 13x + 3 y + 4 z = 12x + 3 y + 3 z = 2

PROGRAM

>> A=[ 1 2 3 ;3 3 4;2 3 3]

A =

1 2 3 3 3 4 2 3 3

>> B=[1;1 ;2]

B =

1 1 2

>> C=A*X

C =

1 1 2

>> X=A\B

X =

-0.5000 1.5000 -0.5000

EXPERIMENT NO. 10SRIT-II, New Raipur / Department of CSE/Vivek Kumar Sinha/ Soft Computing Lab 16

Page 17: Cse 7 softcomputing lab

Shri Rawatpura Sarkar Institute of Technology-II, New RaipurDepartment of Computer Science & Engineering

AIM: Find Eigen values and eigenvector of a 3 X 3 matrix.

PROGRAM :>> a=[1 2 3;4 5 6;7 8 9]

a =

1 2 3 4 5 6 7 8 9

>> E=eig(a)

E =

16.1168 -1.1168 -0.0000

>> [eigvec,eigval]=eig(a)

eigvec =

-0.2320 -0.7858 0.4082 -0.5253 -0.0868 -0.8165 -0.8187 0.6123 0.4082

eigval =

16.1168 0 0 0 -1.1168 0 0 0 -0.0000

CHHATTISGARHSWAMIVIVEKANANDTECHNICALUNIVERSITY,BHILAI (C.G.)

SRIT-II, New Raipur / Department of CSE/Vivek Kumar Sinha/ Soft Computing Lab 17

Page 18: Cse 7 softcomputing lab

Shri Rawatpura Sarkar Institute of Technology-II, New RaipurDepartment of Computer Science & Engineering

Semester: VII Branch: Computer Science &Engg.Subject: Soft Computing Lab. Code: 322721 (22)Total Practical Periods:50Total Marks in End Semester Exam: 401. WRITE MATLAB PROGRAM FOR FOLLOWING.A) AREA = Πr2(USING ARITHMETIC OPERATOR).B) e= π π(150)(USING EXPONENTIAL OPERATOR).C) y = Sin2 π/3 + Cos2 π/3 (USING TRIGONOMETRY OPERATOR).D) y = Cos π/4 + i Sin π/4 (USING COMPLEX NUMBER).E) y= log10(106) (USING LOGARITHMS OPERATOR).2. Compute y- coordinates of a STRAIGHT LINE y = mx + c, where slope of line m =0.5 , intercept c= -2 and x- coordinates : x = 0 to 10 for 0.5 increments.3. Create following vectors t with 10 elements 1 to 10.a) x = t sin(t) [ A MULTIPLE VECTORS}b) y = (t-1) / (t+1) [ A DIVIDE VECTORS}c) z = [sin(t2)/ (t2) [ A EXPONENTIAL VECTORS}4. PLOT y = Sin x where 0 x2π.5. PLOT y = e - 0..4 x Sin x where 0 x 4π.6. Write a script file to draw a unit circle.7. Write a function factorial to compute the factorial n! for any integer n..8 Write a function file crossprod to compute the cross product of two vectors u and v.9. Write a function to compute the geometric series1 + r + r2 + r3 + ………+ rnfor given r and n.10. Write a function to computes the interest on your account for a given principle amount,period and rate of interest.11. Check following linear algebra rule for three MATRIX A,B AND C of any ranks.a) ADDITION COMMUTATIVE.b) ADDITION ASSOCIATIVE.c) MULTIPLICATION WITH A SCALAR DSTRIBUTIVE.d) MULTIPLICATION WITH A MATRIX DSTRIBUTIVEe) MATRIX ARE DIFFERENT FROM SCALAR.12. Find the solution of following linear algebraic equtions.x + 2 y + 3 z = 13x + 3 y + 4 z = 12x + 3 y + 3 z = 213. Find Eigen values and eigenvector of a 3 X 3 matrix.

SRIT-II, New Raipur / Department of CSE/Vivek Kumar Sinha/ Soft Computing Lab 18