**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).
```
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
```
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
```
**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
**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
**4. PLOT y = Sin x where 0 <= x <= 6.5.**
```
x = 0: 0.25: 6.5;
y = sin(x);
plot(x,y);
```
**Output:**
**![](http://3.bp.blogspot.com/-jYRrPTO76Vs/TtWJ9Dp3XCI/AAAAAAAAACY/2ZFbT4f4E2I/s320/4.jpg)**
**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)
```
**Output:**
**![](http://2.bp.blogspot.com/-ee73UmQpZWA/TtWKSJpeA2I/AAAAAAAAACk/BO0O0uo7cpA/s320/5.jpg)**
** **
**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
```
**Output:**
>> prgcircle
![](http://2.bp.blogspot.com/-K9qXBqUqCFQ/TtWKhjO0LpI/AAAAAAAAACw/ezcZoyFddLU/s320/6.jpg)
**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
**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
```
**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
**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;
```
Note: To run script, do as follows:
Write and save the file under the name ‘**Program\_Name.m**‘.
Now go back to the MATLAB **command window** and type the following command to execute the script file.
>>Program\_Name
Leave a comment