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

Lab 02 MATLAB

The document discusses using MATLAB for numerical computation, data analysis, and visualization. It provides an overview of MATLAB's capabilities and uses, including its interactive programming environment, built-in functions and toolboxes, and powerful visualization tools. The document also provides instructions on using the MATLAB editor window to generate script files, write and run code, debug errors, and create user-defined functions for specific tasks.

Uploaded by

Umer Ashfaq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Lab 02 MATLAB

The document discusses using MATLAB for numerical computation, data analysis, and visualization. It provides an overview of MATLAB's capabilities and uses, including its interactive programming environment, built-in functions and toolboxes, and powerful visualization tools. The document also provides instructions on using the MATLAB editor window to generate script files, write and run code, debug errors, and create user-defined functions for specific tasks.

Uploaded by

Umer Ashfaq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Haider Yaseen MEEN201101082

Lab 02
Objective:
Introduction to MATLAB: using editor window for generating script files developing programs,
& UDFs using conditionals & loops with certain applications. (Part-II)

Theory:
Introduction of MATLAB:
MATLAB is a powerful programming language and environment designed for numerical
computation, data analysis, and visualization. It is widely used in scientific research, engineering,
and mathematics to solve complex problems and develop innovative solutions.
MATLAB's name is derived from "Matrix Laboratory," as it was originally designed to handle
matrix computations. However, it has since become a more comprehensive data analysis and
visualization tool. It provides an interactive environment for programming, making it easy for
users to prototype and test algorithms and create custom graphical user interfaces (GUIs) for their
applications.
MATLAB is based on a high-level programming language that allows users to perform complex
computations with just a few lines of code. It provides a wide range of built-in functions and
toolboxes for specialized applications such as signal processing, control systems, optimization,
and machine learning.
MATLAB's powerful visualization capabilities allow users to create custom plots, charts, and
graphs to represent their data in meaningful ways. It also supports the creation of 2D and 3D
animations and interactive visualizations.
Overall, MATLAB is a versatile tool that enables users to tackle a wide range of computational
and analytical challenges, making it an essential tool for many scientists, engineers, and
mathematicians.
Using Editor window Generating script files:
In MATLAB, the Editor window is used to create and edit script files, which are text files
containing MATLAB code. Script files are useful for developing programs because they allow
users to write and save code for later use, and they can be executed all at once or line-by-line. Here
are some steps for using the Editor window to generate script files and develop programs:

Open the Editor window: To open the Editor window, click on the "New Script" button on the
Home tab in the MATLAB desktop or type "edit" in the Command Window.

1|Page
Haider Yaseen MEEN201101082

Write code: Once the Editor window is open, write the code for your program. This can include
variable assignments, function calls, control structures, and other statements.

Save the file: Once you have written your code, save the script file with a descriptive name and a
".m" file extension. For example, if your program calculates the area of a circle, you might save
the file as "calculate_area.m".

Run the program: To run the program, either click on the green "Run" button in the Editor window
or type the name of the script file in the Command Window. For example, if your script file is
named "calculate_area.m", type "calculate_area" in the Command Window and press Enter.

Debugging: If there are errors in your code, MATLAB will provide error messages in the
Command Window. You can use these messages to identify and fix errors in your code.

Overall, the Editor window is a powerful tool for developing programs in MATLAB. By following
these steps, you can create and run script files to perform a variety of tasks and automate your
workflow.

In MATLAB, user-defined functions (UDFs) are a way to create custom functions for specific
tasks. UDFs can be created using the Editor window and saved as .m files for later use. Here are
some steps for creating and using UDFs:

1) Open the Editor window: To create a UDF, open the Editor window by clicking on the
"New Script" button on the Home tab in the MATLAB desktop or type "edit" in the
Command Window.

2) Define the function: In the Editor window, write the code for your UDF. This includes
defining the function name, input arguments, and output variables.
3) Save the file: Once you have written your UDF, save the script file with the function name
and a ".m" file extension.

4) Call the function: To use the UDF, call the function by typing the function name and
passing in the input arguments. For example, to use the "mySum" function from the
previous example, you would type:

2|Page
Haider Yaseen MEEN201101082

Task 01:
Write a MATLAB script to calculate the sine of an angle, if the angle is greater than 0
degrees.

Code:

% Prompt user for input


angle_degrees = input('Enter an angle in degrees (greater than 0): ');

% Check if the angle is greater than 0


if
angle_degrees > 0

end

% Calculate sine of angle


sine_value = sin(angle_degrees);

