0% found this document useful (0 votes)
3 views32 pages

Lecture9 Computing

Computing notes for MATLAB

Uploaded by

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

Lecture9 Computing

Computing notes for MATLAB

Uploaded by

g89284612
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Computing and Software Engineering

GET211

Emmanuel Ali
Ayibaemi Ledum Jaafaru Sanusi

December 9, 2024

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 1 / 32
Outline

1 Introduction
2 Introduction to MATLAB
3 Data Type
4 Inputs and Outputs
5 Problem Solving Strategies
6 Data Structures
7 Structured Programming
8 Function
9 Object Oriented Programming
10 Data Visualization
11 Symbolic Math

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 2 / 32
Line Plot

Create a line plot for the function y = sin(x) where x ranges from 0 to
2π.
x = 0 : 0 . 1 : 2 ∗ pi ; % Create a v e c t o r of x v a l u e s
y = sin (x) ; % Compute t h e s i n e o f each x v a l u e
plot (x , y) ; % Create a l i n e p l o t
t i t l e ( ’ S i n e Wave ’ ) ; % Add a t i t l e
x l a b e l ( ’ x ’ ) ; % L a b e l t h e x−a x i s
y l a b e l ( ’ s i n ( x ) ’ ) ; % L a b e l t h e y−a x i s
g r i d on ; % Turn on t h e g r i d

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 3 / 32
Scatter Plot

Generate 200 random points with x and y values drawn from a normal
distribution. Create a scatter plot of these points.
1 x = randn (200 , 1) ; % Generate 200 random x values
2 y = randn (200 , 1) ; % Generate 200 random y values
3 scatter (x , y ) ; % Create a scatter plot
4 title ( ’ Scatter Plot of Random Points ’) ;
5 xlabel ( ’x ’) ;
6 ylabel ( ’y ’) ;
7 grid on ;

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 4 / 32
Bar Plot

Create a bar plot to show the sales data of four products: A, B, C, and
D with sales of 50, 75, 60, and 90 units, respectively.
1 products = categorical ({ ’A ’ , ’B ’ , ’C ’ , ’D ’ }) ;
2 sales = [50 , 75 , 60 , 90];
3 bar ( products , sales ) ;
4 title ( ’ Product Sales ’) ;
5 xlabel ( ’ Products ’) ;
6 ylabel ( ’ Sales ( units ) ’) ;

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 5 / 32
Histogram

Generate a histogram of 1000 data points drawn from a uniform


distribution between 0 and 1.
1 data = rand (1000 , 1) ;
2 histogram ( data ) ;
3 title ( ’ Histogram of Uniformly Distributed Data ’) ;
4 xlabel ( ’ Data Values ’) ;
5 ylabel ( ’ Frequency ’) ;

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 6 / 32
Adding Legends
Create a plot for the functions y1 = sin(x) and y2 = cos(x) on the same graph
with different line styles and colors. Add a legend to distinguish between the
two lines.
1 x = 0:0.1:2* pi ;
2 y1 = sin ( x ) ;
3 y2 = cos ( x ) ;
4 plot (x , y1 , ’ -r ’ , ’ DisplayName ’ , ’ sin ( x ) ’) ; % Red
line for sine
5 hold on ;
6 plot (x , y2 , ’ --b ’ , ’ DisplayName ’ , ’ cos ( x ) ’) ; % Blue
dashed line for cosine
7 hold off ;
8 legend show ;
9 title ( ’ Sine and Cosine Waves ’) ;
10 xlabel ( ’x ’) ;
11 ylabel ( ’ Value ’) ;
12 grid on ;

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 7 / 32
Subplots
Create a 2x1 grid of subplots. In the first subplot, plot y = sin(x). In the
second subplot, plot y = cos(x).
1 x = 0:0.1:2* pi ;
2 y1 = sin ( x ) ;
3 y2 = cos ( x ) ;
4
5 subplot (2 , 1 , 1) ; % Create a 2 x1 grid of plots , and
activate the first plot
6 plot (x , y1 ) ;
7 title ( ’ Sine Wave ’) ;
8 xlabel ( ’x ’) ;
9 ylabel ( ’ sin ( x ) ’) ;
10
11 subplot (2 , 1 , 2) ; % Activate the second plot
12 plot (x , y2 ) ;
13 title ( ’ Cosine Wave ’) ;
14 xlabel ( ’x ’) ;
15 ylabel ( ’ cos ( x ) ’) ;
Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 8 / 32
Annotating Plots
Plot the function y = sin(x) where x ranges from 0 to 2π. Add a text
annotation at x = π/2, y = 1 and draw an arrow pointing to this point.
1 x = 0:0.1:2* pi ;
2 y = sin ( x ) ;
3 plot (x , y ) ;
4 title ( ’ Sine Wave with Annotation ’) ;
5 xlabel ( ’x ’) ;
6 ylabel ( ’ sin ( x ) ’) ;
7 grid on ;
8
9 % Add text annotation
10 text ( pi /2 , 1 , ’ Maximum Point ’ , ’ H or iz onta lA li gnm ent ’ ,
’ right ’) ;
11

