Matlab Manual DR - Bhawna
Matlab Manual DR - Bhawna
LAB MANUAL
Software Lab(SciLab/Matlab)
SEMESTER3rd
RUNGTACOLLEGE
Rungta Educational
Campus,Kohka-Kurud Road,
Bhilai,
Chhattisgarh, India
MANAGEDBY:SANTOSHRUNGTAGROUPOFINSTITUTIONS
Phone No. 0788-6666666
DEPARTMENTOFComputerScienceandEngineering
LAB MANUAL
Software LAB
SEMESTER3rd
PREPAREDASPERTHESYLLABUSPRESCRIBEDBY
CHHATTISGARHSWAMIVIVEKANANDTECHNICALUNIVERSITY,BHILAI
ListofDOs&DON’Ts.
(Giveinstructionsasper<NameOfTheDepartment>Laboratories)DOs:
▪ Removeyourshoesoutsidethelaboratory.
▪ Comepreparedinthelabregardingtheexperimenttobeperformedinthelab.
▪ TakehelpfromtheManual/WorkBookforpreparationoftheexperiment.
▪ ForanyabnormalworkingofthemachineconsulttheFacultyIn-charge/LabAssistant.
▪ Shutdownthemachineandswitchoffthepowersupplyafterperformingtheexperiment.
▪ Maintainsilenceandproperdisciplineinthelab.
▪ EnteryourmachinenumberintheLoginregister.
DON’Ts:
▪ Donotbringanymagneticmaterialintothelab.
▪ Donoteatordrinkanythinginthelab.
▪ DonottamperwiththeinstrumentsintheLabanddonotdisturbtheirsettings.
LISTOF XPRI TS
ASPERTHESYLLABUSPRESCRIBEDBYTHEUNIVERSITY
ChhattisgarhSwamiVivekanandaTechnicalUniversity,Bhilai
Branch:ComputerScienceandEngineering Semester:III
Subject:SoftwareLaboratory(SciLAB/MATLAB)
SubjectCode:B022324(022)Tot
alLabPeriods:36 BatchSize:30
MaximumMarks:40 MinimumMarks:20
COURSEOUTCOMES
Aftersuccessfulcompletionofthiscourse,thestudentswillbeableto-
1. UnderstandthemainfeaturesoftheMATLAB/SCILABprogramdevelopmentenvironmenttoenabl
etheirusageinhigherlearning.
2. Realizethepowerofinteractivecalculation,programming,graphics,andanimationinSciL
AB/MATLABandcompleteportabilityacrossplatforms.
3. EnjoySciLAB/MATLABasascientificcomputingandvisualizationtool.
4. ExploreInteractiveComputationwithmatricesandarraysofn-dimensions.
5. Interpretandvisualizesimplemathematicalfunctionsandoperationsthereonusingplots/display.
ListofExperiments
1. Identificationofdifferentmatrixtypes.
2. Properties and Operations of arrays and matrices.
3. Writeaprogramtofindtheprobabilityoftossingacoinandrollingadiethroughalargenumber of
experimentation.
4. Compute ycoordinates of a STRAIGHT LINE y = mx + c, where slope of line m
=0.5,intercept c = - 2 and x-coordinates : x = 0 to 10 for 0.5 increments
5. Plot y=Sin x where 0 ≤x≤2.
6. Plot y = e^ - 0.4sin x where : 0 <= x <= 4
7. Find the solution of linear algebraic equations in 2 variables, 3
variables:a.x + 4y = 18; 2x + 3y = 16
b.x + 2y + 3z = 1; 3x + 3y + 4z = 1; 2x + 3y + 3z = 2
8. Determination of roots of a given polynomial & quadratic equations.
9. DeterminationofEigenValue&EigenVectorsformatrices.
10. Writeascriptfiletodrawaunitcircle.
11. Writeafunctionfactorial tocomputethefactorial n!foranyinteger n.
12. Writeafunction factorialtocompute thefactorialn! usingRECURSIONfor anyintegern
13. Writeafunctiontocomputethegeometricseries1 +
r +r2+r3+. ..............+rn for given r and n.
14. Writeafunctionfilecrossprodtocomputethecrossproductoftwovectorsuandv.
15. Design a toy project as an independent study towards problem-based learning.
RecommendedBooks
1. GettingstartedwithMATLAB:AQuickIntroductionforScientistsandEngineersbyRudraPratap,IISBangalo
re.
2. ScilabManualforProbabilityTheoryandStatisticsLabbyProfSNChandraShekhar,https://s
cilab.in/labmigrationrun/82
3. ScilabManualforNumericalTechniqueslabbyProfKanikaGupta;https://scilab.in/labmigrationrun/82
4. ScilabManualforProbabilityTheoryandRandomProcessesbyProfShitalThakkar,https:/
/scilab.in/lab_migrationrun/82.
5. ScilabManualforNumericalTechniquesbyDrJavedDhillon:https://scilab.in/labmigrationrun/82
LISTOFEXPERIMENTS
AS PER RUNGTA COLLEGE OF ENGINEERING & TECHNOLOGY
VIVA QUESTIONS :
1. What is MATLAB?
2. Explain the Features of MATLAB.
3. Explain the Advantages and Disadvantages of MATLAB.
4. Explain all Data Types used in MATLAB.
5. What is the difference between a script and a function in MATLAB? When would you use each?
1
B-TECH3rdSemCSE
Software Laboratory (Sci
Lab/MATLAB)Code:B022324(022)
2 Findthesumoftheintegersfrom1 to100.
Ans: >>x=1:100;
>>
sum(x)ans
5050
>>sum(1:100)
ans=
5050
VIVA QUESTIONS:
1. Write a script that uses a for loop to calculate the sum of the first 100 integer numbers.
2. What is MATLAB syntax?
3. How to write function in MATLAB?
4. What are the three tools of MATLAB?
2
B-TECH3rdSemCSE
Software Laboratory (Sci
Lab/MATLAB)Code:B022324(022)
>>n=input('Enteranumberforn')
Enter a number for n3n=
>>sum=1;
>>x=1;
>> for
i=1:nx=x*r;sum
=sum+x;end
>>
sumsum
=156
VIVA QUESTIONS :
3
B-TECH3rdSemCSE
Software Laboratory (Sci
Lab/MATLAB)Code:B022324(022)
25],1,6)myvector=
24 25 20 25 23 20
>>
newvector=myvector.*5ne
wvector=
120125100125115100
>>
VIVA QUESTIONS :
4
B-TECH3rdSemCSE
Software Laboratory (Sci
Lab/MATLAB)Code:B022324(022)
5 Create a vector spanning the range from 0 to 2π, containing 100 equally
spacedcomponents,sothatthefirstvalueis0, andthe lastvalueis 2π.
Ans: >>myvector=linspace(0,2*pi,100)
myvector=
Columns1through7
Columns8through14
Columns15through21
Columns22through28
Columns29through35
Columns36through42
Columns43through49
Columns50through56
Columns57through63
Columns64through70
Columns71through77
Columns78through84
Columns85through91
5
5.3312 5.3947 5.4581 5.5216 5.5851 5.6485 5.7120
Columns92through98
Columns99through100
6.2197 6.2832
>>
VIVA QUESTIONS :
6
B-TECH3rdSemCSE
Software Laboratory (Sci
Lab/MATLAB)Code:B000324(022)
6 Createa)Matrixofzero’sb) Matrixof one’sc) Identitymatrixd)RandomMatrix
Ans: >>
x=zeros(3)x=
0 0 0
0 0 0
0 0 0
>>
x=ones(3)x=
1 1 1
1 1 1
1 1 1
>>
x=eye(3)x=
1 0 0
0 1 0
0 0 1
>>
x=rand(3)x=
VIVA QUESTIONS :
1. How do you create a zero matrix in MATLAB?
2. How do you create a matrix of ones in MATLAB?
3. Which MATLAB command is used to create an identity matrix?
7
B-TECH3rdSemCSE
Software Laboratory (Sci
Lab/MATLAB)Code:B000324(022)
7 Create a matrix of 10 rows and 10 columns. The odd columns should contain values
2,andthe evencolumns, values 0.
Ans >>x=zeros(10);
>>x(:,1:2:10)=2;
>>
xx=
2 0 2 0 2 0 2 0 2 0
2 0 2 0 2 0 2 0 2 0
2 0 2 0 2 0 2 0 2 0
2 0 2 0 2 0 2 0 2 0
2 0 2 0 2 0 2 0 2 0
2 0 2 0 2 0 2 0 2 0
2 0 2 0 2 0 2 0 2 0
2 0 2 0 2 0 2 0 2 0
2 0 2 0 2 0 2 0 2 0
2 0 2 0 2 0 2 0 2 0
VIVA QUESTIONS :
8
B-TECH3rdSemCSE
Software Laboratory (Sci
Lab/MATLAB)Code:B000324(022)
8 Createa5x5matrix.Deletethelastrowandlastcolumnof thematrix.
Ans: >> x=randi([5
10],5)x=
6 7 9 10 10
9 10 6 8 6
8 7 8 5 9
5 8 9 5 6
5 6 10 6 10
columnx=
6 7 9 10
9 10 6 8
8 7 8 5
5 8 9 5
5 6 10 6
>>x(1,:)=[]
%Deletefirstrow
x=
9 10 6 8
8 7 8 5
5 8 9 5
5 6 10 6
VIVA QUESTIONS :
10
B-TECH3rdSemCSE
Software Laboratory (Sci
Lab/MATLAB)Code:B000324(022)
9 Create a 4x4 matrix. Show Inverse matrix, Upper triangle and Lower Triangle
Matrix,Diagonal Matrix, Determinant
Ans: >> A=[9,7,0,4; 0,8,6,9; 7,1,-6,3; 4,3,2,1]%Create a 4 x 4
MatrixA=
9 7 0 4
0 8 6 9
7 1 -6 3
4 3 2 1
>>A' %InverseMatrix
ans=
9 0 7 4
7 8 1 3
0 6 -6 2
4 9 3 1
Matrixans =
9 7 0 4
0 8 6 9
0 0 -6 3
0 0 0 1
Matrixans =
9 0 0 0
0 8 0 0
7 1 -6 0
4 3 2 1
>>diag(A) % Diagonal
Matrixans =
9
8
-6
1
>>det(A) % Matrix
determinantans =
-608.0000
11
VIVA QUESTIONS :
12
B-TECH3rdSemCSE
Software Laboratory (Sci
Lab/MATLAB)Code:B000324(022)
Columns1through7
Columns8through14
Columns15through21
VIVA QUESTIONS :
13
B-TECH3rdSemCSE
Software Laboratory (Sci
Lab/MATLAB)Code:B000324(022)
>>x=0:0.01:10;
>>y=exp(sin(x).*(-0.4));
>>plot(x,y)
VIVA QUESTIONS :
12 Plot 4differentsignalinasinglefigurewithproperlabel.
Ans: >>x=0:0.01:10;
>>y1=exp(x);
>>y2=cos(x);
>>y3=sin(x);
>>y4=log(x);
>>figure
>>subplot(2,2,1),plot(x,y1,'r'),ylabel("Exp(x)"),xlabel("X")
>>subplot(2,2,2),plot(x,y2,'g'),ylabel("Cos(x)"),xlabel("X")
>>subplot(2,2,3),plot(x,y3,'b'),ylabel("Sin(x)"),xlabel("X")
>>subplot(2,2,4),plot(x,y4,'m'),ylabel("Log(x)"),xlabel("X")
VIVA QUESTIONS :
15
B-TECH3rdSemCSE
Software Laboratory (Sci
Lab/MATLAB)Code:B000324(022)
13 Assumeacarcompany;Productionof 6yearsfrom2010to2015isgivenas30000,
25000,40000,65000,45000 and 20000. Write the code to create a bar chart to
visualizethegiveninformation.
Ans: >>year=[2010:2015];
>>sales=[30000,25000,40000,65000,45000,20000];
>>bar(year,sales)
>>xlabel("Year")
>>ylabel("Sales")
>>title("YearWiseSalesDetails")
VIVA QUESTIONS :
16
B-TECH3rdSemCSE
Software Laboratory (Sci
Lab/MATLAB)Code:B000324(022)
14 Determinationofrootsofagivenpolynomial&quadraticequations.
Ans: >> equation='x^2 - 7*x + 12 =
0'equation=
x^2-7*x+12 =0
>>
root=solve(equation)roo
t=
3
4
Coefficientrepresentationoffunction
>>x=roots([1,-7,12]) %x^2 -
7*x+12=0x=
4
3
17
B-TECH3rdSemCSE
Software Laboratory (Sci
Lab/MATLAB)Code:B000324(022)
18
B-TECH3rdSemCSE
Software Laboratory (Sci
Lab/MATLAB)Code:B000324(022)
r.xans
=3
>>
r.yans
=2
>>r=solve('x+2*y+3*z=1','3*x+3*y+4*z=1','2*x+3*y+3*z=2');
>>
r.xans
-1/2
>>
r.yans
=3/2
>>
r.zans
-1/2
19
>>symsx;
>>symsy;
>>symsz;
>>A=[1 2 3;3 34;23 3]; %CoefficientMatrixA
>>b=[1 ;1;2]; %KnownConstantVector
>>X=[x;y;z]; %UnknownVector
>>X=A\b %Solutionof
unknownvectorAx=bX=
-0.5000
1.5000
-0.5000
solutionans =
1.0000
1.0000
2.0000
VIVA QUESTIONS :
16 DeterminationofEigenvalueandEigenvectorformatrices.
Ans: >>x=rand(3) %Random
Matrixx=
vectorsA=
0.6752-0.7134-0.5420
-0.7375-0.6727-0.2587
-0.0120-0.1964 0.7996
D=
-0.1879 0 0
0 1.7527 0
0 0 0.8399
VIVA QUESTIONS :
21
B-TECH3rdSemCSE
Software Laboratory (Sci
Lab/MATLAB)Code:B022324(022)
VIVA QUESTIONS :
22
B-TECH3rdSemCSE
Software Laboratory (Sci
Lab/MATLAB)Code:B022324(022)
18 Writeafunctionfactorialtocomputethefactorialn!Foranyintegern.
Ans: %Createamfile.Saveasfactorial.m
function[result]=factorial(n)
%factorial(n)willcalculaten!result=1;
fori=1:n
result=result*i;
end
end
functionvalue=
120
VIVA QUESTIONS :
23
B-TECH3rdSemCSE
Software Laboratory (Sci
Lab/MATLAB)Code:B022324(022)
19 DevelopanyMatlabbasedapplication.
Ans: UsingImageProcessingToolbox
24
QUESTIONS BEYOND SYLLABUS
Experiment no. 1
SOURCE CODE:
>>marks=[2530234027]
marks=
25 30 23 40 27
>>
new_marks=marks+5new_marks=
30 35 28 45 32
>>
25
Experiment no.2
AIM : InanInstitution,placement,andhigherstudydetailsareasfollows:
Year Placement Higher
Study
2017 60 40
2018 78 22
2019 75 25
Writethecodetocreateabarcharttovisualizethegiveninformation.
SOURCE CODE :
>>year=[2017:2019];
>>placement=[60;78;75];
>>higher_study=[40;22;25];
>>student_record=[placement,higher_study];
>>bar(year,student_record)
>>legend('Placement','HigherStudy')
26
EXPERIMENT NO. 3
AIM : In a city, 40% people speak Hindi, 20% speak Tamil, 30% speak
Hindiand10%speakMarathi.Writethecodetocreateapiecharttovisualizethegiveninformation.
SOURCE CODE :
>>data_set=[40,20,30,10];
>>labels={'Hindi','Tamil','Bengali','Marathi'};
>>pie(data_set,labels)
27