B61301007 matlab documentation

67
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

description

MATLAB DOCUMENTATION ON SOME OF THE MODULES A.Generate videos in which a skeleton of a person doing the following Gestures. 1.Tilting his head to right and left 2.Tilting his hand to right and left 3.Walking in matlab. B. Write a MATLAB program that converts a decimal number to Roman number and vice versa. C.Using EZ plot & anonymous functions plot the following: · Y=Sqrt(X) · Y= X^2 · Y=e^(-XY) D.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 E.Record your name pronounced by yourself. Try to display the signal(name) in a plot vs Time, using matlab. F.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). G. NEWTON RAPHSON AND SECANT METHOD H.Write any one of the program to do following things using file concept. 1.Create or Open a file 2. Read data from the file and write data to another file 3. Append some text to already existed file 4. Close the file I.Write a function to perform following set operations 1.Union of A and B 2. Intersection of A and B 3. Complement of A and B (Assume A= {1, 2, 3, 4, 5, 6}, B= {2, 4, 6})

Transcript of B61301007 matlab documentation

Page 1: B61301007 matlab documentation

Documentation

On

MATLABReport 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

Page 2: B61301007 matlab documentation

Table of ContentsMATLAB 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

Page 3: B61301007 matlab documentation

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 = 2P = 3.14A = P*R^2PRINT 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"

if Sel == 1 disp('Selection is 1');

Page 4: B61301007 matlab documentation

elseif Sel = 2 then print "Option 2 Here"else print "None of the Aboveendif

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 oncewhile 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 = 0For Theta=0 To 3.14 Step 0.001 SumTh = SumTh + sin(Theta)^2NextPrint "Sum is ";SumTh

SumTh = 0;for Theta=0 : 0.001 : 3.14; SumTh = SumTh + sin(Theta)^2;enddisp(['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.

Page 5: B61301007 matlab documentation

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 befor i=1 to 101b(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.

Page 6: B61301007 matlab documentation

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( π3c)∗cos600

Page 7: B61301007 matlab documentation

c. log(i)

d. 1−2 i1+3i

e. Compare 35

35−1 and1− 1

35

Page 8: B61301007 matlab documentation

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.

Page 9: B61301007 matlab documentation

c. Find A and B , A or B , A xor B, given A = 10110010; B =

11010101

Page 10: B61301007 matlab documentation

5. Find the value of x in the following

a. x2−x+4=0

b. ex=16

Page 11: B61301007 matlab documentation

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:

Page 12: B61301007 matlab documentation

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:

Page 13: B61301007 matlab documentation

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:

Page 14: B61301007 matlab documentation

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))]; endend

OUTPUT:

Page 15: B61301007 matlab documentation

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].

ANSWERCODE

function d=repel(b,m)x=repmat(b,m,1);d=reshape(x,1,size(b,2)*m);end

OUTPUT:

Page 16: B61301007 matlab documentation

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.');endPrime = [];for j=0:(M-N) if all(mod((N+j),2:((N+j)/2))), Prime = [Prime;N+j]; endend

OUTPUT:

Page 17: B61301007 matlab documentation

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 twoEx: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 ….

ANSWER

CODE

function [f]=fibn = input('n (number of Fibonacci numbers to compute): ');f(1) = 1; f(2) = 1;for i = 3:nf(i) = f(i-1) + f(i-2);endend

output:

Page 18: B61301007 matlab documentation

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 numbery = n ;% We go down to 0if n == 0 y = 1; else % We multiply by all the integers before ours, % one at a time... y = y * fact(n-1);end OUTPUT:

