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

Programming Lab

Uploaded by

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

Programming Lab

Uploaded by

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

Programming for Engineers

Module 1: Introduction to Programming Basics


Dr. Gurunadh Velidi
Commercial MATLAB-Modern MATLAB
What we can do on MATLAB
• Application Deployment (3)
• Code Generation (7)
• Computational Biology (2)
• Computational Finance (8)
• Control Systems (8)
• Database Access and Reporting (2)
• Image Processing and Computer Vision (6)
• Math, Statistics, and Optimization (9)
• Parallel Computing (2)
• Signal Processing and Wireless Communications (11)
• Test and Measurement (5)
Course Description

This course provides engineers with a foundation in MATLAB, a


powerful software platform for technical computing and
visualization. Through interactive lectures, hands-on labs, and
real-world engineering applications, students will gain the skills
to:

• Solve engineering problems numerically using


matrices, vectors, and functions.
• Create and analyze data visualizations to understand complex
phenomena.
• Automate repetitive tasks with scripting and programming
techniques.
• Utilize built-in toolboxes for specific engineering disciplines
like control systems, signal processing, and machine learning.
Course Learning Outcomes

• Competently utilize MATLAB's core functionalities:


o Data types, variables, operators, and expressions.
o Matrix and vector operations, including manipulation, indexing, and
slicing.
o Scripting and programming with loops, conditional statements, and
functions.
o Debugging and error handling.
• Effectively solve engineering problems with MATLAB:
o Formulating problems mathematically and translating them into MATLAB
code.
o Solving systems of linear and non-linear equations.
o Performing numerical differentiation and integration.
o Implementing iterative algorithms for optimization and data analysis
• Visualize and analyze engineering data:
o Creating 2D and 3D plots, including bar charts, line graphs, and contour
plots.
o Data interpolation and curve fitting.
o Statistical analysis and hypothesis testing.
o Importing and exporting data from various formats.
• Leverage MATLAB toolboxes for specific engineering disciplines:
o Introduction to relevant toolboxes like Simulink, Control System
Toolbox, Signal Processing Toolbox, and Optimization Toolbox.
o Applying toolbox functionalities to solve real-world engineering problems.
o Understanding the limitations and strengths of each toolbox.
Course Outcomes

CO1 Recall basics of computer programming


CO2 Understand the usage of functions, loops, conditions,
tools in programing
CO3 Understand data structures, visualization, and analysis

CO4 Apply the programming tools and functions to solve


engineering and scientific problems
Prerequisites

• Basic understanding of engineering mathematics (algebra, calculus).


• Familiarity with a programming language (C++, Python, etc.) is helpful but not mandatory.
Course Outline
Module 1: Introduction to Programming Basics [ 2 Theory + 8 Practical hours ]
o Introduction to basics of computer programming and terminologies (variables,
input, output, array, vector, strings, functions, command, syntax, algorithm etc.)
o Getting started with MATLAB interface and workspace.
o Data types, variables, operators, and expressions.
o Scripting and debugging techniques.
o Matrix operations, indexing, and slicing.
o Vector calculus and applications.
Module 2: Functions and Programming Paradigms [2 Theory + 8 Practical hours]
o Defining and using user-defined functions.
o Loops, conditional statements, and control flow.
o Modular programming and code organization.

Module 3: Numerical Methods for Engineers [ 3 Theory + 12 Practical hours]


o Linear algebra fundamentals for solving engineering problems.
o Root finding algorithms (Iterative schemes)
o Numerical differentiation and integration (Simple ones)
o Solving systems of linear and non-linear equations.
Module 4: Data Structures, Visualization and Analysis [ 3 Theory + 12 Practical hours]
o Basics of Data Structures (Arrays and Array lists, recursion, searching and sorting
algorithm)
o Creating 2D and 3D plots for various data types.
o Data analysis and statistics with MATLAB tools.
o Importing and exporting data from various formats (electrical signals, acoustic, string and
numerical data)
Module 5: Introduction to Engineering Toolboxes [ 3 Theory + 12 Practical
hours]
o Simulink for modelling and simulation.
o Statistics and Machine Learning Toolbox (Statistics and Machine Learning
Toolbox) (Focus on Statistics)
o Curve Fitting Toolbox (Curve Fitting Toolbox) (General)
o Signal Processing Toolbox (Signal Processing Toolbox) (Electrical,
Mechanical)
o Image Processing Toolbox (Image Processing Toolbox) (Mechatronics,
Mechanical, Computers)
Module 6: Project-Based Learning (Cluster-specific teaching) [ 2 Theory + 8 Practical
hours ]
o Applying acquired MATLAB skills to solve a real-world engineering problem.
o Developing a comprehensive project report and presentation.
Programming for Engineers
Module – 1 || Lecture - 1

Introduction to Programming Basics


