You are on page 1of 19

# GOVT. ENGG.

COLLEGE SUNDERNAGAR

PRACTICAL FILE
ON MATLAB

BRANCH -ECE

4TH SEM

B.TECH

2012

## SUNDERNAGAR (H.P.) practical file On matlab

LIST OF EXPERIMENTS: i) Roots of a quadratic equation. ii) Guessing a number. iii) Units conversion. iv) Factorial Program v) Simulation of an RC circuit. vi) I-V characteristic of a MOSFET. vii) Finding average with a dynamic array. viii) Writing a binary file. ix) Reading a binary file. x) Plotting one and two-dimensional graphs using various MATLAB 2-D Plot types. xi) Using functions in MATLAB environment.

MATLAB Environment
The MATLAB high-performance language for technical computing 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. It 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 non-interactive 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 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. You can add on toolboxes for signal processing, control systems, neural networks, fuzzy logic, wavelets, simulation, and many other areas. Relational Operators < > <= >= == ~=
Relational operators compare operands quantitatively, using operators like "less than" and "not equal to"

Relational operations Syntax < <= > >= == ~= Less than Less than or equal to Greater than Greater than or equal to Equal to Not equal to

## Arithmetic Operators + - * / \ ^ '

Arithmetic operators perform numeric computations.

## Matrix and array arithmetic

Syntax

+ .* ./ .\ + : .^ .' ' * / \ ^

Addition Subtraction Multiplication Right division Left division Unary plus Unary minus Colon operator Power Transpose Complex conjugate transpose Matrix multiplication Matrix right division Matrix left division Matrix power

## Logical Operators: Short-circuit && ||

MATLAB offers three types of logical operators and functions:

Element-wise operate on corresponding elements of logical arrays. Bit-wise operate on corresponding bits of integer values or arrays. Short-circuit operate on scalar, logical expressions.

Syntax

## || Returns logical 1 if either input, or both evaluate to true.

Aim 1: Program for generation of sin, cos, impulse, ramp, exponential wave.{2-D plots}

A.

%program for generation of sin wave clear all; close all; t=0:.04:pi; y=sin(2*pi*t); subplot(2,2,1); stem(t,y); xlabel('time'); ylabel('amplitude');

## B. %program for generation of cos wave

clear all; close all; t=0:.04:pi; y=cos(2*pi*t); subplot(2,2,1); stem(t,y); xlabel('time'); ylabel('amplitude');

C. %program for generation of impulse signal clear all; close all; t=-2:1:2; y=[zeros(1,2),ones(1,1),zeros(1,2)]; subplot(2,2,1); stem(t,y); xlabel('time'); ylabel('amplitude');

D. %program for generation of exponential signal n=input('enter the length of exponential sequence') t=0:0.1:n; y=exp(t); subplot(2,2,1); stem(t,y); ylabel('amplitude'); xlabel('time');

## E. %program for generation of ramp signal clear all;

close all; n1=input('enter the length of ramp sequence'); t=0:.1:n1; a=input('enter value of slope'); y=a*t; subplot(2,2,1); stem(t,y); ylabel('amplitude'); xlabel('time');

Aim 2: Program to find roots of quadratic equation. a=input('enter the value of a='); b=input('enter the value of b='); c=input('enter the value of c='); x=b^2-4*a*c; x1=sqrt(x); r1=(-b+x1)/(2*a) r2=(-b-x1)/(2*a)

Aim 3: Program to find factorial of a given number . function factn=factorial(n) a=input('Enter the number whose factorial you want ') ; if a>=0 factn=1; for k=a:-1:1 factn=factn*k; end else if a<0 fprintf('Number you entered is not positive \n'); end end Aim 4: Program for unit conversion . c=[2;3]; f=(9/5)*c+32; k=c+273; tempcon=[c f k] Aim 5: Program for guessing a number. a=rand(1)*10; n=round(a); b=input('enter number between 1&10'); while (n~=b) %do while loop if (b<n) disp('number is less than computer has genrated'); end if (b>n) disp('number is greater than computer has genrated'); end b=input('enter number between 1&10'); end

disp('number is right'); Aim 6: Program for simulation of R-C circuit. vcf=5;r=1000;c=1e-6;to=0.0015;tf=0.008;vci=0; t=linspace(0,tf,1000); vc=zeros(1,1000) for i=1:1000; if t(i)<to vc(i)=vci; else vc(i)=vcf+(vci-vcf)*exp(-(t(i)-to)/(r*c)); end end plot(t*1000,vc); title('rc step response.'); ylabel('capacitor voltage(volts)'); xlabel('time(milliseconds)'); grid

Aim 7: Program using functions in MATLAB environment. FUNCTIONS: These take inputs and return outputs .These are saved by extension .m. We shall start with a very simple example:

1.

## function [output] = xsq(input) output = input.2;

The first line of xsq.m tells us this is a function called xsq which takes an input called input and returns a value called output.

The second line of this function actually performs the calculation, in this case squaring the value of the input, and storing this result in the variable output. Having written our function we can now access it from within MATLAB

x = 1:10; y = xsq(x)

2.
Suppose we now want to construct the squares and cubes of the elements of a vector. function [sq,cub] = xpowers(input) sq = input.2; cub = input.3; This function file must be saved as xpowers.m and it can be called as follows:

## x = 1:10; [xsq,xcub] = xpowers(x);

Aim 8: Plotting two-dimensional graphs using various MATLAB 2-D Plot types. plot(x,y): Plots the values in vector y versus x. Plot produces a piecewise linear graph between its data values.

Stem(x,y): Plots the data sequence y at the values specified in x. Xlabel('string'): Labels the x-axis with string. Ylabel('string'): Labels the y-axis with string. Title('string'): Gives the plot the title string. x = -2:0.2:2; y = x.^2; xlabel('x'); figure(1); ylabel('y=x^2'); plot(x,y); title('Simple stem plot'); xlabel('x'); ylabel('y=x^2'); title('Simple plot'); stem(x,y);

legend('plot1','plot2',...,'plot N'): The legend command provides an easy way to identify individual plots when there are more than one per figure. subplot(m,n,p): Divides the figure window into m rows, n columns and selects the pth subplot as the current plot. grid on/off: This command adds or removes a rectangular grid to your plot.

1.A.sin wave:

1.B.cos wave:

1.C.impulse signal:

## 1.D.exponential signal: enter the length of exponential sequence2 n=2

1.E.Ramp signal: enter the length of ramp sequence6 enter value of slope1

OUTPUT: 2.
enter the value of a=2 enter the value of b=3 enter the value of c=4 r1 = -0.7500 + 1.1990i r2 = -0.7500 - 1.1990i

3.
Enter the number whose factorial 5 ans = 120 Enter the number whose factorial you want -9 Number you entered is not positive

4.

## tempcon = 2.0000 35.6000 275.0000 3.0000 37.4000 276.0000

OUTPUT: 5.

enter number between 1 & 10 =5 number is less than computer has genrated enter number between 1 & 10 =8 number is less than computer has genrated enter number between 1 & 10 =9 number is right

6.

OUTPUT: 7.

7.1.
y= 1 4 9 16 25 36 49 64 81 100

7.2.
xsq = 1 4 9 16 25 36 49 64 81 100 xcub = Columns 1 through 7 1 8 27 64 125 216 343