Page 19: B61301007 matlab documentation

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 Dataa=input('Enter First Sets');b=input('Enter Second Sets');u=[1 2 3 4 5 6 7 8 9];%To Perform Operationsuni=union(a,b);intersection=intersect(a,b);acomplement=setdiff(u,a);bcomplement=setdiff(u,b);c=setdiff(u,uni);%Display Outputdisplay('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);

Page 20: B61301007 matlab documentation

output:

Page 21: B61301007 matlab documentation

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 fileoperationfilepointer=fopen('file.txt','r');i=0;while ~feof(filepointer) x=fscanf(filepointer,'%c',1); if ~isempty(x) i=i+1; a(i)=x; endenddisp(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:

Page 22: B61301007 matlab documentation
Page 23: B61301007 matlab documentation

MATLAB MODULE 6

1. Write a program to solve given equation

(𝑥) = cos(𝑥) -12

Using following methods.

(Assume appropriate initial guesses and tolerance is up to 6 digits after decimal point.)

A. BISECTIONANSWERCODE

% program for bisectiona=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 guess\n'); xl=input('Enter lower guess:\n') ; xu=input('Enter upper guess:\n');endfor 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

Page 24: B61301007 matlab documentation

xu=xr; else xl=xr; end xnew(1)=0; xnew(i)=xr; if abs((xnew(i)-xnew(i-1))/xnew(i))<tol,break,endendstr = ['Root: ', num2str(xr), '']

Output:

Page 25: B61301007 matlab documentation

B. INTERPOLATIONANSWER CODE

% Inter polation methodef=input('f(x)=','s');tol=input('error tolerance =1e-5, new tolerance=');if length(tol)==0,tol=1e-5;endx1=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 = %g\n',i,x,fx) if abs(xsave-x)<tol, break,end xsave=x; endelse disp('Need to bracket the roots')end

Page 26: B61301007 matlab documentation
Page 27: B61301007 matlab documentation

C. NEWTON-RAPHSON

ANSWER CODE

% Newton raphson method with numerical derivativef=input('f(x)=','s');tol=input('error tolerance =1e-5, new tolerance=');if length(tol)==0,tol=1e-5;endx1=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 = %g\n',i,x,fx)end

Output

Page 28: B61301007 matlab documentation

D.SECANT

ANSWER

CODE

% Secant methodf=input('f(x)=','s');tol=input('error tolerance =1e-5, new tolerance=');if length(tol)==0,tol=1e-5;endx1=input(' First guess=');x=x1;f1=eval(f);

Page 29: B61301007 matlab documentation

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 = %g\n',i,x,fx) if ex<tol, break,end end

OUTPUT

Page 30: B61301007 matlab documentation

MATLAB MODULE 7

Page 31: B61301007 matlab documentation

ANSWER:CODE

%%sine wavet=0:pi/1000:(2*pi);y= sin(t);z=plot(t,y,'r'); %for plotting graphgrid on; %for showing grid linesxlim([0 2*pi]); %to set x limitylim([-1 1]); %to set y limitset(gca,'XTick',[0 pi (2*pi)]); %to select tha values which x axis should takeset(gca,'XTickLabel',{'0','1','2'}); %to select the values to be visible on x axisset(gca,'YTick',-1:.5:1); %to select tha values which y axis should takeset(gca,'YTickLabel',{'-1','-0.5','0','0.5','1'}); %to select the values to be visible on y axistitle('One Sine wave from 0 to 2pi','FontSize',12,'FontWeight','bold','Color','white'); %for setting titlexlabel('X values(interms of pi)','FontSize',12,'FontWeight','bold','Color','cyan'); %for setting label for x axisylabel('Sin(t)','FontSize',12,'FontWeight','bold','Color','green'); %for setting label for y axisset(gca,'Color','black','XColor','cyan','YColor','green'); %for setting axis coloursset(gcf,'Color',[.3 .3 .3]); %for setting backgroundset(z,'Color','red','LineWidth',2);

OUTPUT:

Page 32: B61301007 matlab documentation

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

clcclear all;close all;lambda = 5;random_sample1 = poissrnd(lambda,1,100)mu=10;

Page 33: B61301007 matlab documentation

sigma=25;random_sample2 = normrnd(mu,sigma)X=0:10:1000Y = normpdf(X,mu,sigma)subplot(2,1,1)stem(Y);title('normal distribution function');xlabel('normal pdf');ylabel('probability')X=0:10:1000Y1 = poisspdf(X,lambda)subplot(2,1,2)stem(Y1);title('possion distribution function');xlabel('poission pdf');ylabel('probability')figuresubplot(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:

Page 34: B61301007 matlab documentation
Page 35: B61301007 matlab documentation

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);figurews = 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.

Page 36: B61301007 matlab documentation

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 screenclear all; %Clear all variblesclose 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

Page 37: B61301007 matlab documentation

s1=3*sin(2*pi*t); %Sine wave with amplitude=3unitc1=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 linesaxis equal;%Equal width of X and Y axis

OUTPUT:

Page 38: B61301007 matlab documentation

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);

Page 39: B61301007 matlab documentation

% Plot the samples.plot(myRecording);

OUTPUT:

Page 40: B61301007 matlab documentation

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 imageAnswer

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.

Page 41: B61301007 matlab documentation

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:

Page 42: B61301007 matlab documentation

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;

Page 43: B61301007 matlab documentation

subplot(3,1,3);syms x y;ws = warning('off','all');ezplot(y==exp(x*y));grid on;

Output:

Page 44: B61301007 matlab documentation

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)); endend

output:

Answer:

ROMAN TO DECIMAL

clcx=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;

Page 45: B61301007 matlab documentation

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'); endendd=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:

Page 46: B61301007 matlab documentation

2.

ANSWER:

CODE

clcx = {'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

Page 47: B61301007 matlab documentation

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.

Page 48: B61301007 matlab documentation

ANSWER:

CODE

function supermarketclc;fprintf('******************************************\n');fprintf('welcome to vikas supermarket\n');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);endfprintf('Do you wish to continue\npress-1 for yes\npress-2 for no\n'); choice= input('enter choice :');if(choice==2) break;endenddisp('bill : ');

Page 49: B61301007 matlab documentation

s=struct(f1,v1,f2,v2,f3,v3);disp(s);fprintf('******************************************\n');fprintf('\t\tthankyou visit again\n');fprintf('******************************************\n');end

output:

Page 50: B61301007 matlab documentation

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.

Page 51: B61301007 matlab documentation

Answer:

Tilting his hand to right and left

clc;clear all;close all;figurex1=[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,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(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,y3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,x9,y9,x10,y10,'color','k','linewidth',4);

Page 52: B61301007 matlab documentation

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,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=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];

Page 53: B61301007 matlab documentation

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,y3,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 limitsa(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:60movie(a,2,3);%press ctrl c to end the video endaxis equal

Answer:

Walking

clc;clear all;close all;figurex1=[2,2];

Page 54: B61301007 matlab documentation

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,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(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,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;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];

Page 55: B61301007 matlab documentation

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,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=0:60

movie(a,2,5);%press ctrl c to end the video

endFor the last module i.e. Module 10, by running the program we get the output as video. So not attached in this document.