0% found this document useful (0 votes)
28 views5 pages

Lab No.1

The document is a lab report that provides an introduction to MATLAB. It defines MATLAB as a programming platform used for engineering and science. It then lists and explains several basic MATLAB commands for creating vectors, matrices, performing mathematical operations, and more. Examples are provided for each command. The report also includes two lab tasks - solving a cubic equation using fsolve, and converting a matrix to diagonal form without using the diag command.

Uploaded by

David James
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views5 pages

Lab No.1

The document is a lab report that provides an introduction to MATLAB. It defines MATLAB as a programming platform used for engineering and science. It then lists and explains several basic MATLAB commands for creating vectors, matrices, performing mathematical operations, and more. Examples are provided for each command. The report also includes two lab tasks - solving a cubic equation using fsolve, and converting a matrix to diagonal form without using the diag command.

Uploaded by

David James
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab report By: M.Hassan (2020-MC-274) Submitted To: Dr.

Arshi Khalid

Date: 15-09-2022
Lab No. 01
Objective:
1) To learn the basics of Matlab.

Matlab:
MATLAB is a programming and numeric computing platform used by millions of
engineers and scientists to analyze data, develop algorithms, and create models.

We will use Numerical Analysis calculated values and simulations.

Fig 1.1: Matlab Window

Matlab Commands:
 For Row Vector:

A = [1,2,3] or A = [1 2 3]

 For Column Vectors:

A = [1;2;3]

 For Matrices:

A = [1,2,3;4,5,6;7,8,9]

 To show the names of variables are using:


who

 To show the matrices type, Bytes, Name and type:

Whos matrix_name

1
Lab report By: M.Hassan (2020-MC-274) Submitted To: Dr. Arshi Khalid

 To clear single Variable data:

clear A

 This will clear the data stored in variable A.


 To declare an identity matrix:

Eye (matrix_order)

 For example, eye (4) will create a 4 x 4 identity matrix and eye (3,4) will create a 3 x 4 identity matrix.

 To clear the command window:

clc

 To clear all the variables stored in workspace:

clearvars

 To find Sin, Cos & Tan:

sin(angle)

The default angle is always in Radian in Matlab. We could replace sin with cos & tan for their values.

In order to input angle in degree, we write the command as follows

sind(angle)

 To find cosec, sec & cot:

asind(angle) (⁘ to find cosec angle)

 To write power of some variable:

2^2 (⁘ Here 2 is the variable whose power is 2)

 To find square root of a variable:

sqrt(variable)

 To find out about something in Matlab:

help command_name

for example: help eye will tell us about eye command.

 To generate a matrix of 1’s:

ones (order_of_matrix)

for example: ones (3,4) will generate a matrix of order 3 x 4 having 1’s.

 To generate a matrix of 0’s:

zeros (order_of_matrix)

for example: zeros (3,4) will generate a matrix of order 3 x 4 having 0’s.

2
Lab report By: M.Hassan (2020-MC-274) Submitted To: Dr. Arshi Khalid

 To replace a specific entry of a matrix with required number:

Name_of_the_matrix (position_of_the_element) = Element_to_replace_with

for example: b (3,4) = 4 will replace the 3rd row and 4th column entry with 4.

 To make some_line comment:

% sign is used. (at the start of that line)

 To generate a string:

‘Characters_of_the_strings_to_be_generated’

for example: b = ‘Hello’ will generate a string of hello characters.

 To generate a random of matrix of any order:

B = rand (order_of_the_random_matrix)

OR B = magic (order_of_the_matrix)

for example: B = rand (3,4) will generate a random matrix of 3 x 4.

B = magic (4) will generate a random marix of 4 x 4.

Note: Magic will always generate a square matrix

 To know the size/order of a mtrix:

Size (name_of_the_matrix)

for example: size (b) will tell us the order of the matrix b.

 To know the length/number of columns of a mtrix:

length (name_of_the_matrix)

for example: length (b) will tell us the number of columns of matrix b.

 For indexing of a specific column from a matrix:

a ( : , No. of column which we want to remove) = [ ]

for example: if a is a 3 x 4 matrix then a (:,4) = [ ] will remove the 4th column of the matrix and display
the remaining matrix.

 For indexing of a specific row from a matrix:

A ( No. of row which we want to remove , :) = [ ]

for example: if a is a 3 x 4 matrix then a (3,:) = [ ] will remove the 3rd row of the matrix and display the
remaining matrix.

 To initialize some character as variable:


syms name_of_the_vaiable
3
Lab report By: M.Hassan (2020-MC-274) Submitted To: Dr. Arshi Khalid

for example: syms x will declare character x as a variable.

 To generate a linearly spaced vector:

X = linspace(X1, X2, N) generates N points between X1 and X2.

for example: x = linspace (-10, 10, 100) will generate a linearly spaced vector from -10 to 10 having 100
entities.

Note:

o If we want to declare/calculate something in main program and don’t want to show that value in
command window, then we will put semicolon (;) sign at the end of that line.
o The name of the main program should not start from a number (like1,2 etc.). And there should’nt be
any space in the name of the main program.

Lab task_1 (solve for a function of cube root of unity with fsolve):
 Matlab Program:
1. clc;
2. clearvars;
3. % solve for a function of cube root of unity with fsolve
4. % 1^1/3 = x --> x^3 = 1 --> x^3 - 1 = 0
5. f = @(x) [x^3 - 1];
6. % solve function for x=1
7. f([1]);
8. % Now solve using fsolve
9. fsolve (f, [1])
10. % find the value of x and function(fval)
11. [x, fval] = fsolve (f, [1])
Output:
x = 1

fval = 0

Lab task_2 (Convert a matrix into diagnol matrix without using diag command):
 Matlab Program:
1. clc;
2. clearvars;
3. close all;
4. x = [1,1,1,1;2,2,2,2;3,3,3,3;4,4,4,4]
5. for m = 1:length(x)
6. for n = 1:length(x)
7. if n==m
8. x(n,m)= 1
9. else
10. x(n,m)= 0
11. end
12. end
13. end
Output:
x =

1 0 0 0

0 1 0 0

4
Lab report By: M.Hassan (2020-MC-274) Submitted To: Dr. Arshi Khalid

0 0 1 0

0 0 0 1

You might also like