Skip to content

Commit 6cfc103

Browse files
Added matlab_for_beginners
1 parent e051eac commit 6cfc103

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+632
-0
lines changed

matlab_for_beginners/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
3+
The best way to learn matlab programming is to go through the basics problems along with the code.
4+
It allows us to understand the matlab language in a better way.
5+
6+
This, matlab_for_beginners tutorial is divided into different parts. Just go one by one through each part.
7+
After completion of all parts you will have good basic knowledge of matlab.
8+
9+
All The Best.
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
author - Puneet Prakash Arya
2+
3+
github - https://github.com/puneet-pr-arya
4+
5+
6+
7+
8+
"MATLAB" is a programming platform designed specifically for engineers and scientists.
9+
The heart of MATLAB is the MATLAB language, a matrix-based language allowing the most natural expression of computational mathematics.
10+
11+
12+
What can you do with MATLAB?
13+
14+
Using MATLAB, you can:
15+
16+
Analyze data
17+
Develop algorithms
18+
Create models and applications
19+
20+
21+
Matlab Advantages
22+
23+
(.) Implement and test your algorithms easily
24+
(.) Develop the computational codes easily
25+
(.) Debug easily
26+
(.) Use a large database of built in algorithms
27+
(.) Process still images and create simulation videos easily
28+
(.) Symbolic computation can be easily done
29+
(.) Call external libraries
30+
(.) Perform extensive data analysis and visualization
31+
(.) Develop application with graphics user interface
32+
33+
34+
Who uses MATLAB?
35+
36+
Millions of engineers and scientists in industry and academia use MATLAB.
37+
You can use MATLAB for a range of applications, including deep learning and machine learning,
38+
signal processing and communications, image and video processing, control systems, test and measurement,
39+
computational finance, and computational biology.
40+
41+
42+
With these tutorials, i would like to give all of you a basic idea of matlab so that all of you got familiar with the platform
43+
and can work on it in future.
44+
45+
Thank you and all the best.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
3+
This part-1 contains basics programs of matlab to make you familiar with the language.
4+
For better understanding the basics of the matlab programming,
5+
please go through the programs according the following
6+
sequence. Details of each the programs is given alongside:--
7+
8+
1- add.m ( addition of numbers)
9+
2- equal.m ( The meaning of "a = b" )
10+
3- math.m ( Basic math operations )
11+
4- equal_add.m ( The meaning of "a = b", continued )
12+
5- print.m ( Formatted output )
13+
6- formatted_output.m ( Formatted output continued )
14+
7- array.m ( Simple addition of array )
15+
8- individual_eL_add.m ( Extracting an individual element of an array )
16+
9- comment.m ( The use of comment)
17+
10- continuation.m ( Continuation to next line )
18+
11- intr_math_fun.m ( Intrinsic math functions and constants )
19+
12- nam_var.m ( Naming a variable )
20+
13- Plot a graph ( for making a quick plot)
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
%%Addition
2+
a = 3;
3+
b = 5;
4+
c = a+b
5+
%%Output:8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
%%Ex. 7 Arrays
2+
3+
4+
a = [3 6 7];
5+
b = [1 9 4];
6+
c = a + b
7+
8+
9+
%Output: 4 15 11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
%%Ex. 9 Comment
2+
%
3+
% This program demonstrates how to "comment out"
4+
% a segment of code
5+
6+
A = 3;
7+
B = A*A;
8+
%
9+
% B = 2*B <--- This statement is not executed
10+
%
11+
C = A+B
12+
13+
14+
15+
%Output: c = 12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
%%Ex. 10 Continuation to next line
2+
summation1 = 1 + 3 + 5 + 7 ...
3+
+ 9 + 11
4+
5+
6+
%Note: The three periods (...) allow continuation to the next line of commands. The two
7+
% lines in the above example are essentially one line of "summation1 = 1+3+5+7+9+11".
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
%%Ex. 2 The meaning of "a = b"
2+
3+
4+
%In Matlab and in any programming language, the statement "a = b" does not mean
5+
%"a equals b". Instead, it prompts the action of replacing the content of a by the
6+
%content of b.
7+
a = 3;
8+
b = a;
9+
b
10+
11+
%Output:3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
%%Ex. 4 The meaning of "a = b", continued
2+
a = 3;
3+
a = a+1;
4+
a
5+
6+
%Output:4
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
%%Ex. 6 Formatted output
2+
a = 3;
3+
b = a*a;
4+
c = a*a*a;
5+
d = sqrt(a);
6+
fprintf('%4u square equals %4u \r', a, b)
7+
fprintf('%4u cube equals %4u \r', a, c)
8+
fprintf('The square root of %2u is %6.4f \r', a, d)
9+
10+
11+
%Output: 3 square equals 9
12+
% 3 cube equals 27
13+
% The square root of 3 is 1.7321
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
%%Ex. 8 Extracting an individual element of an array
2+
3+
4+
a = [3 6 7];
5+
b = [1 9 4 5];
6+
c = a(2) + b(4)
7+
8+
9+
%Output: c = 11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
%%Ex. 11 Intrinsic math functions and constants
2+
x = pi;
3+
y = sin(pi/2)
4+
z = exp(-sin(pi/2))
5+
6+
7+
%Output:
8+
% y = 1
9+
% z = 0.3679
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
%% Ex. 13 Making a quick plot
2+
x = [0:0.1:20];
3+
y = sin(x);
4+
plot(x,y)
5+
6+
7+
8+
9+
% remaks : Remarks: This only serves as a very quick example of what Matlab can do in making
10+
% plots.The first line is equivalent to x = [0 0.1 0.2 0.3 ... 19.8 19.9 20]. It
11+
% assigns the content of x which is an array of 201 elements. The "0:0.1:20" means the
12+
% 201 numbers are evenly spaced. They start from 0 and end at 20 with an increment of
13+
% 0.1. The second line gives the content of the new array, y, as
14+
% y = [sin(x(1)) sin(x(2)) sin(x(3)) ... sin(x(200)) sin(x(201))] ,
15+
% or
16+
% y = [sin(0) sin(0.1) sin(0.2) ... sin(19.9) sin(20)] .
17+
% The 3rd line makes a plot of y vs. x.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
%%Ex. 3 Basic math operations
2+
3+
a = 3;
4+
b = 9;
5+
c = 2*a+b^2-a*b+b/a-10
6+
7+
%Output:53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
%%Ex. 12 "Clear" a variable
2+
3+
%Naming a variable
4+
5+
%(i) Matlab variables are case sensitive. For example, "ASU" and "asu" are two different
6+
%variables. (ii) An underscore (_) or a number (0-9) can also be part of the name of a
7+
%variable. For example, "MAE_384" is a legitimate variable name. (iii) Some names are
8+
%reserved for special constants. For example (see Ex. 11), "pi" is an intrinsic constant
9+
%with a fixed value of 3.14159...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
%%Ex. 5 Formatted output
2+
3+
fprintf('Hello')
4+
%Output:Hello
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
3+
This part-2 introduces the concept of loops in the matlab language.
4+
5+
For understanding the concept of looping just go through the programs one by one.
6+
After that u will be able to apply the loops.
7+
8+
1 - program1 - ( Loop: Using for command )
9+
10+
2- program2 - ( For loop: Utility of the dummy index )
11+
12+
13+
3 - program3 - ( For loop: More on the dummy index)
14+
15+
4 - program4 - (Treatment of array within a loop)
16+
17+
5 - program5 - ( Double loop )
18+
19+
20+
6 - program6 - ( another double lopp )
21+
22+
23+
7- program7 - ( More complicated use of loop and index )
24+
25+
26+
8- wh_loop - ( while loop )
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
%% The for loop
2+
%Ex. 1 Loop: Using for command
3+
b = 3;
4+
for k = 1:5
5+
b
6+
end
7+
8+
9+
%Output:
10+
% 3
11+
% 3
12+
% 3
13+
% 3
14+
% 3
15+
16+
%Remark: The blue-colored segment in lines 2-4 forms a "for-loop". The statement
17+
% sandwiched between "for k = 1:5" and "end" is repeated 5 times, with the "k" index
18+
% going from 1 to 5 step 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
%% Ex. 2 For loop: Utility of the dummy index
2+
3+
b = 3;
4+
for k = 1:5
5+
b^k
6+
end
7+
%Output:
8+
% 3
9+
% 9
10+
% 27
11+
% 81
12+
% 243
13+
14+
%Remark: The outputs are 3^1, 3^2, 3^3, 3^4, and 3^5. the value of "k" keeps changing as
15+
% we go through the loop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
%% Ex.3 For loop: More on the dummy index
2+
3+
sum1 = 0;
4+
for k = 1:9
5+
sum1 = sum1+k;
6+
end
7+
sum1
8+
%Output:
9+
% 45
10+
% Remark: this program performs the summation of 1+2+3+4+5+6+7+8+9 (= 45).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
%% Ex.4 Treatment of array within a loop
2+
3+
4+
b = [3 8 9 4 7 5];
5+
sum1 = 0;
6+
for k = 1:4
7+
sum1 = sum1+b(k);
8+
end
9+
sum1
10+
11+
12+
%Output:
13+
% 24
14+
15+
% Remark: This program performs the summation of sum1 = b(1)+b(2)+b(3)+b(4) =
16+
% 3+8+9+4 = 24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
%% Ex. 5 Double loop
2+
3+
sum1 = 0;
4+
for n = 1:2
5+
for m = 1:3
6+
sum1 = sum1+n*m;
7+
end
8+
end
9+
sum1
10+
11+
12+
%Output:
13+
18
14+
% Remark: this program performs the summation of
15+
% Sum1 = 1*1+1*2+1*3 +2*1+2*2+2*3 = 18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
%% Ex. 6 Double loop
2+
3+
for n = 1:2
4+
for m = 1:3
5+
fprintf('n = %3u m = %3u \r', n, m)
6+
end
7+
end
8+
9+
10+
%Output:
11+
% n = 1 m = 1
12+
% n = 1 m = 2
13+
% n = 1 m = 3
14+
% n = 2 m = 1
15+
% n = 2 m = 2
16+
% n = 2 m = 3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
%% Ex. 7 More complicated use of loop and index
2+
3+
4+
b = [2 5 7 4 9 8 3];
5+
c = [2 3 5 7];
6+
sum1 = 0;
7+
for k = 1:4
8+
sum1 = sum1+b(c(k));
9+
end
10+
sum1
11+
%Output:
12+
% 24
13+
% Remark: This program performs the summation of
14+
% sum1 = b(c(1))+b(c(2))+b(c(3))+b(c(4))
15+
% = b(2)+b(3)+b(5)+b(7)
16+
% = 5+7+9+3
17+
% = 24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
%% The while loop
2+
3+
4+
x = 3;
5+
while (x < 100)
6+
x = x*3;
7+
end
8+
x
9+
10+
11+
% Output:
12+
% x = 243
13+
% Remark: One can think of a while loop as a combination of a for loop and an if
14+
% statement. Here, the looping will keep going indefinitely as long as the condition,
15+
% (x < 100), is satisfied. Therefore, the value of x progresses from 3, 9, 27, 81, to 243
16+
% when the loop is terminated.

0 commit comments

Comments
 (0)