Basic MATLAB Program
Lets Do Together arithmetic calculations.
Addition of Explanation:
1.Input Prompt:
Two numbers •input('Enter the first number: ') prompts the user to enter the first
number.
•input('Enter the second number: ') prompts the user to enter the
% MATLAB Program to Add Two Numbers second number.
2.Addition:
% Prompt the user to enter the first number •The sum of number1 and number2 is calculated using the +
operator.
number1 = input('Enter the first number: ');
3.Output:
•fprintf is used to format and display the result. The %.2f specifies
% Prompt the user to enter the second number that the numbers should be displayed with two decimal places.
number2 = input('Enter the second number: '); To run this program, you can copy and paste it into the MATLAB
Command Window or save it as a script (e.g., add_two_numbers.m) and
% Calculate the sum of the two numbers run the script.
sum = number1 + number2;

% Display the result


fprintf('The sum of %.2f and %.2f is %.2f\n', number1,
number2, sum);
Tasks
• Write a MATLAB script to add two numbers provided by the user and display
the result.
• Write a MATLAB script to subtract the second number from the first number
and display the result.
• Write a MATLAB script to multiply two numbers provided by the user and
display the result
• Write a MATLAB script to divide the first number by the second number and
display the result. Ensure to handle division by zero.
• Write a MATLAB script to calculate the square of a number provided by the
user and display the result.
• Write a MATLAB script to calculate the cube of a number provided by the
user and display the result.
Simple Scripting-Commands
Arithmetic Operations
% Exponentiation
base = 2;
exponent = 3;
result = base^exponent;
fprintf('%d raised to the power of %d is %d\n', base, exponent, result);

base = 2; exponent = 3;: Assigns 2 and 3 to base and exponent.


result = base^exponent;: The ^ operator raises base to the power of exponent.
fprintf(...);: Prints the result of exponentiation.
Modulus Operation
% Modulus Operation
a = 13;
b = 5;
remainder = mod(a, b);
fprintf('The remainder when %d is divided by %d is %d\n', a, b, remainder);

a = 13; b = 5;: Assigns values 13 and 5 to a and b.


remainder = mod(a, b);: The mod function returns the remainder when a is divided by b.
fprintf(...);: Displays the remainder of the division.
% Square Root Calculation
number = 16;
sqrt_result = sqrt(number);
fprintf('The square root of %d is %f\n', number, sqrt_result);

number = 16;:
Assigns the value 16 to number.
sqrt_result = sqrt(number);:
The sqrt function calculates the square root of number.
fprintf(...);:
Outputs the square root of the number. %f is used for the floating-point result.
% Natural Logarithm
number = 10;
log_result = log(number);
fprintf('The natural logarithm of %d is %f\n', number, log_result);

•number = 10;: Assigns the value 10 to number.


•log_result = log(number);: The log function calculates the natural logarithm (base e) of number.
•fprintf(...);: Displays the result of the logarithm calculation.
% Trigonometric Functions •angle_degrees = 45;: Assigns 45 degrees to angle_degrees.
angle_degrees = 45; •angle_radians = deg2rad(angle_degrees);: Converts
angle_radians = deg2rad(angle_degrees); the angle from degrees to radians using deg2rad.
•sin_value = sin(angle_radians);: Calculates the sine of
sin_value = sin(angle_radians); the angle.
cos_value = cos(angle_radians); •cos_value = cos(angle_radians);: Calculates the cosine
tan_value = tan(angle_radians); of the angle.
•tan_value = tan(angle_radians);: Calculates the tangent
fprintf('sin(%d) = %f\n', angle_degrees, sin_value); of the angle.
fprintf('cos(%d) = %f\n', angle_degrees, cos_value); •fprintf(...);: Prints the results of the trigonometric
fprintf('tan(%d) = %f\n', angle_degrees, tan_value); calculations.
Compute the Following
% Calculate the numerator
numerator = 2^5;
numerator = 2^5;: This calculates 252 5 and stores it in the
% Calculate the denominator variable numerator.
denominator = (2^5) - 1;
denominator = (2^5) - 1;: This calculates (25)−1(2 5 )−1 and
% Calculate the result of the expression stores it in the variable denominator.
result = numerator / denominator;

% Display the result result = numerator / denominator;: This divides the numerator
fprintf('The result of 2^5/((2^5) - 1) is %f\n', result); by the denominator and stores the result

.fprintf(...);: This prints the result of the expression.


% Calculate the first expression: 2^5/((2^5) - 1)
numerator = 2^5;
denominator = (2^5) - 1;
result1 = numerator / denominator;

% Calculate the second expression: (1 - (1/2^5))^-1


result2 = (1 - (1/2^5))^-1;

% Display the results


fprintf('The result of 2^5/((2^5) - 1) is %f\n', result1);
fprintf('The result of (1 - (1/2^5))^-1 is %f\n', result2);

% Check if the results are equal


if abs(result1 - result2) < 1e-10
fprintf('The two results are equal.\n');
else
fprintf('The two results are not equal.\n');
end
Basic Program 1.TASK
Add section breaks to divide the live script into three
blocks thatLoad the data.
Projectile Motion 2.Extract the x and y data.
3.Plot the data.

load balldata.mat
time = balldata(:,1)
height = balldata(:,2)
plot(time, height)
yline(0);
xlabel('Time (s)')
ylabel('Height (m)')
Thank You

You might also like