12 % Add arrow annotation


13 annotation ( ’ textarrow ’ , [0.3 0.4] , [0.8 0.9] ,
’ String ’ , ’ Peak ’) ;

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 9 / 32
Custom Axis Limits and Ticks

Plot the function y = sin(2x) where x ranges from 0 to 2π. Set the x-axis limits from 0 to 2π and the
y-axis limits from −2 to 2. Customize the x-axis ticks to show every π
2
and the y-axis ticks to show every
unit.

% Define the data


x = 0:0.01:2∗ pi ;
y = s i n (2∗ x ) ;

% Create the p l o t
figure ;
p l o t ( x , y , ’ b ’ , ’ LineWidth ’ , 2 ) ;
t i t l e ( ’ P l o t o f y = s i n ( 2 x ) w i t h Custom A x i s L i m i t s and T i c k s ’ ) ;
xlabel ( ’x ’ ) ;
ylabel ( ’y ’ ) ;
g r i d on ;

% S e t custom a x i s limits
x l i m ( [ 0 2∗ p i ] ) ;
y l i m ([ −2 2 ] ) ;

% S e t custom t i c k s
xticks ( 0 : pi /2:2∗ pi ) ;
yticks ( −2:1:2) ;

% S e t custom t i c k l a b e l s f o r x−a x i s
x t i c k l a b e l s ({ ’ 0 ’ , ’ p i /2 ’ , ’ p i ’ , ’ 3 p i /2 ’ , ’ 2 pi ’ }) ;

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 10 / 32
Changing Line Styles and Colors

1 x = 0:0.1:10;
2 y1 = sin ( x ) ;
3 y2 = cos ( x ) ;
4 plot (x , y1 , ’ -. k ’ , ’ LineWidth ’ , 2) ; % Black dash - dot
line with width 2
5 hold on ;
6 plot (x , y2 , ’: m ’ , ’ LineWidth ’ , 1) ; % Magenta dotted
line with width 1
7 hold off ;
8 title ( ’ Customized Lines ’) ;
9 xlabel ( ’x ’) ;
10 ylabel ( ’ Value ’) ;
11 legend ( ’ sin ( x ) ’ , ’ cos ( x ) ’) ;
12 grid on ;

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 11 / 32
Exporting Plots

1 x = 0:0.1:10;
2 y = sin ( x ) ;
3 plot (x , y ) ;
4 title ( ’ Sine Wave ’) ;
5 xlabel ( ’x ’) ;
6 ylabel ( ’ sin ( x ) ’) ;
7 grid on ;
8
9 % Save the plot as a PNG file
10 saveas ( gcf , ’ sine_wave . png ’) ;
11
12 % Save the plot as a PDF file
13 saveas ( gcf , ’ sine_wave . pdf ’) ;

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 12 / 32
Figure Properties

