You are on page 1of 4

13/10/2017 MATLAB programs, for 7th sem CSE students | Tushar Kant

(HTTP://TUSHARKANT.COM/)

Home (http://tusharkant.com/) My Blog (http://tusharkant.com/tushars-blog) My Works (http://tusharkant.com/my-works)

About Me (http://tusharkant.com/?p=88) Privacy Policy (http://tusharkant.com/privacy-policy)

BLOG POST
Article from my blog

POSTED ON APRIL 29, 2013 (HTTP://TUSHARKANT.COM/2013/04/MATLAB-PROGRAMS-FOR-7TH-SEM-CSE-STUDENTS.HTML) BY TUSHAR


(HTTP://TUSHARKANT.COM/AUTHOR/TUSHAR)

MATLAB programs, for 7th sem CSE students

Programs coded, compiled and tested by- Tushar kant verma, for CSVTU & CVRU students
Semester: VII Branch: Computer Science & Engg.
Subject: Soft Computing Lab. Code: 322721 (22)
Total Practical Periods:50
Total Marks in End Semester Exam: 40
1. WRITE MATLAB PROGRAM FOR FOLLOWING.
A) AREA = r2 (USING ARITHMETIC OPERATOR).
1 Area=pi*r^2
2 r = 5
3 Area = 78.5398

B) 5e3 (USING EXPONENTIAL OPERATOR).


>>
1 A=5*exp(3)
2 A = 100.4277

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


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

http://tusharkant.com/2013/04/matlab-programs-for-7th-sem-cse-students.html 1/6
13/10/2017 MATLAB programs, for 7th sem CSE students | Tushar Kant

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


>>
1 y=(cos(pi/4) + sin(pi/4)*i);
2 disp(y)
3 0.7071 + 0.7071i

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


1 y=log10(10^6)
2 y = 6

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.
1 % A script file to print y-coordinates of straight line.
2
3 m = 0.5;
4 c = -2;
5 disp(Y- Co-ordinates are);
6 for x=0: 0.5: 10
7 y = m*x + c;
8 disp(y);
9 disp(, );
10 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
3. Create following vectors t with 10 elements 1 to 10.
a) x = t sin(t) [ A MULTIPLY VECTORS]
1 disp('Multiple Vectors are');
2 t = 1: 1: 10;
3 r = sin(t);
4 x = r .* t;
5 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]
1 disp(Divide Vectors are);
2 t = 1: 1: 10;
3 y = (t-1) ./ (t+1);
4 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]
1 disp('EXPONENTIAL VECTORS');
2 t = 1: 1: 10;
3 z = [ sin(t.^2) ./ (t.^2) ];
4 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
4. PLOT y = Sin x where 0 <= x <= 6.5.
1 x = 0: 0.25: 6.5;
2 y = sin(x);
3 plot(x,y);

Output:

http://tusharkant.com/2013/04/matlab-programs-for-7th-sem-cse-students.html 2/6
13/10/2017 MATLAB programs, for 7th sem CSE students | Tushar Kant

5. PLOT y = e-0.4x Sin x where 0 <= x <= 10.


1 x = 0: .25: 10;
2 y = (exp(-0.4.*x)).*(sin(x));
3 plot(x,y)

Output:

6. Write a script file to draw a unit circle.


1 % Circle - A script file to draw a unit circle
2
3 theta = linspace(0,2*pi,101); % create a vector theta
4 x = cos(theta); % x-coordinates of circle
5 y = sin(theta); % y-coordinates of circle
6 plot(x,y); % plot the circle
7 axis('equal'); % set x and y scales equal
8 title('Circle of unit radius'); % set graph title

Output:
>> prgcircle

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

http://tusharkant.com/2013/04/matlab-programs-for-7th-sem-cse-students.html 3/6
13/10/2017 MATLAB programs, for 7th sem CSE students | Tushar Kant

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


2 fact = 1;
3 for c=1: 1: n
4 fact = fact * c;
5 end
6 disp(fact);

Output:
Enter the number now 5
120
8. Write a function factorial to compute the factorial n! using RECURSION for any integer n.
1 function y = fact(n)
2 % We have the highest number
3
4 y = n;
5 % We go down to 0
6
7 if n == 0
8 y = 1;
9 else
10 % We multiply by all the integers before ours,
11
12 % one at a time...
13
14 y = y * fact(n-1);
15 end

9. Write a function file crossprod to compute the cross product of two vectors u and v.
1 function w = crossprod(u,v)
2 % We're assuming that both u and v are 3D vectors.
3
4 % Naturally, w = [w(1) w(2) w(3)]
5
6 w(1) = u(2)*v(3) - u(3)*v(2);
7 w(2) = u(3)*v(1) - u(1)*v(3);
8 w(3) = u(1)*v(2) - u(2)*v(1);
9
10 % Clear screen, clear previous variables and closes all figures
11
12 clc; close all; clear
13 % Supress empty lines in the output
14
15 format compact
16
17 % Define unit vectors
18
19 i = [1 0 0];
20 j = [0 1 0];
21 k = [0 0 1];
22
23 % Call the previously created function
24
25 w1 = crossprod(i,j)
26 w2 = crossprod(i,k)

Output:w1 = 0 0 1
w2 = 0 -1 0
10. Write a function to compute the geometric series
1 + r + r2 + r3 + + rn for given r and n.
1 function w = geocomp(r,n);
2 sum = 0;
3 for i=0: 1: n
4 sum = sum + ( i * r );
5 end
6 sum = sum+1;

http://tusharkant.com/2013/04/matlab-programs-for-7th-sem-cse-students.html 4/6

You might also like