0% found this document useful (0 votes)
17 views

ciea lab 3 by umer

Uploaded by

Umer Afzal
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)
17 views

ciea lab 3 by umer

Uploaded by

Umer Afzal
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/ 7

Computational Intelligence in Engineering

Applications EE-408
Fall 2024 Lab Report
Department of Electrical
Engineering

Additional Details
Obtained Marks
Total Marks
Signature/Comments
Sr. No Student Name
1 Umer Afzal
2 Hashim Ali Mughal
3 Shahzad Ijaz

Section : B Group: 13
Experiment No : 03 Date of Submission: October-17-2024
Experiment Title: Linear Separation of Data into Distinct Classes
Batch : 22-26 Teacher: Dr. Sufi Tabassum Gull
Semester : 05 Lab Engineer: Mr.Bilal Ashraf
Mr.Muhammad Waheed

Department of Electrical Engineering


Contents
Abstract 2

Objectives 2

Equipment 2

Functions to Learn 2

1 Tasks 2
1.1 Task 1.........................................................................................................................................2
1.2 Task 2.........................................................................................................................................3
1.3 Task 3.........................................................................................................................................4
1.4 Task 4.........................................................................................................................................5
1.5 Task 5.........................................................................................................................................5

2 Discussion 6

3 Conclusion 6

1
Abstract
This lab uses various functions of MATLAB that are used to perform array manipulation and plotting.

Objectives
To develop a basic understanding of MATLAB functions to prepare for upcoming labs.

Equipment
• Computer/ Laptop
• MATLAB Software

Functions to Learn
• rand(), randn(), reshape(), ones(), zeros()
• plot(), scatter(), stem()
• xlim(), ylim(), xlabel(), ylabel(), title()
• linewidth, color, shape, grid
• text(), set(), repmat(), length()
• drawnow(), figure, hold on/off

1 Tasks
1.1 Task 1
MATLAB Code:
1 % 1. Write the MATLAB code to generate the vector with elements from 1 to
2 20. row_vector = [1:20]
3
4 % 2. Use the reshape function to reshape this vector into a 4 x5 matrix .
5 matrix_ 4 x5 = reshape ( row_vector , 4 , 5) ;
6
7 % 3. Display the resulting
8 matrix . disp ( matrix_ 4 x5 )

Result:

Figure 1: Task 1 Result

Comments:

2
• Using a semi-colon in-between two numbers ’x’ and ’y’ and assigning the result to a variable would
give us the numbers would give us the numbers from ’x’ to ’y’ eliminating the need to write them
digit by digit, simultaneously allowing us to form a vector by giving the result to any variable.
• The reshape function then allows us to take our vector and convert it into a matrix, the function
takes three arguments, our vector, and the dimensions of the resulting vector.
• The display function allows us to display the vector, this can also be achieved by not writing a semi-
colon at the end of our reshape function, thus directly displaying the output.

1.2 Task 2
MATLAB Code:
1 % 1. Generate a 3 x4 matrix using the randn function
2 . A = randn (3 , 4)
3
4 % 2. Generate a 5 x2 matrix using the rand function
5 . A_uni = rand (5 , 2)
6
7 % 3. Create a 2 x3 matrix of zeros using the zeros function .
8 A_o = zeros (2 , 3)
9
10 % 4. Create a 4 x4 matrix of ones using the ones function .
11 A_one = ones(4 , 4)

Result:

Figure 2: Task 2 Result

Comments:
• The task involved using the rand, randn, ones and zeros function sto create various matrices,
we used the simplest form of the functions in MATLAB, with 2 arguments, to give the desired
results where the arguments just indicated the dimensions of the matrix while the computational
part was carried out by the function itself.

3
• ’rand’ or ’randn’ gave us a matrix with random numbers just one gives it according to a uniform
distribution between 0 and 1, while the other according to a normal distribution with a mean of 0
and standard distribution of 1.
• ’ones’ and ’zeros’ as the name suggests set all the elements to either 1 or 0 as needed.