Create a figure with a white background and name it ’My Figure’. Plot
the function y = exp(−x) for x ranging from 0 to 5.
1 figure ( ’ Name ’ , ’ My Figure ’ , ’ NumberTitle ’ , ’ off ’ ,
’ Color ’ , ’w ’) ;
2 x = 0:0.1:5;
3 y = exp ( - x ) ;
4 plot (x , y ) ;
5 title ( ’ Exponential Decay ’) ;
6 xlabel ( ’x ’) ;
7 ylabel ( ’ exp ( - x ) ’) ;
8 grid on ;

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 13 / 32
3D Plotting

Create a contour plot for the function z = sin(x) · cos(y) over the range
−2π ≤ x, y ≤ 2π.
1 [X , Y ] = meshgrid ( -2* pi :0.1:2* pi , -2* pi :0.1:2* pi ) ;
2 Z = sin ( X ) .* cos ( Y ) ;
3 contour (X , Y , Z ) ; % Create a contour plot
4 title ( ’ Contour Plot ’) ;
5 xlabel ( ’X ’) ;
6 ylabel ( ’Y ’) ;

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 14 / 32
Using Built-in Apps for Data Visualization

MATLAB includes several built-in apps for specific types of data


visualization, such as the Curve Fitting app, the Signal Analyzer app,
and the Image Viewer app. These apps provide graphical interfaces for
performing complex visualization tasks.

curveFittingApp; % Opens the Curve Fitting app


signalAnalyzer; % Opens the Signal Analyzer app
imageTool; % Opens the Image Viewer app

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 15 / 32
Exercise

- Create a plot for the function y = x2 where x ranges from -10 to 10.
- Customize the plot by:
- Adding a grid
- Changing the line color to green
- Setting the line width to 3
- Adding a title, labels, and a legend

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 16 / 32
Symbolic Calculations

MATLAB provides powerful tools for symbolic calculations using the


Symbolic Math Toolbox. Symbolic calculations allow for exact
arithmetic, algebraic manipulation, and calculus operations.

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 17 / 32
Defining Symbolic Variables

To perform symbolic calculations, you first need to define symbolic


variables. This can be done using the syms function.
% Define symbolic v a r i a b l e s
syms x y z

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 18 / 32
Symbolic Differentiation

Differentiate the cubic function f (x) = x3 + 2x2 + x + 1 with respect to


x.
1 % Define a symbolic variable and function
2 syms x
3 f = x ^3 + 2* x ^2 + x + 1;
4

5 % Differentiate the function with respect to x


6 df = diff (f , x ) ;
7 disp ( ’ The derivative of the function is : ’) ;
8 disp ( df ) ;

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 19 / 32
Symbolic Integration

2
Determine the indefinite integral of the Gaussian function f (x) = e−x .
1 % Define a symbolic variable and function
2 syms x
3 f = exp ( - x ^2) ;
4
5 % Integrate the function with respect to x
6 F = int (f , x ) ;
7 disp ( ’ The indefinite integral of the function is : ’) ;
8 disp ( F ) ;

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 20 / 32
Simplifying a Symbolic Expression

x3 −x2 +x−1
Simplify the rational expression x−1 .
1 % Define a symbolic variable and expression
2 syms x
3 expr = ( x ^3 - x ^2 + x - 1) / ( x - 1) ;
4
5 % Simplify the expression
6 simplified_expr = simplify ( expr ) ;
7 disp ( ’ The simplified expression is : ’) ;
8 disp ( simplified_expr ) ;

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 21 / 32
Solving a Quadratic Equation

Symbolic math also enables solving equations algebraically. You can


solve both single-variable and multi-variable equations.
Solve the quadratic equation x2 − 5x + 6 = 0 for x.
1 % Define a symbolic variable and quadratic equation
2 syms x
3 eqn = x ^2 - 5* x + 6 == 0;
4
5 % Solve the equation
6 solutions = solve ( eqn , x ) ;
7 disp ( ’ The solutions of the quadratic equation are : ’) ;
8 disp ( solutions ) ;

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 22 / 32
Solving a System of Linear Equations

Solve the system of linear equations 2x + 3y = 6 and x − 4y = −5 for x


