SlideShare uma empresa Scribd logo
1 de 55
Documentation
On
MATLAB
Report submitted in partial fulfillment of the requirement of the award of the
Degree of Master of Technology in
Computational Engineering
Submitted on
(11-12-2013)
Submitted By
MANCHIREDDY VIKAS VARDHAN REDDY B61301007
Department of Computational Engineering (2013-2015)
Rajiv Gandhi University of Knowledge Technologies
Basar, Adilabad Dist – 504107
Table of Contents
MATLAB MODULE 1:.................................................................................................................. 3
MATLAB MODULE 2:................................................................................................................ 10
MATLAB MODULE 3:................................................................................................................ 14
MATLAB MODULE 4:................................................................................................................ 16
MATLAB MODULE 5:................................................................................................................ 19
MATLAB MODULE 6:................................................................................................................ 23
MATLAB MODULE 7:................................................................................................................ 30
MATLAB MODULE 8:................................................................................................................ 40
MATLAB MODULE 9:................................................................................................................ 44
MATLAB MODULE 10:............................................................................................................. 50
MATLAB MODULE 1
1. Compare MATLAB with other high level language
Answer
Comparison: MATLAB Scripts & BASIC
There are many similarities between the language used for Matlab scripts and more
the more familiar BASIC programming language (and other higher level languages
such as FORTRAN PASCAL or C.
This document may help when becoming familiar with Matlab scripts.
* * * * * * * BASIC * * * * * * * * * * * * * * MATLAB * * * * * * *
Variable Assignments
In Matlab, all variable assignments are case sensitive, so the variable Test1 is different from the
variable test1. In Matlab, if an assignment is terminated with a semicolon, Matlab does not
print out the result. If it is not terminated by a semicolon, it will print out the results of the
assignment (In the Matlab code below, there is no semicolon after the assignment for A. This
causes Matlab to print the answer).
R = 2
P = 3.14
A = P*R^2
PRINT A
12.56
R = 2;
P = 3.14;
A = P*R^2
ans =
12.560
Printing
PRINT "Area = ";A;" m^2" disp(['Area = ' num2str(A) ' m^2']);
Conditional & Logical Operators
= equal
<> not equal
< less than
<= less than or equal
> greater than
>= greater than or equal
AND and
OR or
NOT not
== equal
~= not equal
< less than
<= less than or equal
> greater than
>= greater than or equal
& and
| or
~ not
Conditional Execution
The code below shows a simple set of Block-If statements. In some older versions of BASIC,
there is no Block-If structure and so it would have to be done strictly using GOTO statements.
if Sel = 1 then
print "Selection is 1"
elseif Sel = 2 then
print "Option 2 Here"
else
print "None of the Above
endif
if Sel == 1
disp('Selection is 1');
elseif Sel == 2
disp('Option 2 Here');
else
disp('None of the Above');
end
* * * * * * * BASIC * * * * * * * * * * * * * * MATLAB * * * * * * *
* * * * * * * BASIC * * * * * * * * * * * * * * MATLAB * * * * * * *
Prompting for Input
The following code will prompt for a value from 1 to 10. This also demonstrates the use of
a WHILE in Matlab to wait for a valid input
15 Print "Pick Number between 1 &
10"
Input V
if V<1 or V>10 then 15
V = 0; % Initialize to value<1
% so it does the loop at least once
while V<1 | V>10
V = input('Pick Number between 1 & 10
');
end
Loop Structures
The code below is a simple example of a FOR loop which prints the sum of the squares of the
sines of all angles from 0 to pi/2 in increments of 0.001
SumTh = 0
For Theta=0 To 3.14 Step 0.001
SumTh = SumTh + sin(Theta)^2
Next
Print "Sum is ";SumTh
SumTh = 0;
for Theta=0 : 0.001 : 3.14;
SumTh = SumTh + sin(Theta)^2;
end
disp(['Sum is ' num2str(SumTh)]);
Because of the vector nature of Matlab, it is often possible to replace a FOR loop with an array
process. The code below creates an array called Theta which contains all of the angles. It then
calculates the sines of all of the values in one statement. This script runs about 10 times faster
than the previous code. (This is unimportant for a short script, but for a script which takes several
seconds or longer, this can be significant.) Note the use of the array operator. ^ to square the
array of sines. This is required since the operation sTheta^2 would perform the matrix
multiplication sTheta*sTheta, and would result in an error since sTheta is not a square matrix.
Theta=[0 : 0.01 : 3.14];
sTheta = sin(Theta);
SumTh = sum(sTheta.^2);
disp(['Sum is ' num2str(SumTh)]);
* * * * * * * BASIC * * * * * * * * * * * * * * MATLAB * * * * * * *
There are many matrix functions in Matlab that are not available at all in BASIC.
Some of the more useful ones are shown below.
COMMAND What it Does
x =
zeros(100,2); Makes x a 100 row by 2 column matrix with all zeros
y =
ones(20,20); Makes y a 20 row by 20 column matrix with all ones
z = 0:.1:10; Puts the values of 0,.1,.2,...10 into the variable z
a = z.^2;
The .^2 operator will take each individual element of z and square it,
so a will contain 0,.01,.04,...100
b = a.*z;
The .* operator will take each individual element of a and multiply it by the
same element in z. The equivalent code in BASIC would be
for i=1 to 101
b(i) = a(i)*z(i)
next i
C = rand(3,3);
D = ones(3,3);
E = C*D;
The first command makes C a 3-by-3 matrix of random numbers between 0
and 1. Matrix D will be all ones, and the matrix E will be the matrix product of
these two matrices (so each element of the top row of E will be the sum of the
first row of C.
2. Write a note on applications of MATLAB.
Answer
MATLAB can be used in the following Fields:
*Computations including linear algebra, data analysis, signal processing,
polynomials and interpolation, numerical integration, and numerical solution of
differential equations.
*Graphics, in 2-D and 3-D, including color, lighting, and animation.
MATLAB (matrix laboratory) is a numerical computing environment and fourth-
generation programming language. Developed by Math works MATLAB
allows matrix manipulations, plotting of functions and data, implementation
of algorithms, creation of user interfaces, and interfacing with programs written in
other languages, including C, C++, Java, and Fortran.
Although MATLAB is intended primarily for numerical computing, an optional
toolbox uses the MuPAD symbolic engine, allowing access to symbolic
computing capabilities. An additional package, Simulink, adds graphical multi-
domain simulation and Model-Based Design for dynamic and embedded systems.
3. Compute the following using MATLAB.
a. The radius of a circle is 𝜋1/3
− 1 .Find the diameter,
circumference, and Area.
b. sinh (
𝜋
3
) + sin (
𝜋
3
𝑐
) ∗ cos 600
c. log(i)
d.
1−2𝑖
1+3𝑖
e. Compare
35
35−1
and 1 −
1
35
4. Write a MATLAB program to
a. Bit reversal of a 8 bit binary number.
b. To exchange the upper four bits with lower four bits of a 8 bit
Binary number.
c. Find A and B , A or B , A xor B, given A = 10110010; B =
11010101
5. Find the value of x in the following
a. 𝑥2
− 𝑥 + 4=0
b. 𝑒 𝑥
=16
MATLAB MODULE 2
1. Write MATLAB function sigma = ascsum(x) that takes a one-dimensional array
x of real numbers and computes their sum sigma in the ascending order of
magnitudes.
Hint: You may wish to use MATLAB functions sort, sum, and abs.
ANSWER:
CODE
function[sigma]=ascsum(x)
n=sort(x);
m=sum(n);
sigma=abs(m);
end
output:
2. In this exercise you are to write MATLAB function d = dsc(c) that takes a one-
dimensional array of numbers c and returns an array d consisting of all numbers in
the array c with all Neighboring duplicated numbers being removed. For instance,
if c = [1 2 2 2 3 1], then d = [1 2 3 1].
ANSWER:
CODE
function d=dsc(x)
d=unique(x);
end
output:
3. Write MATLAB function [in, fr] = infr(x) that takes an array x of real numbers
and returns arrays in and fr holding the integral and fractional parts, respectively,
of all numbers in the array x.
ANSWER:
CODE
function[in,fr]=infr(x)
in=fix(x);
fr=x-in;
end
output:
MATLAB MODULE 3
1. Given an array b and a positive integer m create an array d whose
entries are those in the array b each replicated m-times. Write
MATLAB function d = repel(b, m) that generates array d as
described in this problem.
Example: if b=[ 3 8 9 1], m=3 then result d is [3 3 3 8 8 8 9 9 9 1]
ANSWER:
CODE
function d=rep(b,m)
d=[];
for i=1:length(b)
d=[d repmat(b(i),1,m(i))];
end
end
OUTPUT:
2. In this exercise you are to write MATLAB function d = rep(b, m)
that has more functionality than the function repel of Problem 4. It
takes an array of numbers b and the array m of positive integers
and returns an array d whose each entry is taken from the array b
and is duplicated according to the corresponding value in the array
m. For instance, if b = [ 1 2] and m = [2 3], then d = [1 1 2 2 2].
ANSWER
CODE
function d=repel(b,m)
x=repmat(b,m,1);
d=reshape(x,1,size(b,2)*m);
end
OUTPUT:
MATLAB MODULE 4
1. Print Prime Numbers from M to N.
ANSWER
CODE
function [Prime] = PrimeNum(N,M)
if (N>M || N <0 || M < 0)
error('ERROR: Invalid Input, you entered biggere number first or you
entered negative number as you interval.');
end
Prime = [];
for j=0:(M-N)
if all(mod((N+j),2:((N+j)/2))), Prime = [Prime;N+j];
end
end
OUTPUT:
2. Fibonacci Series up to N numbers.
Definition: The first two numbers in the Fibonacci sequence
are 0 and 1 (alternatively, 1 and 1), and each subsequent number
is the sum of the previous two
Ex: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 ….
ANSWER
CODE
function [f]=fib
n = input('n (number of Fibonacci numbers to compute): ');
f(1) = 1; f(2) = 1;
for i = 3:n
f(i) = f(i-1) + f(i-2);
end
end
output:
3. Factorial of a given Number.
Ex: 5! = 5 x 4 x 3 x 2 x 1 = 120
ANSWER
CODE
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:
MATLAB MODULE 5
1. Write a function to perform following set operations
A. Union of A and B
B. Intersection of A and B
C. Complement of A and B
(Assume A= {1, 2, 3, 4, 5, 6}, B= {2, 4, 6})
ANSWER:
CODE
%Enter Data
a=input('Enter First Sets');
b=input('Enter Second Sets');
u=[1 2 3 4 5 6 7 8 9];
%To Perform Operations
uni=union(a,b);
intersection=intersect(a,b);
acomplement=setdiff(u,a);
bcomplement=setdiff(u,b);
c=setdiff(u,uni);
%Display Output
display('Union Of Two Sets');
display(uni);
display('Intersection Of Two Sets');
display(intersection);
display('Complement Of First Sets');
display(acomplement);
display('Complement Of Second Sets');
display(bcomplement);
display('Complement Of A and B');
display(c);
output:
2. Write any one of the program to do following things using file
concept.
A. Create or Open a file
B. Read data from the file and write data to another file
C. Append some text to already existed file
D. Close the file
ANSWER:
CODE
function fileoperation
filepointer=fopen('file.txt','r');
i=0;
while ~feof(filepointer)
x=fscanf(filepointer,'%c',1);
if ~isempty(x)
i=i+1;
a(i)=x;
end
end
disp(a);
fp1=fopen('writemode.txt','w');
fprintf(fp1,'%s',a);
fp2=fopen('appendmode.txt','a');
fprintf(fp2,'%s',a);
fclose(filepointer);
fclose(fp1);
fclose(fp2);
end
output:
MATLAB MODULE 6
1. Write a program to solve given equation
(𝑥) = cos(𝑥) -
1
2
Using following methods.
(Assume appropriate initial guesses and tolerance is up to 6 digits after
decimal point.)
A. BISECTION
ANSWER
CODE
% program for bisection
a=input('Enter Function:','s');
f=inline(a);
xl=input('Enter lower guess:') ;
xu=input('Enter upper guess:');
tol=input('Enter tolerance(recommended 0.001):');
if f(xu)*f(xl)<0
else
fprintf('Wrong Guess!Enter new guessn');
xl=input('Enter lower guess:n') ;
xu=input('Enter upper guess:n');
end
for i=2:1000
xr=(xu+xl)/2;
if f(xu)*f(xr)<0
xl=xr;
else
xu=xr;
end
if f(xl)*f(xr)<0
xu=xr;
else
xl=xr;
end
xnew(1)=0;
xnew(i)=xr;
if abs((xnew(i)-xnew(i-1))/xnew(i))<tol,break,end
end
str = ['Root: ', num2str(xr), '']
Output:
B. INTERPOLATION
ANSWER
CODE
% Inter polation methode
f=input('f(x)=','s');
tol=input('error tolerance =1e-5, new tolerance=');
if length(tol)==0,tol=1e-5;end
x1=input(' First guess=');
x=x1;
f1=eval(f);
x2=input(' Second guess=');
x=x2;
f2=eval(f);xsave=x2;
if f1*f2<0
disp(' False Position ')
for i=2:21
x=x2-f2*(x2-x1)/(f2-f1);
fx=eval(f);
if fx*f1>0
x1=x;f1=fx;
else
x2=x;f2=fx;
end
fprintf('i = %g, x = %g, fx = %gn',i,x,fx)
if abs(xsave-x)<tol, break,end
xsave=x;
end
else
disp('Need to bracket the roots')
end
C. NEWTON-RAPHSON
ANSWER
CODE
% Newton raphson method with numerical derivative
f=input('f(x)=','s');
tol=input('error tolerance =1e-5, new tolerance=');
if length(tol)==0,tol=1e-5;end
x1=input(' First guess=');
x=x1; fx=eval(f);
for i=1:100
if abs(fx)<tol, break,end
x=x+.01;
ff=eval(f);
fdx=(ff-fx)/.01;
x1=x1-fx/fdx;
x=x1;
fx=eval(f);
fprintf('i = %g, x = %g, fx = %gn',i,x,fx)
end
Output
D.SECANT
ANSWER
CODE
% Secant method
f=input('f(x)=','s');
tol=input('error tolerance =1e-5, new tolerance=');
if length(tol)==0,tol=1e-5;end
x1=input(' First guess=');
x=x1;
f1=eval(f);
x2=input(' Second guess=');
x=x2;
f2=eval(f);
xsave=x2;
disp(' Secant method')
for i=2:21
x=x2-f2*(x2-x1)/(f2-f1);
fx=eval(f);
x1=x2;f1=f2;ex=abs(x2-x);
x2=x;f2=fx;
fprintf('i = %g, x = %g, fx = %gn',i,x,fx)
if ex<tol, break,end
end
OUTPUT
MATLAB MODULE 7
ANSWER:
CODE
%%sine wave
t=0:pi/1000:(2*pi);
y= sin(t);
z=plot(t,y,'r'); %for plotting graph
grid on; %for showing grid lines
xlim([0 2*pi]); %to set x limit
ylim([-1 1]); %to set y limit
set(gca,'XTick',[0 pi (2*pi)]); %to select tha values
which x axis should take
set(gca,'XTickLabel',{'0','1','2'}); %to select the values to
be visible on x axis
set(gca,'YTick',-1:.5:1); %to select tha values
which y axis should take
set(gca,'YTickLabel',{'-1','-0.5','0','0.5','1'}); %to select the values to
be visible on y axis
title('One Sine wave from 0 to 2pi','FontSize',12,'FontWeight','bold','Color','white');
%for setting title
xlabel('X values(interms of pi)','FontSize',12,'FontWeight','bold','Color','cyan');
%for setting label for x axis
ylabel('Sin(t)','FontSize',12,'FontWeight','bold','Color','green'); %for setting label
for y axis
set(gca,'Color','black','XColor','cyan','YColor','green'); %for setting axis
colours
set(gcf,'Color',[.3 .3 .3]); %for setting
background
set(z,'Color','red','LineWidth',2);
OUTPUT:
2. Write a script for generating
a. 1000 Poisson distributed random numbers with parameter λ= 5.
b. 1000 normal distributed numbers with mean 10, variance 25
c. Plot the PDFs for the above two in the same plot using sub plot, name them appropriately.
ANSWER:
CODE
clc
clear all;
close all;
lambda = 5;
random_sample1 = poissrnd(lambda,1,100)
mu=10;
sigma=25;
random_sample2 = normrnd(mu,sigma)
X=0:10:1000
Y = normpdf(X,mu,sigma)
subplot(2,1,1)
stem(Y);
title('normal distribution function');
xlabel('normal pdf');
ylabel('probability')
X=0:10:1000
Y1 = poisspdf(X,lambda)
subplot(2,1,2)
stem(Y1);
title('possion distribution function');
xlabel('poission pdf');
ylabel('probability')
figure
subplot(2,1,1)
plot(Y);
title('normal distribution function');
xlabel('normal pdf');
ylabel('probability')
subplot(2,1,2)
plot(Y1);
title('possion distribution function');
xlabel('poission pdf');
ylabel('probability')
OUTPUT:
3. Write a script to load the data file randomData.mat (which contains variables x
and y) and fit first, second, third, fourth, and fifth degree polynomials to it. Plot the
data as blue dots on a figure, and plot all five polynomial fits using lines of
different colors on the same axes. Label the figure appropriately.
Hint: To get good fits, you’ll have to use the centering and scaling version of
polyfit (the one that returns three arguments, see help) and its counterpart in
polyval (the one that accepts the centering and scaling parameters).
ANSWER:
CODE
clc;
clear all;
load('randomData.mat')
polyy=polyfit(x,y,1);
figure
ws = warning('off','all');
f=polyval(polyy,x);
plot(f,'o');
hold on;
ws = warning('off','all');
polyy=polyfit(x,y,2);
f=polyval(polyy,x);
plot(f,'.g');
ws = warning('off','all');
polyy=polyfit(x,y,3);
f=polyval(polyy,x);
plot(f,'-m');
ws = warning('off','all');
polyy=polyfit(x,y,4);
f=polyval(polyy,x);
plot(f,'-+r');
ws = warning('off','all');
polyy=polyfit(x,y,5);
f=polyval(polyy,x);
plot(f,'p');
legend('order 1','order 2','order 3','order 4','order 5');
title('POLYNOMIALS','fontweight','bold','color','m');
warning(ws); % Turn it back on.
OUTPUT:
4. Write a script to open a new figure and plot five circles, all centered at the
origin and with increasing radii. Set the line width for each circle to something
thick (at least 2 points), and use the colors from a 5-color jet colormap (jet).
ANSWER:
CODE
clc; %Clear screen
clear all; %Clear all varibles
close all; %Close all unwanted windows
t=0:0.001:10; %Initializing time samples
%Transfer chara of sine and cosi.e sin Vs Cos will give circle
%Here we are plotting group of sine Vscos with differnt amplitudes
s1=3*sin(2*pi*t); %Sine wave with amplitude=3unit
c1=3*cos(2*pi*t); %Cosine wave with amplitude=3unit
s2=2*sin(2*pi*t);
c2=2*cos(2*pi*t);
s3=1*sin(2*pi*t);
c3=1*cos(2*pi*t);
s4=4*sin(2*pi*t);
c4=4*cos(2*pi*t);
s5=5*sin(2*pi*t);
c5=5*cos(2*pi*t);
hold on;
plot(s1,c1,'r','LineWidth',4)
plot(s2,c2,'k','LineWidth',2)
plot(s3,c3,'g','LineWidth',6)
plot(s4,c4,'y','LineWidth',8)
plot(s5,c5,'m','LineWidth',10)
legend('r= 3-width=4', 'r= 2-width=2', 'r= 1-width=6', 'r= 4-width=8', 'r= 5-
width=10')
%grid on;%Enable grid lines
axis equal;%Equal width of X and Y axis
OUTPUT:
5. Record your name pronounced by yourself. Try to display the signal(name) in a
plot vs Time, using matlab.
ANSWER:
CODE
% Record your voice for 5 seconds.
recObj = audiorecorder;
disp('Start speaking.')
recordblocking(recObj, 5);
disp('End of Recording.');
% Play back the recording.
play(recObj);
% Store data in double-precision array.
myRecording = getaudiodata(recObj);
% Plot the samples.
plot(myRecording);
OUTPUT:
MATLAB MODULE 8
1. Take your picture and
 Show R, G, B channels along with RGB Image in same
figure using sub figure.
 Convert into HSV( Hue, saturation and value) and show the
H,S,V channels along with HSV image
Answer
Code
Im1 = imread('vik4.jpg');
figure;
subplot(2,2,1);
imshow(Im1);
title('Original color Image', 'FontSize',10);
%Im2 = imadjust(Im1, [0.2, 0.8], [0, 1]);
% Display the original color image.
% Extract the individual red, green, and blue color channels.
redChannel = Im1(:,:,1);
greenChannel = Im1(:,:,2);
blueChannel = Im1(:,:,3);
subplot(2,2,2),imshow(redChannel);
title('Red Channel','Color','m', 'FontSize', 10);
subplot(2,2,3),imshow(greenChannel);
title('Green Channel','Color','r', 'FontSize', 10);
subplot(2,2,4),imshow(blueChannel);
title('Blue Channel','Color','g', 'FontSize', 10);
img=imread('vik4.jpg');
[H S V]=rgb2hsv(img);
figure;
subplot(2,2,1);
imshow(img);
h = img(:, :, 1); % Hue image.
s = img(:, :, 2); % Saturation image.
v = img(:, :, 3); % Value (intensity) image.
subplot(2,2,2),imshow(H);
title('HUE IMAGE','Color','m', 'FontSize', 10);
subplot(2,2,3),imshow(S);
title('SATURATION','Color','r' ,'FontSize', 10);
subplot(2,2,4),imshow(V);
title('VALUE', 'Color','g','FontSize', 10);
Output:
2. Using EZ plot & anonymous functions plot the following:
 Y=Sqrt(X)
 Y= X^2
 Y=e^(-XY)
Answer
Code
clc;
clear all;
close all;
subplot(3,1,1);
ezplot(@(x) sqrt(x));
grid on;
subplot(3,1,2);
ws = warning('off','all');
ezplot(@(x) x^2);
grid on;
subplot(3,1,3);
syms x y;
ws = warning('off','all');
ezplot(y==exp(x*y));
grid on;
Output:
MATLAB MODULE 9
1. Write a MATLAB program that converts a decimal number to Roman
number and vice versa.
Answer:
DECIMAL TO ROMAN
function ans = decimal2roman(z)
d = [ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
c = {'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'};
[];
for ii = 1:numel(d)
if z >= d(ii)
ans = [ans,repmat(c{ii},1,fix(z/d(ii)))];
z = rem(z,d(ii));
end
end
output:
Answer:
ROMAN TO DECIMAL
clc
x=input('Enter the roman numeral: ','s');
l=length(x);for(i=1:l)
if(x(i)=='I')
a(i)=1;
elseif(x(i)=='V')
a(i)=5;
elseif(x(i)=='X')
a(i)=10;
elseif(x(i)=='L')
a(i)=50;
elseif(x(i)=='C')
a(i)=100;
elseif(x(i)=='D')
a(i)=500;
elseif(x(i)=='M')
a(i)=1000;
else disp('Not a valid entry');
end
end
d=a(l);
for(i=l:-1:2)
if(a(i)>a(i-1))
d=d-a(i-1);
elseif(a(i)==a(i-1) || a(i)<a(i-1))
d=d+a(i-1);
end
end
disp('The decimal equivalent is: ');
disp(d);
OUTPUT:
2.
ANSWER:
CODE
clc
x = {'joe' 'smith' 30000 ; 'sarah' 'brown' 150000;'pat' 'jackson' 120000 };
disp(x);
yes=input('want to change data :n if yes press 1 else press 2 ');
if (yes==1)
disp('enter the details n ');
row=input('row : ');
coloumn=input('column : ');
k=input('enter data : ');
x{row,coloumn}=k;
disp(x);
else
end
Output:
3. A supermarket conveyor belt holds an array of groceries. Construct a
structure with superMarket, which contains costumer id, total bill a
structure with names of the item, and corresponding cost of the items,
quantity of the items. Initialize the structure for 100 costumers. Write a
program to prepare bill by taking the inputs from the user.
ANSWER:
CODE
function supermarket
clc;
fprintf('******************************************n');
fprintf('welcome to vikas supermarketn');
fprintf('******************************************n');
item1='tshirt_1';
price1=500;
item2='jeans_2';
price2=2000;
item3='shoes_3';
price3=600;
item =struct(item1,price1,item2,price2,item3,price3);
for i=1:100
f1='id';
v1=input('please enter the customer number : ','s');
f2='item';
v2=item;
f3='bill';
v3=0;
disp(item);
n=input('enter item number:');
q=input('enter quantity:');
if n==1
v3=v3+(q*price1);
elseif n==2
v3=v3+(q*price2);
elseif n==3
v3=v3+(q*price3);
end
fprintf('Do you wish to continuenpress-1 for yesnpress-2 for non');
choice= input('enter choice :');
if(choice==2)
break;
end
end
disp('bill : ');
s=struct(f1,v1,f2,v2,f3,v3);
disp(s);
fprintf('******************************************n');
fprintf('ttthankyou visit againn');
fprintf('******************************************n');
end
output:
MATLAB MODULE 10
Generate videos in which a skeleton of a person doing the following
Gestures.
 Tilting his head to right and left
 Tilting his hand to right and left
 Walking
The skeleton looks something like this.
Answer:
Tilting his hand to right and left
clc;
clear all;
close all;
figure
x1=[2,2];
y1=[4.5,4];
x2=[2,1.5];
y2=[4,3.5];
x3=[2,2.5];
y3=[4,3.5];
x4=[1.5,1.5];
y4=[3.5,3];
x5=[2.5,2.5];
y5=[3.5,3];
x6=[2,2.5];
y6=[3,1.5];
x7=[2,1.5];
y7=[3,1.5];
x8=[1.5,1.5];
y8=[1.5,1];
x9=[2.5,2.5];
y9=[1.5,1];
x10=[2,2];
y10=[4,3];
plot(2,4.5,'O',2.5,3.5,'O',1.5,3.5,'O',2.5,1.5,'O',1.5,1.5,'O',x1,y1,x2,y2,x3,y
3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,x9,y9,x10,y10,'color','k','linewidth',4);
axis([0 6 0 6]);
a(1)=getframe;
x4=[1.5,1.8];
y4=[3.5,3];
x5=[2.5,2.8];
y5=[3.5,3];
plot(2,4.5,'O',2.5,3.5,'O',1.5,3.5,'O',2.5,1.5,'O',1.5,1.5,'O',x1,y1,x2,y2,x3,y
3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,x9,y9,x10,y10,'color','k','linewidth',4);
axis([0 6 0 6]);
a(2)=getframe;
x4=[1.5,1.2];
y4=[3.5,3];
x5=[2.5,2.2];
y5=[3.5,3];
plot(2,4.5,'O',2.5,3.5,'O',1.5,3.5,'O',2.5,1.5,'O',1.5,1.5,'O',x1,y1,x2,y2,x3,y
3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,x9,y9,x10,y10,'color','k','linewidth',4);
axis([0 6 0 6]);
a(3)=getframe;
for i=0:60
movie(a,2,2);
%press cntrl c to end the video
end
Answer:
Tilting his head to right and left
clc;
clear all;
close all;
figure;
x1=[2,2];
y1=[4.5,4];
x2=[2,1.5];
y2=[4,3.5];
x3=[2,2.5];
y3=[4,3.5];
x4=[1.5,1.5];
y4=[3.5,3];
x5=[2.5,2.5];
y5=[3.5,3];
x6=[2,2.5];
y6=[3,1.5];
x7=[2,1.5];
y7=[3,1.5];
x8=[1.5,1.5];
y8=[1.5,1];
x9=[2.5,2.5];
y9=[1.5,1];
x10=[2,2];
y10=[4,3];
plot(2,4.5,'O',2.5,3.5,'O',1.5,3.5,'O',2.5,1.5,'O',1.5,1.5,'O',x1,y1,x2,y2,x3,y
3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,x9,y9,x10,y10,'color','k','linewidth',4);
axis([0 6 0 6]);%to set up axes limits
a(1)=getframe;
x1=[2.5,2];
y1=[4.5,4];
plot(2.5,4.5,'O',2.5,3.5,'O',1.5,3.5,'O',2.5,1.5,'O',1.5,1.5,'O',x1,y1,x2,y2,x3,
y3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,x9,y9,x10,y10,'color','k','linewidth',4);
axis([0 6 0 6]);
a(2)=getframe;
x1=[1.5,2];
y1=[4.5,4];
plot(1.5,4.5,'O',2.5,3.5,'O',1.5,3.5,'O',2.5,1.5,'O',1.5,1.5,'O',x1,y1,x2,y2,x3,
y3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,x9,y9,x10,y10,'color','k','linewidth',4);
axis([0 6 0 6]);
a(3)=getframe;
for i=1:60
movie(a,2,3);
%press ctrl c to end the video
end
axis equal
Answer:
Walking
clc;
clear all;
close all;
figure
x1=[2,2];
y1=[4.5,4];
x2=[2,1.5];
y2=[4,3.5];
x3=[2,2.5];
y3=[4,3.5];
x4=[1.5,1.5];
y4=[3.5,3];
x5=[2.5,2.5];
y5=[3.5,3];
x6=[2,2.5];
y6=[3,1.5];
x7=[2,1.5];
y7=[3,1.5];
x8=[1.5,1.5];
y8=[1.5,1];
x9=[2.5,2.5];
y9=[1.5,1];
x10=[2,2];
y10=[4,3];
plot(2,4.5,'O',2.5,3.5,'O',1.5,3.5,'O',2.5,1.5,'O',1.5,1.5,'O',x1,y1,x2,y2,x3,y
3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,x9,y9,x10,y10,'color','k','linewidth',4);
axis([0 6 0 6]);
a(1)=getframe;
x4=[1.5,1.5];
y4=[3.5,3];
x5=[2.5,3];
y5=[3.5,4];
x8=[1.5,1];
y8=[1.5,1];
x9=[2.5,2.5];
y9=[1.5,1];
plot(2,4.5,'O',2.5,3.5,'O',1.5,3.5,'O',2.5,1.5,'O',1.5,1.5,'O',x1,y1,x2,y2,x3,y
3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,x9,y9,x10,y10,'color','k','linewidth',4);
axis([0 6 0 6]);
a(2)=getframe;
x4=[1.5,1];y4=[3.5,3];x5=[2.5,2.5];y5=[3.5,3];
x8=[1.5,1.5];y8=[1.5,1];x9=[2.5,3];y9=[1.5,1];
plot(2,4.5,'O',2.5,3.5,'O',1.5,3.5,'O',2.5,1.5,'O',1.5,1.5,'O',x1,y1,x2,y2,x3,y
3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,x9,y9,x10,y10,'color','k','linewidth',4);
axis ([0 6 0 6]);
a(3)=getframe;
for i=0:60
movie(a,2,5);
%press ctrl c to end the video
end
For the last module i.e. Module 10, by running the program we get the
output as video. So not attached in this document.

Mais conteúdo relacionado

Mais procurados

Mcs lecture19.methods ofproof(1)
Mcs lecture19.methods ofproof(1)Mcs lecture19.methods ofproof(1)
Mcs lecture19.methods ofproof(1)kevinwu1994
 
Graphing Absolute Value
Graphing Absolute ValueGraphing Absolute Value
Graphing Absolute Valueswartzje
 
NAND and NOR connectives
NAND and NOR connectivesNAND and NOR connectives
NAND and NOR connectivesLakshmi R
 
Discrete Mathematics Lecture
Discrete Mathematics LectureDiscrete Mathematics Lecture
Discrete Mathematics LectureGenie Rose Santos
 
Quadratic Programming : KKT conditions with inequality constraints
Quadratic Programming : KKT conditions with inequality constraintsQuadratic Programming : KKT conditions with inequality constraints
Quadratic Programming : KKT conditions with inequality constraintsMrinmoy Majumder
 
Homogeneous Linear Differential Equations
 Homogeneous Linear Differential Equations Homogeneous Linear Differential Equations
Homogeneous Linear Differential EquationsAMINULISLAM439
 
Linear transformation and application
Linear transformation and applicationLinear transformation and application
Linear transformation and applicationshreyansp
 
orthogonal matrix and its properties
orthogonal matrix and its propertiesorthogonal matrix and its properties
orthogonal matrix and its propertiesGOVINDKUMAR689610
 
Integration in the complex plane
Integration in the complex planeIntegration in the complex plane
Integration in the complex planeAmit Amola
 
Why We Need To Study Calculus
Why We Need To Study CalculusWhy We Need To Study Calculus
Why We Need To Study CalculusAfaq Siddiqui
 
Definite Integral and Properties of Definite Integral
Definite Integral and Properties of Definite IntegralDefinite Integral and Properties of Definite Integral
Definite Integral and Properties of Definite IntegralShaifulIslam56
 
CMSC 56 | Lecture 16: Equivalence of Relations & Partial Ordering
CMSC 56 | Lecture 16: Equivalence of Relations & Partial OrderingCMSC 56 | Lecture 16: Equivalence of Relations & Partial Ordering
CMSC 56 | Lecture 16: Equivalence of Relations & Partial Orderingallyn joy calcaben
 
Discrete Math Chapter 1 :The Foundations: Logic and Proofs
Discrete Math Chapter 1 :The Foundations: Logic and ProofsDiscrete Math Chapter 1 :The Foundations: Logic and Proofs
Discrete Math Chapter 1 :The Foundations: Logic and ProofsAmr Rashed
 
Discrete mathematics Ch2 Propositional Logic_Dr.khaled.Bakro د. خالد بكرو
Discrete mathematics Ch2 Propositional Logic_Dr.khaled.Bakro د. خالد بكروDiscrete mathematics Ch2 Propositional Logic_Dr.khaled.Bakro د. خالد بكرو
Discrete mathematics Ch2 Propositional Logic_Dr.khaled.Bakro د. خالد بكروDr. Khaled Bakro
 

Mais procurados (20)

Double Integrals
Double IntegralsDouble Integrals
Double Integrals
 
Mcs lecture19.methods ofproof(1)
Mcs lecture19.methods ofproof(1)Mcs lecture19.methods ofproof(1)
Mcs lecture19.methods ofproof(1)
 
Graphing Absolute Value
Graphing Absolute ValueGraphing Absolute Value
Graphing Absolute Value
 
NAND and NOR connectives
NAND and NOR connectivesNAND and NOR connectives
NAND and NOR connectives
 
Discrete Mathematics Lecture
Discrete Mathematics LectureDiscrete Mathematics Lecture
Discrete Mathematics Lecture
 
Quadratic Programming : KKT conditions with inequality constraints
Quadratic Programming : KKT conditions with inequality constraintsQuadratic Programming : KKT conditions with inequality constraints
Quadratic Programming : KKT conditions with inequality constraints
 
Homogeneous Linear Differential Equations
 Homogeneous Linear Differential Equations Homogeneous Linear Differential Equations
Homogeneous Linear Differential Equations
 
LinearAlgebra.ppt
LinearAlgebra.pptLinearAlgebra.ppt
LinearAlgebra.ppt
 
Divrgence theorem with example
Divrgence theorem with exampleDivrgence theorem with example
Divrgence theorem with example
 
Linear transformation and application
Linear transformation and applicationLinear transformation and application
Linear transformation and application
 
Chapter 11 - Differentiation
Chapter 11 - DifferentiationChapter 11 - Differentiation
Chapter 11 - Differentiation
 
orthogonal matrix and its properties
orthogonal matrix and its propertiesorthogonal matrix and its properties
orthogonal matrix and its properties
 
Integration in the complex plane
Integration in the complex planeIntegration in the complex plane
Integration in the complex plane
 
Why We Need To Study Calculus
Why We Need To Study CalculusWhy We Need To Study Calculus
Why We Need To Study Calculus
 
Definite Integral and Properties of Definite Integral
Definite Integral and Properties of Definite IntegralDefinite Integral and Properties of Definite Integral
Definite Integral and Properties of Definite Integral
 
CMSC 56 | Lecture 16: Equivalence of Relations & Partial Ordering
CMSC 56 | Lecture 16: Equivalence of Relations & Partial OrderingCMSC 56 | Lecture 16: Equivalence of Relations & Partial Ordering
CMSC 56 | Lecture 16: Equivalence of Relations & Partial Ordering
 
Discrete Math Chapter 1 :The Foundations: Logic and Proofs
Discrete Math Chapter 1 :The Foundations: Logic and ProofsDiscrete Math Chapter 1 :The Foundations: Logic and Proofs
Discrete Math Chapter 1 :The Foundations: Logic and Proofs
 
Discrete mathematics Ch2 Propositional Logic_Dr.khaled.Bakro د. خالد بكرو
Discrete mathematics Ch2 Propositional Logic_Dr.khaled.Bakro د. خالد بكروDiscrete mathematics Ch2 Propositional Logic_Dr.khaled.Bakro د. خالد بكرو
Discrete mathematics Ch2 Propositional Logic_Dr.khaled.Bakro د. خالد بكرو
 
Euler’s formula
Euler’s formulaEuler’s formula
Euler’s formula
 
2d transformations
2d transformations2d transformations
2d transformations
 

Destaque

Matlab practical and lab session
Matlab practical and lab sessionMatlab practical and lab session
Matlab practical and lab sessionDr. Krishna Mohbey
 
Advanced MATLAB Tutorial for Engineers & Scientists
Advanced MATLAB Tutorial for Engineers & ScientistsAdvanced MATLAB Tutorial for Engineers & Scientists
Advanced MATLAB Tutorial for Engineers & ScientistsRay Phan
 
Taking R to the Limit (High Performance Computing in R), Part 1 -- Paralleliz...
Taking R to the Limit (High Performance Computing in R), Part 1 -- Paralleliz...Taking R to the Limit (High Performance Computing in R), Part 1 -- Paralleliz...
Taking R to the Limit (High Performance Computing in R), Part 1 -- Paralleliz...Ryan Rosario
 
MATLAB CODE OF FIR Filter Designing LPF HPF BPF BSF
MATLAB CODE OF FIR Filter Designing LPF HPF BPF BSFMATLAB CODE OF FIR Filter Designing LPF HPF BPF BSF
MATLAB CODE OF FIR Filter Designing LPF HPF BPF BSFBharti Airtel Ltd.
 
Chapter 2 signals and spectra,
Chapter 2   signals and spectra,Chapter 2   signals and spectra,
Chapter 2 signals and spectra,nahrain university
 
Introduction to Matlab
Introduction to MatlabIntroduction to Matlab
Introduction to MatlabAmr Rashed
 

Destaque (8)

Matlab practical and lab session
Matlab practical and lab sessionMatlab practical and lab session
Matlab practical and lab session
 
Advanced MATLAB Tutorial for Engineers & Scientists
Advanced MATLAB Tutorial for Engineers & ScientistsAdvanced MATLAB Tutorial for Engineers & Scientists
Advanced MATLAB Tutorial for Engineers & Scientists
 
Taking R to the Limit (High Performance Computing in R), Part 1 -- Paralleliz...
Taking R to the Limit (High Performance Computing in R), Part 1 -- Paralleliz...Taking R to the Limit (High Performance Computing in R), Part 1 -- Paralleliz...
Taking R to the Limit (High Performance Computing in R), Part 1 -- Paralleliz...
 
Fibonacci using matlab
Fibonacci using matlabFibonacci using matlab
Fibonacci using matlab
 
MATLAB CODE OF FIR Filter Designing LPF HPF BPF BSF
MATLAB CODE OF FIR Filter Designing LPF HPF BPF BSFMATLAB CODE OF FIR Filter Designing LPF HPF BPF BSF
MATLAB CODE OF FIR Filter Designing LPF HPF BPF BSF
 
Chapter 2 signals and spectra,
Chapter 2   signals and spectra,Chapter 2   signals and spectra,
Chapter 2 signals and spectra,
 
Introduction to Matlab
Introduction to MatlabIntroduction to Matlab
Introduction to Matlab
 
Solved problems
Solved problemsSolved problems
Solved problems
 

Semelhante a B61301007 matlab documentation

Dsp manual completed2
Dsp manual completed2Dsp manual completed2
Dsp manual completed2bilawalali74
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013amanabr
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Kurmendra Singh
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlabBilawalBaloch1
 
MATLAB-Introd.ppt
MATLAB-Introd.pptMATLAB-Introd.ppt
MATLAB-Introd.pptkebeAman
 
Matlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMatlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMohd Esa
 
More instructions for the lab write-up1) You are not obli.docx
More instructions for the lab write-up1) You are not obli.docxMore instructions for the lab write-up1) You are not obli.docx
More instructions for the lab write-up1) You are not obli.docxgilpinleeanna
 
MATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docx
MATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docxMATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docx
MATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docxandreecapon
 
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docxSAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docxagnesdcarey33086
 
MATLAB Questions and Answers.pdf
MATLAB Questions and Answers.pdfMATLAB Questions and Answers.pdf
MATLAB Questions and Answers.pdfahmed8651
 
A complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projectsA complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projectsMukesh Kumar
 
CSCI 2033 Elementary Computational Linear Algebra(Spring 20.docx
CSCI 2033 Elementary Computational Linear Algebra(Spring 20.docxCSCI 2033 Elementary Computational Linear Algebra(Spring 20.docx
CSCI 2033 Elementary Computational Linear Algebra(Spring 20.docxmydrynan
 

Semelhante a B61301007 matlab documentation (20)

Tutorial2
Tutorial2Tutorial2
Tutorial2
 
Matlab1
Matlab1Matlab1
Matlab1
 
Dsp manual completed2
Dsp manual completed2Dsp manual completed2
Dsp manual completed2
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
MATLAB-Introd.ppt
MATLAB-Introd.pptMATLAB-Introd.ppt
MATLAB-Introd.ppt
 
Matlab-free course by Mohd Esa
Matlab-free course by Mohd EsaMatlab-free course by Mohd Esa
Matlab-free course by Mohd Esa
 
Mmc manual
Mmc manualMmc manual
Mmc manual
 
Matlab booklet
Matlab bookletMatlab booklet
Matlab booklet
 
Matlab ppt
Matlab pptMatlab ppt
Matlab ppt
 
More instructions for the lab write-up1) You are not obli.docx
More instructions for the lab write-up1) You are not obli.docxMore instructions for the lab write-up1) You are not obli.docx
More instructions for the lab write-up1) You are not obli.docx
 
MATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docx
MATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docxMATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docx
MATLAB sessions Laboratory 2MAT 275 Laboratory 2Matrix .docx
 
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docxSAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
 
Es272 ch1
Es272 ch1Es272 ch1
Es272 ch1
 
Programming with matlab session 1
Programming with matlab session 1Programming with matlab session 1
Programming with matlab session 1
 
MATLAB Questions and Answers.pdf
MATLAB Questions and Answers.pdfMATLAB Questions and Answers.pdf
MATLAB Questions and Answers.pdf
 
A complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projectsA complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projects
 
CSCI 2033 Elementary Computational Linear Algebra(Spring 20.docx
CSCI 2033 Elementary Computational Linear Algebra(Spring 20.docxCSCI 2033 Elementary Computational Linear Algebra(Spring 20.docx
CSCI 2033 Elementary Computational Linear Algebra(Spring 20.docx
 
An Introduction to MATLAB with Worked Examples
An Introduction to MATLAB with Worked ExamplesAn Introduction to MATLAB with Worked Examples
An Introduction to MATLAB with Worked Examples
 

B61301007 matlab documentation

  • 1. Documentation On MATLAB Report submitted in partial fulfillment of the requirement of the award of the Degree of Master of Technology in Computational Engineering Submitted on (11-12-2013) Submitted By MANCHIREDDY VIKAS VARDHAN REDDY B61301007 Department of Computational Engineering (2013-2015) Rajiv Gandhi University of Knowledge Technologies Basar, Adilabad Dist – 504107
  • 2. Table of Contents MATLAB MODULE 1:.................................................................................................................. 3 MATLAB MODULE 2:................................................................................................................ 10 MATLAB MODULE 3:................................................................................................................ 14 MATLAB MODULE 4:................................................................................................................ 16 MATLAB MODULE 5:................................................................................................................ 19 MATLAB MODULE 6:................................................................................................................ 23 MATLAB MODULE 7:................................................................................................................ 30 MATLAB MODULE 8:................................................................................................................ 40 MATLAB MODULE 9:................................................................................................................ 44 MATLAB MODULE 10:............................................................................................................. 50
  • 3. MATLAB MODULE 1 1. Compare MATLAB with other high level language Answer Comparison: MATLAB Scripts & BASIC There are many similarities between the language used for Matlab scripts and more the more familiar BASIC programming language (and other higher level languages such as FORTRAN PASCAL or C. This document may help when becoming familiar with Matlab scripts. * * * * * * * BASIC * * * * * * * * * * * * * * MATLAB * * * * * * * Variable Assignments In Matlab, all variable assignments are case sensitive, so the variable Test1 is different from the variable test1. In Matlab, if an assignment is terminated with a semicolon, Matlab does not print out the result. If it is not terminated by a semicolon, it will print out the results of the assignment (In the Matlab code below, there is no semicolon after the assignment for A. This causes Matlab to print the answer). R = 2 P = 3.14 A = P*R^2 PRINT A 12.56 R = 2; P = 3.14; A = P*R^2 ans = 12.560 Printing PRINT "Area = ";A;" m^2" disp(['Area = ' num2str(A) ' m^2']); Conditional & Logical Operators = equal <> not equal < less than <= less than or equal > greater than >= greater than or equal AND and OR or NOT not == equal ~= not equal < less than <= less than or equal > greater than >= greater than or equal & and | or ~ not Conditional Execution The code below shows a simple set of Block-If statements. In some older versions of BASIC, there is no Block-If structure and so it would have to be done strictly using GOTO statements.
  • 4. if Sel = 1 then print "Selection is 1" elseif Sel = 2 then print "Option 2 Here" else print "None of the Above endif if Sel == 1 disp('Selection is 1'); elseif Sel == 2 disp('Option 2 Here'); else disp('None of the Above'); end * * * * * * * BASIC * * * * * * * * * * * * * * MATLAB * * * * * * * * * * * * * * BASIC * * * * * * * * * * * * * * MATLAB * * * * * * * Prompting for Input The following code will prompt for a value from 1 to 10. This also demonstrates the use of a WHILE in Matlab to wait for a valid input 15 Print "Pick Number between 1 & 10" Input V if V<1 or V>10 then 15 V = 0; % Initialize to value<1 % so it does the loop at least once while V<1 | V>10 V = input('Pick Number between 1 & 10 '); end Loop Structures The code below is a simple example of a FOR loop which prints the sum of the squares of the sines of all angles from 0 to pi/2 in increments of 0.001 SumTh = 0 For Theta=0 To 3.14 Step 0.001 SumTh = SumTh + sin(Theta)^2 Next Print "Sum is ";SumTh SumTh = 0; for Theta=0 : 0.001 : 3.14; SumTh = SumTh + sin(Theta)^2; end disp(['Sum is ' num2str(SumTh)]); Because of the vector nature of Matlab, it is often possible to replace a FOR loop with an array process. The code below creates an array called Theta which contains all of the angles. It then calculates the sines of all of the values in one statement. This script runs about 10 times faster than the previous code. (This is unimportant for a short script, but for a script which takes several seconds or longer, this can be significant.) Note the use of the array operator. ^ to square the array of sines. This is required since the operation sTheta^2 would perform the matrix multiplication sTheta*sTheta, and would result in an error since sTheta is not a square matrix. Theta=[0 : 0.01 : 3.14]; sTheta = sin(Theta); SumTh = sum(sTheta.^2); disp(['Sum is ' num2str(SumTh)]);
  • 5. * * * * * * * BASIC * * * * * * * * * * * * * * MATLAB * * * * * * * There are many matrix functions in Matlab that are not available at all in BASIC. Some of the more useful ones are shown below. COMMAND What it Does x = zeros(100,2); Makes x a 100 row by 2 column matrix with all zeros y = ones(20,20); Makes y a 20 row by 20 column matrix with all ones z = 0:.1:10; Puts the values of 0,.1,.2,...10 into the variable z a = z.^2; The .^2 operator will take each individual element of z and square it, so a will contain 0,.01,.04,...100 b = a.*z; The .* operator will take each individual element of a and multiply it by the same element in z. The equivalent code in BASIC would be for i=1 to 101 b(i) = a(i)*z(i) next i C = rand(3,3); D = ones(3,3); E = C*D; The first command makes C a 3-by-3 matrix of random numbers between 0 and 1. Matrix D will be all ones, and the matrix E will be the matrix product of these two matrices (so each element of the top row of E will be the sum of the first row of C. 2. Write a note on applications of MATLAB. Answer MATLAB can be used in the following Fields: *Computations including linear algebra, data analysis, signal processing, polynomials and interpolation, numerical integration, and numerical solution of differential equations. *Graphics, in 2-D and 3-D, including color, lighting, and animation. MATLAB (matrix laboratory) is a numerical computing environment and fourth- generation programming language. Developed by Math works MATLAB allows matrix manipulations, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs written in other languages, including C, C++, Java, and Fortran.
  • 6. Although MATLAB is intended primarily for numerical computing, an optional toolbox uses the MuPAD symbolic engine, allowing access to symbolic computing capabilities. An additional package, Simulink, adds graphical multi- domain simulation and Model-Based Design for dynamic and embedded systems. 3. Compute the following using MATLAB. a. The radius of a circle is 𝜋1/3 − 1 .Find the diameter, circumference, and Area.
  • 7. b. sinh ( 𝜋 3 ) + sin ( 𝜋 3 𝑐 ) ∗ cos 600 c. log(i) d. 1−2𝑖 1+3𝑖 e. Compare 35 35−1 and 1 − 1 35
  • 8. 4. Write a MATLAB program to a. Bit reversal of a 8 bit binary number. b. To exchange the upper four bits with lower four bits of a 8 bit Binary number.
  • 9. c. Find A and B , A or B , A xor B, given A = 10110010; B = 11010101
  • 10. 5. Find the value of x in the following a. 𝑥2 − 𝑥 + 4=0 b. 𝑒 𝑥 =16
  • 11. MATLAB MODULE 2 1. Write MATLAB function sigma = ascsum(x) that takes a one-dimensional array x of real numbers and computes their sum sigma in the ascending order of magnitudes. Hint: You may wish to use MATLAB functions sort, sum, and abs. ANSWER: CODE function[sigma]=ascsum(x) n=sort(x); m=sum(n); sigma=abs(m); end output:
  • 12. 2. In this exercise you are to write MATLAB function d = dsc(c) that takes a one- dimensional array of numbers c and returns an array d consisting of all numbers in the array c with all Neighboring duplicated numbers being removed. For instance, if c = [1 2 2 2 3 1], then d = [1 2 3 1]. ANSWER: CODE function d=dsc(x) d=unique(x); end output:
  • 13. 3. Write MATLAB function [in, fr] = infr(x) that takes an array x of real numbers and returns arrays in and fr holding the integral and fractional parts, respectively, of all numbers in the array x. ANSWER: CODE function[in,fr]=infr(x) in=fix(x); fr=x-in; end output:
  • 14. MATLAB MODULE 3 1. Given an array b and a positive integer m create an array d whose entries are those in the array b each replicated m-times. Write MATLAB function d = repel(b, m) that generates array d as described in this problem. Example: if b=[ 3 8 9 1], m=3 then result d is [3 3 3 8 8 8 9 9 9 1] ANSWER: CODE function d=rep(b,m) d=[]; for i=1:length(b) d=[d repmat(b(i),1,m(i))]; end end OUTPUT:
  • 15. 2. In this exercise you are to write MATLAB function d = rep(b, m) that has more functionality than the function repel of Problem 4. It takes an array of numbers b and the array m of positive integers and returns an array d whose each entry is taken from the array b and is duplicated according to the corresponding value in the array m. For instance, if b = [ 1 2] and m = [2 3], then d = [1 1 2 2 2]. ANSWER CODE function d=repel(b,m) x=repmat(b,m,1); d=reshape(x,1,size(b,2)*m); end OUTPUT:
  • 16. MATLAB MODULE 4 1. Print Prime Numbers from M to N. ANSWER CODE function [Prime] = PrimeNum(N,M) if (N>M || N <0 || M < 0) error('ERROR: Invalid Input, you entered biggere number first or you entered negative number as you interval.'); end Prime = []; for j=0:(M-N) if all(mod((N+j),2:((N+j)/2))), Prime = [Prime;N+j]; end end OUTPUT:
  • 17. 2. Fibonacci Series up to N numbers. Definition: The first two numbers in the Fibonacci sequence are 0 and 1 (alternatively, 1 and 1), and each subsequent number is the sum of the previous two Ex: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 …. ANSWER CODE function [f]=fib n = input('n (number of Fibonacci numbers to compute): '); f(1) = 1; f(2) = 1; for i = 3:n f(i) = f(i-1) + f(i-2); end end output:
  • 18. 3. Factorial of a given Number. Ex: 5! = 5 x 4 x 3 x 2 x 1 = 120 ANSWER CODE 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:
  • 19. MATLAB MODULE 5 1. Write a function to perform following set operations A. Union of A and B B. Intersection of A and B C. Complement of A and B (Assume A= {1, 2, 3, 4, 5, 6}, B= {2, 4, 6}) ANSWER: CODE %Enter Data a=input('Enter First Sets'); b=input('Enter Second Sets'); u=[1 2 3 4 5 6 7 8 9]; %To Perform Operations uni=union(a,b); intersection=intersect(a,b); acomplement=setdiff(u,a); bcomplement=setdiff(u,b); c=setdiff(u,uni); %Display Output display('Union Of Two Sets'); display(uni); display('Intersection Of Two Sets'); display(intersection); display('Complement Of First Sets'); display(acomplement); display('Complement Of Second Sets'); display(bcomplement); display('Complement Of A and B'); display(c);
  • 21. 2. Write any one of the program to do following things using file concept. A. Create or Open a file B. Read data from the file and write data to another file C. Append some text to already existed file D. Close the file ANSWER: CODE function fileoperation filepointer=fopen('file.txt','r'); i=0; while ~feof(filepointer) x=fscanf(filepointer,'%c',1); if ~isempty(x) i=i+1; a(i)=x; end end disp(a); fp1=fopen('writemode.txt','w'); fprintf(fp1,'%s',a); fp2=fopen('appendmode.txt','a'); fprintf(fp2,'%s',a); fclose(filepointer); fclose(fp1); fclose(fp2); end output:
  • 22.
  • 23. MATLAB MODULE 6 1. Write a program to solve given equation (𝑥) = cos(𝑥) - 1 2 Using following methods. (Assume appropriate initial guesses and tolerance is up to 6 digits after decimal point.) A. BISECTION ANSWER CODE % program for bisection a=input('Enter Function:','s'); f=inline(a); xl=input('Enter lower guess:') ; xu=input('Enter upper guess:'); tol=input('Enter tolerance(recommended 0.001):'); if f(xu)*f(xl)<0 else fprintf('Wrong Guess!Enter new guessn'); xl=input('Enter lower guess:n') ; xu=input('Enter upper guess:n'); end for i=2:1000 xr=(xu+xl)/2; if f(xu)*f(xr)<0 xl=xr; else xu=xr; end if f(xl)*f(xr)<0
  • 25. B. INTERPOLATION ANSWER CODE % Inter polation methode f=input('f(x)=','s'); tol=input('error tolerance =1e-5, new tolerance='); if length(tol)==0,tol=1e-5;end x1=input(' First guess='); x=x1; f1=eval(f); x2=input(' Second guess='); x=x2; f2=eval(f);xsave=x2; if f1*f2<0 disp(' False Position ') for i=2:21 x=x2-f2*(x2-x1)/(f2-f1); fx=eval(f); if fx*f1>0 x1=x;f1=fx; else x2=x;f2=fx; end fprintf('i = %g, x = %g, fx = %gn',i,x,fx) if abs(xsave-x)<tol, break,end xsave=x; end else disp('Need to bracket the roots') end
  • 26.
  • 27. C. NEWTON-RAPHSON ANSWER CODE % Newton raphson method with numerical derivative f=input('f(x)=','s'); tol=input('error tolerance =1e-5, new tolerance='); if length(tol)==0,tol=1e-5;end x1=input(' First guess='); x=x1; fx=eval(f); for i=1:100 if abs(fx)<tol, break,end x=x+.01; ff=eval(f); fdx=(ff-fx)/.01; x1=x1-fx/fdx; x=x1; fx=eval(f); fprintf('i = %g, x = %g, fx = %gn',i,x,fx) end Output
  • 28. D.SECANT ANSWER CODE % Secant method f=input('f(x)=','s'); tol=input('error tolerance =1e-5, new tolerance='); if length(tol)==0,tol=1e-5;end x1=input(' First guess='); x=x1; f1=eval(f);
  • 29. x2=input(' Second guess='); x=x2; f2=eval(f); xsave=x2; disp(' Secant method') for i=2:21 x=x2-f2*(x2-x1)/(f2-f1); fx=eval(f); x1=x2;f1=f2;ex=abs(x2-x); x2=x;f2=fx; fprintf('i = %g, x = %g, fx = %gn',i,x,fx) if ex<tol, break,end end OUTPUT
  • 31. ANSWER: CODE %%sine wave t=0:pi/1000:(2*pi); y= sin(t); z=plot(t,y,'r'); %for plotting graph grid on; %for showing grid lines xlim([0 2*pi]); %to set x limit ylim([-1 1]); %to set y limit set(gca,'XTick',[0 pi (2*pi)]); %to select tha values which x axis should take set(gca,'XTickLabel',{'0','1','2'}); %to select the values to be visible on x axis set(gca,'YTick',-1:.5:1); %to select tha values which y axis should take set(gca,'YTickLabel',{'-1','-0.5','0','0.5','1'}); %to select the values to be visible on y axis title('One Sine wave from 0 to 2pi','FontSize',12,'FontWeight','bold','Color','white'); %for setting title xlabel('X values(interms of pi)','FontSize',12,'FontWeight','bold','Color','cyan'); %for setting label for x axis ylabel('Sin(t)','FontSize',12,'FontWeight','bold','Color','green'); %for setting label for y axis set(gca,'Color','black','XColor','cyan','YColor','green'); %for setting axis colours set(gcf,'Color',[.3 .3 .3]); %for setting background set(z,'Color','red','LineWidth',2); OUTPUT:
  • 32. 2. Write a script for generating a. 1000 Poisson distributed random numbers with parameter λ= 5. b. 1000 normal distributed numbers with mean 10, variance 25 c. Plot the PDFs for the above two in the same plot using sub plot, name them appropriately. ANSWER: CODE clc clear all; close all; lambda = 5; random_sample1 = poissrnd(lambda,1,100) mu=10;
  • 33. sigma=25; random_sample2 = normrnd(mu,sigma) X=0:10:1000 Y = normpdf(X,mu,sigma) subplot(2,1,1) stem(Y); title('normal distribution function'); xlabel('normal pdf'); ylabel('probability') X=0:10:1000 Y1 = poisspdf(X,lambda) subplot(2,1,2) stem(Y1); title('possion distribution function'); xlabel('poission pdf'); ylabel('probability') figure subplot(2,1,1) plot(Y); title('normal distribution function'); xlabel('normal pdf'); ylabel('probability') subplot(2,1,2) plot(Y1); title('possion distribution function'); xlabel('poission pdf'); ylabel('probability') OUTPUT:
  • 34.
  • 35. 3. Write a script to load the data file randomData.mat (which contains variables x and y) and fit first, second, third, fourth, and fifth degree polynomials to it. Plot the data as blue dots on a figure, and plot all five polynomial fits using lines of different colors on the same axes. Label the figure appropriately. Hint: To get good fits, you’ll have to use the centering and scaling version of polyfit (the one that returns three arguments, see help) and its counterpart in polyval (the one that accepts the centering and scaling parameters). ANSWER: CODE clc; clear all; load('randomData.mat') polyy=polyfit(x,y,1); figure ws = warning('off','all'); f=polyval(polyy,x); plot(f,'o'); hold on; ws = warning('off','all'); polyy=polyfit(x,y,2); f=polyval(polyy,x); plot(f,'.g'); ws = warning('off','all'); polyy=polyfit(x,y,3); f=polyval(polyy,x); plot(f,'-m'); ws = warning('off','all'); polyy=polyfit(x,y,4); f=polyval(polyy,x); plot(f,'-+r'); ws = warning('off','all'); polyy=polyfit(x,y,5); f=polyval(polyy,x); plot(f,'p'); legend('order 1','order 2','order 3','order 4','order 5'); title('POLYNOMIALS','fontweight','bold','color','m'); warning(ws); % Turn it back on.
  • 36. OUTPUT: 4. Write a script to open a new figure and plot five circles, all centered at the origin and with increasing radii. Set the line width for each circle to something thick (at least 2 points), and use the colors from a 5-color jet colormap (jet). ANSWER: CODE clc; %Clear screen clear all; %Clear all varibles close all; %Close all unwanted windows t=0:0.001:10; %Initializing time samples %Transfer chara of sine and cosi.e sin Vs Cos will give circle
  • 37. %Here we are plotting group of sine Vscos with differnt amplitudes s1=3*sin(2*pi*t); %Sine wave with amplitude=3unit c1=3*cos(2*pi*t); %Cosine wave with amplitude=3unit s2=2*sin(2*pi*t); c2=2*cos(2*pi*t); s3=1*sin(2*pi*t); c3=1*cos(2*pi*t); s4=4*sin(2*pi*t); c4=4*cos(2*pi*t); s5=5*sin(2*pi*t); c5=5*cos(2*pi*t); hold on; plot(s1,c1,'r','LineWidth',4) plot(s2,c2,'k','LineWidth',2) plot(s3,c3,'g','LineWidth',6) plot(s4,c4,'y','LineWidth',8) plot(s5,c5,'m','LineWidth',10) legend('r= 3-width=4', 'r= 2-width=2', 'r= 1-width=6', 'r= 4-width=8', 'r= 5- width=10') %grid on;%Enable grid lines axis equal;%Equal width of X and Y axis OUTPUT:
  • 38. 5. Record your name pronounced by yourself. Try to display the signal(name) in a plot vs Time, using matlab. ANSWER: CODE % Record your voice for 5 seconds. recObj = audiorecorder; disp('Start speaking.') recordblocking(recObj, 5); disp('End of Recording.'); % Play back the recording. play(recObj); % Store data in double-precision array. myRecording = getaudiodata(recObj);
  • 39. % Plot the samples. plot(myRecording); OUTPUT:
  • 40. MATLAB MODULE 8 1. Take your picture and  Show R, G, B channels along with RGB Image in same figure using sub figure.  Convert into HSV( Hue, saturation and value) and show the H,S,V channels along with HSV image Answer Code Im1 = imread('vik4.jpg'); figure; subplot(2,2,1); imshow(Im1); title('Original color Image', 'FontSize',10); %Im2 = imadjust(Im1, [0.2, 0.8], [0, 1]); % Display the original color image. % Extract the individual red, green, and blue color channels. redChannel = Im1(:,:,1); greenChannel = Im1(:,:,2); blueChannel = Im1(:,:,3); subplot(2,2,2),imshow(redChannel); title('Red Channel','Color','m', 'FontSize', 10); subplot(2,2,3),imshow(greenChannel); title('Green Channel','Color','r', 'FontSize', 10); subplot(2,2,4),imshow(blueChannel); title('Blue Channel','Color','g', 'FontSize', 10); img=imread('vik4.jpg'); [H S V]=rgb2hsv(img); figure; subplot(2,2,1); imshow(img); h = img(:, :, 1); % Hue image. s = img(:, :, 2); % Saturation image.
  • 41. v = img(:, :, 3); % Value (intensity) image. subplot(2,2,2),imshow(H); title('HUE IMAGE','Color','m', 'FontSize', 10); subplot(2,2,3),imshow(S); title('SATURATION','Color','r' ,'FontSize', 10); subplot(2,2,4),imshow(V); title('VALUE', 'Color','g','FontSize', 10); Output:
  • 42. 2. Using EZ plot & anonymous functions plot the following:  Y=Sqrt(X)  Y= X^2  Y=e^(-XY) Answer Code clc; clear all; close all; subplot(3,1,1); ezplot(@(x) sqrt(x)); grid on; subplot(3,1,2); ws = warning('off','all'); ezplot(@(x) x^2); grid on;
  • 43. subplot(3,1,3); syms x y; ws = warning('off','all'); ezplot(y==exp(x*y)); grid on; Output:
  • 44. MATLAB MODULE 9 1. Write a MATLAB program that converts a decimal number to Roman number and vice versa. Answer: DECIMAL TO ROMAN function ans = decimal2roman(z) d = [ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]; c = {'M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'}; []; for ii = 1:numel(d) if z >= d(ii) ans = [ans,repmat(c{ii},1,fix(z/d(ii)))]; z = rem(z,d(ii)); end end output: Answer: ROMAN TO DECIMAL clc x=input('Enter the roman numeral: ','s'); l=length(x);for(i=1:l) if(x(i)=='I') a(i)=1; elseif(x(i)=='V') a(i)=5;
  • 45. elseif(x(i)=='X') a(i)=10; elseif(x(i)=='L') a(i)=50; elseif(x(i)=='C') a(i)=100; elseif(x(i)=='D') a(i)=500; elseif(x(i)=='M') a(i)=1000; else disp('Not a valid entry'); end end d=a(l); for(i=l:-1:2) if(a(i)>a(i-1)) d=d-a(i-1); elseif(a(i)==a(i-1) || a(i)<a(i-1)) d=d+a(i-1); end end disp('The decimal equivalent is: '); disp(d); OUTPUT:
  • 46. 2. ANSWER: CODE clc x = {'joe' 'smith' 30000 ; 'sarah' 'brown' 150000;'pat' 'jackson' 120000 }; disp(x); yes=input('want to change data :n if yes press 1 else press 2 '); if (yes==1) disp('enter the details n '); row=input('row : '); coloumn=input('column : '); k=input('enter data : '); x{row,coloumn}=k; disp(x); else end
  • 47. Output: 3. A supermarket conveyor belt holds an array of groceries. Construct a structure with superMarket, which contains costumer id, total bill a structure with names of the item, and corresponding cost of the items, quantity of the items. Initialize the structure for 100 costumers. Write a program to prepare bill by taking the inputs from the user.
  • 48. ANSWER: CODE function supermarket clc; fprintf('******************************************n'); fprintf('welcome to vikas supermarketn'); fprintf('******************************************n'); item1='tshirt_1'; price1=500; item2='jeans_2'; price2=2000; item3='shoes_3'; price3=600; item =struct(item1,price1,item2,price2,item3,price3); for i=1:100 f1='id'; v1=input('please enter the customer number : ','s'); f2='item'; v2=item; f3='bill'; v3=0; disp(item); n=input('enter item number:'); q=input('enter quantity:'); if n==1 v3=v3+(q*price1); elseif n==2 v3=v3+(q*price2); elseif n==3 v3=v3+(q*price3); end fprintf('Do you wish to continuenpress-1 for yesnpress-2 for non'); choice= input('enter choice :'); if(choice==2) break; end end disp('bill : ');
  • 50. MATLAB MODULE 10 Generate videos in which a skeleton of a person doing the following Gestures.  Tilting his head to right and left  Tilting his hand to right and left  Walking The skeleton looks something like this.
  • 51. Answer: Tilting his hand to right and left clc; clear all; close all; figure x1=[2,2]; y1=[4.5,4]; x2=[2,1.5]; y2=[4,3.5]; x3=[2,2.5]; y3=[4,3.5]; x4=[1.5,1.5]; y4=[3.5,3]; x5=[2.5,2.5]; y5=[3.5,3]; x6=[2,2.5]; y6=[3,1.5]; x7=[2,1.5]; y7=[3,1.5]; x8=[1.5,1.5]; y8=[1.5,1]; x9=[2.5,2.5]; y9=[1.5,1]; x10=[2,2]; y10=[4,3]; plot(2,4.5,'O',2.5,3.5,'O',1.5,3.5,'O',2.5,1.5,'O',1.5,1.5,'O',x1,y1,x2,y2,x3,y 3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,x9,y9,x10,y10,'color','k','linewidth',4); axis([0 6 0 6]); a(1)=getframe; x4=[1.5,1.8]; y4=[3.5,3]; x5=[2.5,2.8]; y5=[3.5,3]; plot(2,4.5,'O',2.5,3.5,'O',1.5,3.5,'O',2.5,1.5,'O',1.5,1.5,'O',x1,y1,x2,y2,x3,y 3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,x9,y9,x10,y10,'color','k','linewidth',4); axis([0 6 0 6]); a(2)=getframe;
  • 52. x4=[1.5,1.2]; y4=[3.5,3]; x5=[2.5,2.2]; y5=[3.5,3]; plot(2,4.5,'O',2.5,3.5,'O',1.5,3.5,'O',2.5,1.5,'O',1.5,1.5,'O',x1,y1,x2,y2,x3,y 3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,x9,y9,x10,y10,'color','k','linewidth',4); axis([0 6 0 6]); a(3)=getframe; for i=0:60 movie(a,2,2); %press cntrl c to end the video end Answer: Tilting his head to right and left clc; clear all; close all; figure; x1=[2,2]; y1=[4.5,4]; x2=[2,1.5]; y2=[4,3.5]; x3=[2,2.5]; y3=[4,3.5]; x4=[1.5,1.5]; y4=[3.5,3]; x5=[2.5,2.5]; y5=[3.5,3]; x6=[2,2.5]; y6=[3,1.5]; x7=[2,1.5]; y7=[3,1.5]; x8=[1.5,1.5]; y8=[1.5,1]; x9=[2.5,2.5]; y9=[1.5,1];
  • 53. x10=[2,2]; y10=[4,3]; plot(2,4.5,'O',2.5,3.5,'O',1.5,3.5,'O',2.5,1.5,'O',1.5,1.5,'O',x1,y1,x2,y2,x3,y 3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,x9,y9,x10,y10,'color','k','linewidth',4); axis([0 6 0 6]);%to set up axes limits a(1)=getframe; x1=[2.5,2]; y1=[4.5,4]; plot(2.5,4.5,'O',2.5,3.5,'O',1.5,3.5,'O',2.5,1.5,'O',1.5,1.5,'O',x1,y1,x2,y2,x3, y3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,x9,y9,x10,y10,'color','k','linewidth',4); axis([0 6 0 6]); a(2)=getframe; x1=[1.5,2]; y1=[4.5,4]; plot(1.5,4.5,'O',2.5,3.5,'O',1.5,3.5,'O',2.5,1.5,'O',1.5,1.5,'O',x1,y1,x2,y2,x3, y3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,x9,y9,x10,y10,'color','k','linewidth',4); axis([0 6 0 6]); a(3)=getframe; for i=1:60 movie(a,2,3); %press ctrl c to end the video end axis equal Answer: Walking clc; clear all; close all; figure x1=[2,2]; y1=[4.5,4]; x2=[2,1.5]; y2=[4,3.5]; x3=[2,2.5]; y3=[4,3.5]; x4=[1.5,1.5];
  • 54. y4=[3.5,3]; x5=[2.5,2.5]; y5=[3.5,3]; x6=[2,2.5]; y6=[3,1.5]; x7=[2,1.5]; y7=[3,1.5]; x8=[1.5,1.5]; y8=[1.5,1]; x9=[2.5,2.5]; y9=[1.5,1]; x10=[2,2]; y10=[4,3]; plot(2,4.5,'O',2.5,3.5,'O',1.5,3.5,'O',2.5,1.5,'O',1.5,1.5,'O',x1,y1,x2,y2,x3,y 3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,x9,y9,x10,y10,'color','k','linewidth',4); axis([0 6 0 6]); a(1)=getframe; x4=[1.5,1.5]; y4=[3.5,3]; x5=[2.5,3]; y5=[3.5,4]; x8=[1.5,1]; y8=[1.5,1]; x9=[2.5,2.5]; y9=[1.5,1]; plot(2,4.5,'O',2.5,3.5,'O',1.5,3.5,'O',2.5,1.5,'O',1.5,1.5,'O',x1,y1,x2,y2,x3,y 3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,x9,y9,x10,y10,'color','k','linewidth',4); axis([0 6 0 6]); a(2)=getframe; x4=[1.5,1];y4=[3.5,3];x5=[2.5,2.5];y5=[3.5,3]; x8=[1.5,1.5];y8=[1.5,1];x9=[2.5,3];y9=[1.5,1]; plot(2,4.5,'O',2.5,3.5,'O',1.5,3.5,'O',2.5,1.5,'O',1.5,1.5,'O',x1,y1,x2,y2,x3,y 3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,x9,y9,x10,y10,'color','k','linewidth',4); axis ([0 6 0 6]); a(3)=getframe; for i=0:60 movie(a,2,5); %press ctrl c to end the video end
  • 55. For the last module i.e. Module 10, by running the program we get the output as video. So not attached in this document.