0% found this document useful (0 votes)
36 views9 pages

nUMERICAL 01

The document provides an introduction to MATLAB including its objectives, theory, required software and skills, and examples of MATLAB code to perform common tasks like generating random matrices, creating functions, working with vectors and complex numbers, solving systems of equations, and generating sinusoidal signals.

Uploaded by

john.smith.jc601
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)
36 views9 pages

nUMERICAL 01

The document provides an introduction to MATLAB including its objectives, theory, required software and skills, and examples of MATLAB code to perform common tasks like generating random matrices, creating functions, working with vectors and complex numbers, solving systems of equations, and generating sinusoidal signals.

Uploaded by

john.smith.jc601
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
You are on page 1/ 9

APRIL 29, 2024

INTRODUCTION TO MATLAB
TOUKIR AHMED
20108074
4th year 2nd semester
Objectives
The purpose of this experiment is to gain familiarity with MATLAB and build some basic skills
in MATLAB language. The very basics of MATLAB, i.e. writing codes, computing
mathematical calculations, the concept of arrays and matrices, plotting data, creating functions
etc. are described in this experiment.

Theory
MATLAB is a high-performance language for technical computing. The name MATLAB stands
for MATRIX LABORATORY. In Academic environment, it is the standard instructional tool for
introductory and advanced courses in mathematics, engineering and science. There are some basic
character’s descriptions explained in the table below which are commonly using in MATLAB.
Required software and skills

Required Software: MATLAB Software


Skills: LOOP (for or while), If-else, 1D array, user defined function, symbolic objects
Built in Functions:
QUESTION MATLAB CODE COMMAND WINDOW

1. Find (√1 – x) ^2 clc; The value of (sqrt(1 - x))^2 where


where x = sin(π/5). clear all; x = sin(pi/5) is: 0.41221
% Given x
Store the result in x = sin(pi/5);
y.
% Compute the expression
(sqrt(1 - x))^2
y = (sqrt(1 - x))^2;

% Display the result


disp(['The value of (sqrt(1
- x))^2 where x = sin(pi/5)
is: ', num2str(y)]);

Original Random Matrix:


2. Generate a % Define the size of the 0.6294 -0.8049 -0.6848 -0.7162 0.3115
random numbers matrix 0.8116 -0.4430 0.9412 -0.1565 -0.9286
rows = 5; % Number of rows -0.7460 0.0938 0.9143 0.8315 0.6983
matrix, it will cols = 5; % Number of 0.8268 0.9150 -0.0292 0.5844 0.8680
replace all the 0.2647 0.9298 0.6006 0.9190 0.3575
columns
negative numbers Modified Matrix with Negative Values Replaced by
of that matrix by 0. % Generate a random matrix 0:
0.6294 0 0 0 0.3115
with values between -1 and 1 0.8116 0 0.9412 0 0
random_matrix = 2 * 0 0.0938 0.9143 0.8315 0.6983
rand(rows, cols) - 1; 0.8268 0.9150 0 0.5844 0.8680
0.2647 0.9298 0.6006 0.9190 0.3575
% Display the original
random matrix
disp('Original Random
Matrix:');
disp(random_matrix);

% Replace negative values


with 0
random_matrix(random_matrix
< 0) = 0;

% Display the modified


matrix
disp('Modified Matrix with
Negative Values Replaced by
0:');
disp(random_matrix);
3. Create a clc;
function to clear all;
generate a cosine
frequency = 100; % Hz
wave named duration = 1; % seconds
cosgen. Input
parameters are cos_wave = cosgen(frequency,
desired frequency duration);
and duration of
waveform in
seconds. Function

function waveform =
cosgen(freq, duration)
% Calculate the number
of samples needed
num_samples = duration *
freq;

% Generate the time


vector
t = linspace(0,
duration, num_samples);

% Generate the cosine


wave
waveform = cos(2 * pi *
freq * t);

% Plot the waveform


plot(t, waveform);
xlabel('Time (s)');
ylabel('Amplitude');
title('Cosine
Waveform');
end
4. Create a clc; Sum: 4-2i
function to add clear all; Product: 11-10i
and multiply two
a = 3 + 2i; % Define complex
complex numbers. number a
b = 1 - 4i; % Define complex
number b

[sum_result, product_result]
= add_multiply_complex(a,
b);

disp(['Sum: ',
num2str(sum_result)]);
disp(['Product: ',
num2str(product_result)]);

Function

function [sum_result,
product_result] =
add_multiply_complex(a, b)
% a and b are complex
numbers
% sum_result is the sum
of a and b
% product_result is the
product of a and b

% Add the complex


numbers
sum_result = a + b;

% Multiply the complex


numbers
product_result = a * b;
end
5. Work with ( a ) yy = 0:0.5:10;
vectors.
yy = 0:10; ( b ) yy = 100*ones(1,100);
yy = zeros (1,25);
yy = 1:.25:5; ( c ) yy = 0:0.5:10; % for (a)
(a) How would yy = 100*ones(1,100); % for (b)
you modify one of
the above lines of
MATLAB code to
create a vector that
steps from 0
to 10 in steps of
1/2.
(b) How would
you modify one of
the lines in the
code to create a
vector of one
hundred 100's?

6. Solve this clc; Solution:


system of linear clear all; x = 10.00
equations in y = 3.80
% Define the coefficient
matrix matrix A and the constant z = -9.20
2x − y + z = 7 matrix B
−x + y − z = 3 A = [2, -1, 1; -1, 1, -1; 1,
x + 2y + 3z = −10 2, 3];
B = [7; 3; -10];

% Solve for X
X = A \ B;

% Display the solution


fprintf('Solution:\n');
fprintf('x = %.2f\n', X(1));
fprintf('y = %.2f\n', X(2));
fprintf('z = %.2f\n', X(3));
7. Generate two % Define parameters Solution:
3000 hertz A1 = 13; x = 10.00
A2 = 25; % Using my age as
sinusoids with A2 y = 3.80
different j1_degrees = 43; % Last two z = -9.20
amplitudes and digits of my phone number
phases. j2_degrees = 30;
x1(t) = A1 cos (2π
% Convert degrees to radians
(3000)t + j1) x2(t) j1_radians =
= A2 deg2rad(j1_degrees);
cos(2π(3000)t + j2_radians =
j2) deg2rad(j2_degrees);

(a) Select the value % Frequency (in Hz)


f = 3000;
of the amplitudes
as follows: let A1 % Duration of the signal (in
= 13 and use your seconds)
age for A2. For the duration = 1; % for example,
phases, 1 second
use the last two
% Generate time vector
digits of your t = linspace(0, duration,
telephone number 1000); % 1000 points for
for j1 (in degrees), better resolution
and take j2 =30o.
When doing % Generate sinusoidal
computations in signals
x1 = A1 * cos(2*pi*f*t +
MATLAB, make j1_radians);
sure to convert x2 = A2 * cos(2*pi*f*t +
degrees to radians. j2_radians);

% Plot the signals


figure;
subplot(2,1,1);
plot(t, x1);
title('Sinusoid x1(t)');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(2,1,2);
plot(t, x2);
title('Sinusoid x2(t)');
xlabel('Time (s)');
ylabel('Amplitude');

You might also like