and y.
1 % Define symbolic variables and equations
2 syms x y
3 eqn1 = 2* x + 3* y == 6;
4 eqn2 = x - 4* y == -5;
5
6 % Solve the system of equations
7 solutions = solve ([ eqn1 , eqn2 ] , [x , y ]) ;
8 disp ( ’ The solutions for x and y are : ’) ;
9 disp ( solutions ) ;
10
11 % Extract the solutions
12 sol_x = solutions . x ;
13 sol_y = solutions . y ;

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 23 / 32
Solving a Nonlinear Equation

Solve the cubic nonlinear equation x3 − 3x + 1 = 0.


1 % Define a symbolic variable and equation
2 syms x
3 eqn = x ^3 - 3* x + 1 == 0;
4
5 % Solve the equation
6 solutions = solve ( eqn , x ) ;
7 disp ( ’ The solutions of the nonlinear equation are : ’) ;
8 disp ( solutions ) ;

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 24 / 32
Symbolic Operations Exercise

Given the expression below, determine the differentiation, integration


and simplification of the expression
1 % Define an expression
2 expr = x ^2 + 3* x + 2;
3
4 % Differentiate the expression with respect to x
5

6
7 % Integrate the expression with respect to x
8
9
10 % Simplify the expression

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 25 / 32
Symbolic Taylor Series Expansion

Find the Taylor series expansion of sin(x) around x = 0 up to the fifth


degree.
1 % Define a symbolic variable and function
2 syms x
3 f = sin ( x ) ;
4
5 % Compute the Taylor series expansion around x = 0 up
to the 5 th degree
6 taylor_f = taylor (f , x , ’ Order ’ , 6) ;
7 disp ( ’ The Taylor series expansion of sin ( x ) around x
= 0 is : ’) ;
8 disp ( taylor_f ) ;

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 26 / 32
Symbolic Matrix Operations

Compute the determinant and inverse of a symbolic 2x2 matrix.


1 % Define symbolic variables and a symbolic matrix
2 syms a b c d
3 A = [ a b ; c d ];
4
5 % Compute the determinant and inverse of the matrix
6 det_A = det ( A ) ;
7 inv_A = inv ( A ) ;
8 disp ( ’ The determinant of matrix A is : ’) ;
9 disp ( det_A ) ;
10 disp ( ’ The inverse of matrix A is : ’) ;
11 disp ( inv_A ) ;

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 27 / 32
Solving a Differential Equation

Solve the second-order linear differential equation y ′′ + 5y ′ + 6y = 0.


1 % Define a symbolic function and its derivative
2 syms y ( t )
3 Dy = diff (y , t ) ;
4
5 % Define the differential equation
6 ode = diff (y , t , 2) + 5* Dy + 6* y == 0;
7
8 % Solve the differential equation
9 solution = dsolve ( ode ) ;
10 disp ( ’ The general solution of the differential
equation is : ’) ;
11 disp ( solution ) ;

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 28 / 32
Laplace Transform

Compute the Laplace transform of f (t) = e−2t sin(3t).


1 % Define a symbolic function of time
2 syms t s
3 f = exp ( -2* t ) * sin (3* t ) ;
4
5 % Compute the Laplace transform
6 F = laplace (f , t , s ) ;
7 disp ( ’ The Laplace transform of the function is : ’) ;
8 disp ( F ) ;

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 29 / 32
Exercise

Using symbolic method


1. Define a symbolic expression f (x) = x3 + 3x2 − x − 3.
2. Factorize and simplify the expression.
3. Evaluate f (x) at x = 2.

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 30 / 32
Exercise

Using symbolic method


1. Solve x3 + 3x2 − x − 3 = 0 for x.
2. Solve x2 + y 2 = 25 and x + y = 7 for x and y.

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 31 / 32
Exercise

Using symbolic method


1. Compute the derivative and integral of g(x) = ex sin(x).
2. Evaluate the integral of g(x) from x = 0 to x = π.

Emmanuel Ali, Ayibaemi Ledum, Jaafaru Sanusi 1st Semester December 9, 2024 32 / 32

You might also like