% Display result
fprintf('The sine of %g degrees is %g.\n', angle_degrees, sine_value);

Task 02:

3|Page
Haider Yaseen MEEN201101082

Write a MATLAB script to calculate the sine or cosine of an input angle depending on
the selection made on a menu box.

Code:
% Create menu box
choice = menu('Select an option', 'Sine', 'Cosine');

% Prompt user for input angle


angle_degrees = input('Enter an angle in degrees: ');

% Calculate sine or cosine based on user choice


if choice == 1
result = sin(angle_degrees);
function_name = 'sine';
else
result = cos(angle_degrees);
function_name = 'cosine';
end

% Display result
fprintf('The %s of %g degrees is %g.\n', function_name, angle_degrees, result);

Task no 03:
Sine of an angle can be determined using the Macclaurin series as follows:

4|Page
Haider Yaseen MEEN201101082

Write a MATLAB script code to determine the sine of an angle “x” using first 10 terms
given series.

% Prompt user for input angle


X = input('Enter an angle : ');

% Initialize variables
sin_x = 0;
term = x;
n = 1;

% Calculate sine using Maclaurin series up to 10 terms


while n <= 10
sin_x = sin_x + term;
n = n + 2;
term = (-1)^((n-1)/2) * x^n / factorial(n);
end

% Display result
fprintf('The sine of %g degrees is approximately %g.\n', X , sin_x);

Task no 04:

5|Page
Haider Yaseen MEEN201101082

Sine of an angle can be determined using the Macclaurin series as follows:

Write a MATLAB script code to determine the sine of an angle “x” until true error is less
than 10^(-5)

Code:
% Prompt user for input angle
x = input('Enter an angle in degrees: ');

% Initialize variables
sin_x = 0;
prev_sin_x = 0;
term = x;
n = 1;
error = 1;

% Calculate sine using Maclaurin series until true error is less than 10^(-5)
while error > 1e-5
prev_sin_x = sin_x;
sin_x = sin_x + term;
n = n + 2;
term = (-1)^((n-1)/2) * x^n / factorial(n);
error = abs(sin_x - prev_sin_x);
end

% Display result

6|Page
Haider Yaseen MEEN201101082

fprintf('The sine of %g degrees is approximately %g with a true error of %g.\n', x , sin_x, error);

Task no 05:
Generate a MATLAB function for solving a quadratic equation using the independent
variable x as an input variable provided by the user along with the coefficients.
Code:
function [y]=quadratic_Equation (x, a, b, c)
%This function implements the quadratic equation for inputs, x,a,b, and c.
y = a*x. ^2 + b*x + c;
end
% In Command Window
x=5; a=5; b=6; c=1.5;
y = quadratic Equation (x, a, b, c)
y=
156.5000
a = 15: % this changes coefficient a of quadratic equation.
y= quadratic_Equacion (x,a,b,c)
y =
406.5000

Task no 06:
Generate a MATLAB function for solving a cubic equation using the independent variable
x as an input variable provided by the user along with the coefficients.
Code:

function [y] = cubic_Equation (x, coef)


% This function implements the quadratic equation for inputs, x, a, b and c.
y= coef (1) *x^3 + coef (2) * x^2 + coef (3) *x + coef (4) ;
end

7|Page
Haider Yaseen MEEN201101082

Task no 07:
Generate a data file named sensor.dat (for example) with readings obtained from the
experiments conducted in your previous labs (may contain 3-5 variables depending on
your experiment and the prevailing scenario). This file contains data from a number of
different sensors (or measured manually) at different times. Load the data file into
MATLAB. Find maximum of the sensor 1(/variable_1), Mean from the sensor
2(/variable_2), Median of sensor 3 data(/variable_3) and standard deviation from sensor
4(/variable_4).
Code:

clc ; % clear conmand window


clear all ; % clear workspace
load sensor. dat % loading data file into MAILAB.
sensor1 = sensor (: ,1); % Load first column into the sensor1 variable.
sensor2 = sensor (: ,2) ; % Load second column into the sensor2 variable
.
sensor3 = sensor (: ,3); % Load first column into the sensor3 variable
sensor = sensor (:, 4); % Load second column into the sensor4 variable
% Find Maximum of the dataset from sensor 1
Max_sensor = max (sensor1)
% Find mean of the dataset from sensor 2
Mean_sensor2 = mean (sensor2)
% Find median of the dataset from sensor 3
Median_sensor3 = median (sensor3)
% Find standard deviation in dataset form sensor 4
Std_sensor4 = std (sensor4)

8|Page

You might also like