You are on page 1of 13

A

Practical

On


Submitted to

CHHATTISGARH SWAMI VIVEKANAND TECHNICAL


UNIVERSITY, BHILAI (C.G), INDIA
Bachelor of Engineering

In

COMPUTER SCIENCE & ENGINEERING

SUBMITTED BY: SUBMITTED TO:

ROLL NO.
INDEX
S.No TITLE PAGE SIGNATURE
. NO.
1. AREA = r2 (USING ARITHMETIC OPERATOR). 1
2. COMPUTE Y- COORDINATES OF A STRAIGHT LINE 2
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 3-4
ELEMENTS 1 TO 10.
4. PLOT y = Sin x where 0 <= x <= 6.5 5
5. PLOT y = e-0.4x Sin x WHERE 0 <= x <= 10 6
6 WRITE A SCRIPT FILE TO DRAW A UNIT CIRCLE. 7
7 WRITE A FUNCTION FACTORIAL TO COMPUTE 8
THE FACTORIAL n! FOR ANY INTEGER n.
8 WRITE A FUNCTION FACTORIAL TO COMPUTE 9
THE FACTORIAL n! USING RECURSION FOR ANY
INTEGER n.
9 WRITE A FUNCTION FILE crossprod TO COMPUTE 10
THE CROSS PRODUCT OF TWO VECTORS u AND
v.
10 WRITE A FUNCTION TO COMPUTE THE 11
GEOMETRIC SERIES.
1 + r + r2 + r3 + + rn FOR GIVEN r AND n.
EXPERIMENT - 1

WRITE MATLAB PROGRAM FOR FOLLOWING.


A) AREA = r2 (USING ARITHMETIC OPERATOR).

Area=pi*r^2
r=5
Area = 78.5398

B) 5e3 (USING EXPONENTIAL OPERATOR).

A=5*exp(3)
A = 100.4277

C) y = Sin2 /3 + Cos2 /3 (USING TRIGONOMETRY OPERATOR).

y=((sin(pi/3))^2 + (cos(pi/3))^2);
disp(y)
1&nbsp;

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

y=(cos(pi/4) + sin(pi/4)*i);
disp(y)
0.7071 + 0.7071i

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

y=log10(10^6)
y=6
EXPERIMENT - 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.

% A script file to print y-coordinates of straight line.

m = 0.5;
c = -2;
disp(Y- Co-ordinates are);
for x=0: 0.5: 10
y = m*x + c;
disp(y);
disp(, );
end

Output: >>

straightline Y- Co-ordinates are -2, -1.7500, -1.5000, -1.2500, -1, -0.7500, -0.5000,
- 0.2500, 0, 0.2500, 0.5000, 0.7500, 1, 1.2500, 1.5000, 1.7500, 2, 2.2500, 2.5000,
2.7500, 3
EXPERIMENT - 3

CREATE FOLLOWING VECTORS T WITH 10 ELEMENTS 1 TO 10.

a) x = t sin(t) [ A MULTIPLY VECTORS]

disp('Multiple Vectors are');


t = 1: 1: 10;
r = sin(t);
x = r .* t;
disp(x);

Output:
Multiple Vectors are
0.8415 1.8186 0.4234 -3.0272 -4.7946 -1.6765 4.5989 7.9149 3.7091 -5.4402

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

disp(Divide Vectors are);


t = 1: 1: 10;
y = (t-1) ./ (t+1);
disp(y);

Output:
Divide Vectors are
0 0.3333 0.5000 0.6000 0.6667 0.7143 0.7500 0.7778 0.8000 0.8182

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


disp('EXPONENTIAL VECTORS');
t = 1: 1: 10;
z = [ sin(t.^2) ./ (t.^2) ];
disp(z)

Output:
EXPONENTIAL VECTORS
0.8415 -0.1892 0.0458 -0.0180 -0.0053 -0.0275 -0.0195 0.0144 -0.0078 -0.0051
EXPERIMENT - 4

PLOT y = Sin x where 0 <= x <= 6.5

x = 0: 0.25: 6.5;
y = sin(x);
plot(x,y);
EXPERIMENT - 5

PLOT y = e-0.4x Sin x WHERE 0 <= x <= 10

x = 0: .25: 10;
y = (exp(-0.4.*x)).*(sin(x));
plot(x,y)
EXPERIMENT - 6

WRITE A SCRIPT FILE TO DRAW A UNIT CIRCLE.

% Circle - A script file to draw a unit circle


theta = linspace(0,2*pi,101); % create a vector theta
x = cos(theta); % x-coordinates of circle
y = sin(theta); % y-coordinates of circle
plot(x,y); % plot the circle
axis('equal'); % set x and y scales equal
title('Circle of unit radius'); % set graph title
EXPERIMENT 7

WRITE A FUNCTION FACTORIAL TO COMPUTE THE FACTORIAL n! FOR ANY


INTEGER n.

n = input('Enter the number now ');


fact = 1;
for c=1: 1: n
fact = fact * c;
end
disp(fact);

Output:
Enter the number now 5
120
EXPERIMENT 8

WRITE A FUNCTION FACTORIAL TO COMPUTE THE FACTORIAL n! USING


RECURSION FOR ANY INTEGER n.

function y = fact(n)
% We have the highest number

y = n;
% We go down to 0

if n == 0
y = 1;
else
% We multiply by all the integers before ours,

% one at a time...

y = y * fact(n-1);
end

Output:
Enter the number now 5
120
EXPERIMENT 9

WRITE A FUNCTION FILE crossprod TO COMPUTE THE CROSS PRODUCT OF


TWO VECTORS u AND v.

function w = crossprod(u,v)
% We're assuming that both u and v are 3D vectors.
% Naturally, w = [w(1) w(2) w(3)]

w(1) = u(2)*v(3) - u(3)*v(2);


w(2) = u(3)*v(1) - u(1)*v(3);
w(3) = u(1)*v(2) - u(2)*v(1);

% Clear screen, clear previous variables and closes all figures


clc; close all; clear
% Supress empty lines in the output
format compact
% Define unit vectors

i = [1 0 0];
j = [0 1 0];
k = [0 0 1];

% Call the previously created function


w1 = crossprod(i,j)
w2 = crossprod(i,k)

Output: w1 = 0 0 1
w2 = 0 -1 0
EXPERMENT - 10

Write a function to compute the geometric series


1 + r + r2 + r3 + + rn FOR GIVEN r AND n.

function w = geocomp(r,n);
sum = 0;
for i=0: 1: n
sum = sum + ( i * r );
end
sum = sum+1;

You might also like