1.3 Task 3
MATLAB Code:
1
2 x = linspace (0 , 2* pi , 100) ;
3 y1 = sin ( x );
4 y2 = cos( x);
5 figure ;
6 hold on ;
7 plot (x , y1 , ’b - ’ , ’ Line Width ’ , 2) ;
8 plot (x , y2 , ’r -- ’ , ’ Line Width ’ ,
9 2) ; grid on ;
10 xlim ([0 2* pi ]) ;
11 ylim ([ -1.5 1.5]) ;
12 xlabel ( ’ Angle ( radians ) ’ , ’ FontSize ’ , 14) ;
13 ylabel ( ’ Value ’ , ’ FontSize ’ , 14) ;
14 title ( ’ Sine and Cosine Waves ’ , ’ FontSize ’ , 16) ;
15 legend ( ’ Sine Wave ’ , ’ Cosine Wave ’ , ’ Location ’ , ’ northeast ’ , ’ TextColor ’ , ’ blue ’);
16 set ( gca , ’ FontSize ’ , 14) ;

Result:

Figure 3: Task 3 Result

Comments:
• The third task was comparatively simple with the only issue being correctly identifying a vector
to feed to the sine and cosine functions, which we took from an example on the internet. Then
plugging it into the sine and cosine functions, we were able to get the desired outputs.
• Then using functions of ’figure’, ’plot, ’x/ylim’, ’x/ylabel’, ’title’ and ’legend’ we were able to
modify the given plot according to the requirements.
• The thing to note was the order of the use of the functions, if labels or limits were used before plot,
the desired effect would not be displayed on the graph itself.

4
1.4 Task 4
MATLAB Code:
1 % 1. Create a base vector containing some numerical values .
2 base_vector = [1 , 2 , 3]
3 % 2. Replicate this vector to create a larger matrix using repmat ()
4 re pli cate d_m atr ix = repmat ( base_vector , 2 , 5)
5 % 3. Determine the length of the original vector and the dimensions of the
replicated matrix using the length () .
6 original_length = length ( base_vector )
7 matri x_di men sions = size ( re pli cate d_m atr ix )

Result:

Figure 4: Task 4 Result

Comments:
• This task involved the use of ’repmat’, to turn our given vector, into a matrix but the difference
between this task and task 1 was that, the elements would be repeated and as we see, in our
case the original vector had 3 columns and 1 row. ’Repmat’ then took that vector and multiplied
the number of columns and rows, 2*1, 3*5, giving us the resulting matrix. And after every third
column the values start repeating.

1.5 Task 5
MATLAB Code:
1 % Data values for both
2 plots x_stem = 1:10;
3 y_stem = rand (1 , 10) ;
4 1. Create a stem plot for discrete data
5 . figure ;
6 stem ( x_stem , y_stem , ’ filled
7 ’); title ( ’ Stem and Scatter
8 Plot ’); xlabel ( ’X - axis ’);
9 ylabel ( ’Y - axis ’);
10 % 2. Create a scatter plot for random data .
11 x_scatter = rand (1 , 20);
12 y_scatter = rand (1 , 20);
13 % 3. Use hold on to combine both plots on the same figure .
14 hold on ;
15 scatter ( x_scatter , y_scatter , ’ filled ’ , ’r ’);
16 % 4. Use drawnow to update the plot interactively .
17 drawnow ;

Result:

5
Figure 5: Task 5 Result

Comments:
• Task 5 allowed us to use known functions such as ’plot’, ’rand’, etc. while also introducing the
concept of stem and scatter plots.
• stem plots use data and plot it against the specified axis, in our took the x values, from 1-10,
whereas the y values were randomly generated. Then the stem plot was made, in which a line
extended from the x value connecting to the y value highlighted by a circle.
• The only difference between a stem and scatter plot is that there no line connecting the values.

2 Discussion
This lab allowed us to revise concepts of MATLAB that we had used throughout our degree in small
instances but never as a whole while also introducing basic MATLAB functions, showcasing the similarity
between the MATLAB command window to the Terminal application and allowing us to practice and
familiarize ourselves with how to work with vectors, matrices and plotting data which forms the core of
most our course work.

3 Conclusion
In this lab, various MATLAB functions were used to manipulate arrays and visualize data through
plotting. The tasks helped build a foundational understanding of array manipulation, plotting functions,
and MATLAB’s handling of data visualization. These skills will be further built upon in future labs,
where more complex operations and data analysis will be required.

You might also like