diff --git a/matlab_for_beginners/README.md b/matlab_for_beginners/README.md new file mode 100644 index 0000000..b2265a2 --- /dev/null +++ b/matlab_for_beginners/README.md @@ -0,0 +1,10 @@ + + +The best way to learn matlab programming is to go through the basics problems along with the code. +It allows us to understand the matlab language in a better way. + +This, matlab_for_beginners tutorial is divided into different parts. Just go one by one through each part. +After completion of all parts you will have good basic knowledge of matlab. + +All The Best. + diff --git a/matlab_for_beginners/matlab_introduction.md b/matlab_for_beginners/matlab_introduction.md new file mode 100644 index 0000000..edabd9f --- /dev/null +++ b/matlab_for_beginners/matlab_introduction.md @@ -0,0 +1,45 @@ +author - Puneet Prakash Arya +email - puneetarya66@gmail.com +github - https://github.com/puneet-pr-arya + + + + +"MATLAB" is a programming platform designed specifically for engineers and scientists. +The heart of MATLAB is the MATLAB language, a matrix-based language allowing the most natural expression of computational mathematics. + + +What can you do with MATLAB? + +Using MATLAB, you can: + +Analyze data +Develop algorithms +Create models and applications + + +Matlab Advantages + + (.) Implement and test your algorithms easily + (.) Develop the computational codes easily + (.) Debug easily + (.) Use a large database of built in algorithms + (.) Process still images and create simulation videos easily + (.) Symbolic computation can be easily done + (.) Call external libraries + (.) Perform extensive data analysis and visualization + (.) Develop application with graphics user interface + + +Who uses MATLAB? + +Millions of engineers and scientists in industry and academia use MATLAB. +You can use MATLAB for a range of applications, including deep learning and machine learning, +signal processing and communications, image and video processing, control systems, test and measurement, +computational finance, and computational biology. + + +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 +and can work on it in future. + +Thank you and all the best. \ No newline at end of file diff --git a/matlab_for_beginners/part_1(learn_basic_programing)/README.md b/matlab_for_beginners/part_1(learn_basic_programing)/README.md new file mode 100644 index 0000000..a9fabf9 --- /dev/null +++ b/matlab_for_beginners/part_1(learn_basic_programing)/README.md @@ -0,0 +1,21 @@ + + +This part-1 contains basics programs of matlab to make you familiar with the language. +For better understanding the basics of the matlab programming, +please go through the programs according the following +sequence. Details of each the programs is given alongside:-- + +1- add.m ( addition of numbers) +2- equal.m ( The meaning of "a = b" ) +3- math.m ( Basic math operations ) +4- equal_add.m ( The meaning of "a = b", continued ) +5- print.m ( Formatted output ) +6- formatted_output.m ( Formatted output continued ) +7- array.m ( Simple addition of array ) +8- individual_eL_add.m ( Extracting an individual element of an array ) +9- comment.m ( The use of comment) +10- continuation.m ( Continuation to next line ) +11- intr_math_fun.m ( Intrinsic math functions and constants ) +12- nam_var.m ( Naming a variable ) +13- Plot a graph ( for making a quick plot) + diff --git a/matlab_for_beginners/part_1(learn_basic_programing)/add.m b/matlab_for_beginners/part_1(learn_basic_programing)/add.m new file mode 100644 index 0000000..f1d58d0 --- /dev/null +++ b/matlab_for_beginners/part_1(learn_basic_programing)/add.m @@ -0,0 +1,5 @@ +%%Addition +a = 3; +b = 5; +c = a+b +%%Output:8 diff --git a/matlab_for_beginners/part_1(learn_basic_programing)/array.m b/matlab_for_beginners/part_1(learn_basic_programing)/array.m new file mode 100644 index 0000000..a09a1bc --- /dev/null +++ b/matlab_for_beginners/part_1(learn_basic_programing)/array.m @@ -0,0 +1,9 @@ +%%Ex. 7 Arrays + + +a = [3 6 7]; +b = [1 9 4]; +c = a + b + + +%Output: 4 15 11 \ No newline at end of file diff --git a/matlab_for_beginners/part_1(learn_basic_programing)/comment.m b/matlab_for_beginners/part_1(learn_basic_programing)/comment.m new file mode 100644 index 0000000..caeb671 --- /dev/null +++ b/matlab_for_beginners/part_1(learn_basic_programing)/comment.m @@ -0,0 +1,15 @@ +%%Ex. 9 Comment +% +% This program demonstrates how to "comment out" +% a segment of code + +A = 3; +B = A*A; +% +% B = 2*B <--- This statement is not executed +% +C = A+B + + + +%Output: c = 12 \ No newline at end of file diff --git a/matlab_for_beginners/part_1(learn_basic_programing)/continuation.m b/matlab_for_beginners/part_1(learn_basic_programing)/continuation.m new file mode 100644 index 0000000..90d38d9 --- /dev/null +++ b/matlab_for_beginners/part_1(learn_basic_programing)/continuation.m @@ -0,0 +1,7 @@ +%%Ex. 10 Continuation to next line +summation1 = 1 + 3 + 5 + 7 ... + + 9 + 11 + + +%Note: The three periods (...) allow continuation to the next line of commands. The two +% lines in the above example are essentially one line of "summation1 = 1+3+5+7+9+11". \ No newline at end of file diff --git a/matlab_for_beginners/part_1(learn_basic_programing)/equal.m b/matlab_for_beginners/part_1(learn_basic_programing)/equal.m new file mode 100644 index 0000000..d61f3b0 --- /dev/null +++ b/matlab_for_beginners/part_1(learn_basic_programing)/equal.m @@ -0,0 +1,11 @@ +%%Ex. 2 The meaning of "a = b" + + +%In Matlab and in any programming language, the statement "a = b" does not mean +%"a equals b". Instead, it prompts the action of replacing the content of a by the +%content of b. +a = 3; +b = a; +b + +%Output:3 diff --git a/matlab_for_beginners/part_1(learn_basic_programing)/equal_add.m b/matlab_for_beginners/part_1(learn_basic_programing)/equal_add.m new file mode 100644 index 0000000..7dbf39f --- /dev/null +++ b/matlab_for_beginners/part_1(learn_basic_programing)/equal_add.m @@ -0,0 +1,7 @@ +%%Ex. 4 The meaning of "a = b", continued +a = 3; +a = a+1; +a + +%Output:4 + diff --git a/matlab_for_beginners/part_1(learn_basic_programing)/formatted_output.m b/matlab_for_beginners/part_1(learn_basic_programing)/formatted_output.m new file mode 100644 index 0000000..26c34a6 --- /dev/null +++ b/matlab_for_beginners/part_1(learn_basic_programing)/formatted_output.m @@ -0,0 +1,13 @@ +%%Ex. 6 Formatted output +a = 3; +b = a*a; +c = a*a*a; +d = sqrt(a); +fprintf('%4u square equals %4u \r', a, b) +fprintf('%4u cube equals %4u \r', a, c) +fprintf('The square root of %2u is %6.4f \r', a, d) + + +%Output: 3 square equals 9 + % 3 cube equals 27 + % The square root of 3 is 1.7321 \ No newline at end of file diff --git a/matlab_for_beginners/part_1(learn_basic_programing)/individual_eL_add.m b/matlab_for_beginners/part_1(learn_basic_programing)/individual_eL_add.m new file mode 100644 index 0000000..c5f4b86 --- /dev/null +++ b/matlab_for_beginners/part_1(learn_basic_programing)/individual_eL_add.m @@ -0,0 +1,9 @@ +%%Ex. 8 Extracting an individual element of an array + + +a = [3 6 7]; +b = [1 9 4 5]; +c = a(2) + b(4) + + +%Output: c = 11 \ No newline at end of file diff --git a/matlab_for_beginners/part_1(learn_basic_programing)/intr_math_fun.m b/matlab_for_beginners/part_1(learn_basic_programing)/intr_math_fun.m new file mode 100644 index 0000000..fc29b96 --- /dev/null +++ b/matlab_for_beginners/part_1(learn_basic_programing)/intr_math_fun.m @@ -0,0 +1,9 @@ +%%Ex. 11 Intrinsic math functions and constants +x = pi; +y = sin(pi/2) +z = exp(-sin(pi/2)) + + +%Output: + % y = 1 + % z = 0.3679 \ No newline at end of file diff --git a/matlab_for_beginners/part_1(learn_basic_programing)/make_graph.m b/matlab_for_beginners/part_1(learn_basic_programing)/make_graph.m new file mode 100644 index 0000000..ef26300 --- /dev/null +++ b/matlab_for_beginners/part_1(learn_basic_programing)/make_graph.m @@ -0,0 +1,17 @@ +%% Ex. 13 Making a quick plot +x = [0:0.1:20]; +y = sin(x); +plot(x,y) + + + + +% remaks : Remarks: This only serves as a very quick example of what Matlab can do in making +% plots.The first line is equivalent to x = [0 0.1 0.2 0.3 ... 19.8 19.9 20]. It +% assigns the content of x which is an array of 201 elements. The "0:0.1:20" means the +% 201 numbers are evenly spaced. They start from 0 and end at 20 with an increment of +% 0.1. The second line gives the content of the new array, y, as + % y = [sin(x(1)) sin(x(2)) sin(x(3)) ... sin(x(200)) sin(x(201))] , +% or +% y = [sin(0) sin(0.1) sin(0.2) ... sin(19.9) sin(20)] . +% The 3rd line makes a plot of y vs. x. \ No newline at end of file diff --git a/matlab_for_beginners/part_1(learn_basic_programing)/math.m b/matlab_for_beginners/part_1(learn_basic_programing)/math.m new file mode 100644 index 0000000..fcedfc7 --- /dev/null +++ b/matlab_for_beginners/part_1(learn_basic_programing)/math.m @@ -0,0 +1,7 @@ +%%Ex. 3 Basic math operations + +a = 3; +b = 9; +c = 2*a+b^2-a*b+b/a-10 + +%Output:53 diff --git a/matlab_for_beginners/part_1(learn_basic_programing)/nam_var.m b/matlab_for_beginners/part_1(learn_basic_programing)/nam_var.m new file mode 100644 index 0000000..9fc47c5 --- /dev/null +++ b/matlab_for_beginners/part_1(learn_basic_programing)/nam_var.m @@ -0,0 +1,9 @@ +%%Ex. 12 "Clear" a variable + +%Naming a variable + +%(i) Matlab variables are case sensitive. For example, "ASU" and "asu" are two different +%variables. (ii) An underscore (_) or a number (0-9) can also be part of the name of a +%variable. For example, "MAE_384" is a legitimate variable name. (iii) Some names are +%reserved for special constants. For example (see Ex. 11), "pi" is an intrinsic constant +%with a fixed value of 3.14159... \ No newline at end of file diff --git a/matlab_for_beginners/part_1(learn_basic_programing)/print.m b/matlab_for_beginners/part_1(learn_basic_programing)/print.m new file mode 100644 index 0000000..bda8cbe --- /dev/null +++ b/matlab_for_beginners/part_1(learn_basic_programing)/print.m @@ -0,0 +1,4 @@ +%%Ex. 5 Formatted output + +fprintf('Hello') +%Output:Hello diff --git a/matlab_for_beginners/part_2(basic_looping)/README.md b/matlab_for_beginners/part_2(basic_looping)/README.md new file mode 100644 index 0000000..100b9ac --- /dev/null +++ b/matlab_for_beginners/part_2(basic_looping)/README.md @@ -0,0 +1,26 @@ + + +This part-2 introduces the concept of loops in the matlab language. + +For understanding the concept of looping just go through the programs one by one. + After that u will be able to apply the loops. + +1 - program1 - ( Loop: Using for command ) + +2- program2 - ( For loop: Utility of the dummy index ) + + +3 - program3 - ( For loop: More on the dummy index) + +4 - program4 - (Treatment of array within a loop) + +5 - program5 - ( Double loop ) + + +6 - program6 - ( another double lopp ) + + +7- program7 - ( More complicated use of loop and index ) + + +8- wh_loop - ( while loop ) \ No newline at end of file diff --git a/matlab_for_beginners/part_2(basic_looping)/program1.m b/matlab_for_beginners/part_2(basic_looping)/program1.m new file mode 100644 index 0000000..bc3db0f --- /dev/null +++ b/matlab_for_beginners/part_2(basic_looping)/program1.m @@ -0,0 +1,18 @@ +%% The for loop +%Ex. 1 Loop: Using for command +b = 3; +for k = 1:5 + b +end + + +%Output: +% 3 +% 3 +% 3 +% 3 +% 3 + +%Remark: The blue-colored segment in lines 2-4 forms a "for-loop". The statement +% sandwiched between "for k = 1:5" and "end" is repeated 5 times, with the "k" index +% going from 1 to 5 step 1 \ No newline at end of file diff --git a/matlab_for_beginners/part_2(basic_looping)/program2.m b/matlab_for_beginners/part_2(basic_looping)/program2.m new file mode 100644 index 0000000..0bc9277 --- /dev/null +++ b/matlab_for_beginners/part_2(basic_looping)/program2.m @@ -0,0 +1,15 @@ +%% Ex. 2 For loop: Utility of the dummy index + +b = 3; +for k = 1:5 + b^k +end +%Output: +% 3 +% 9 +% 27 +% 81 +% 243 + +%Remark: The outputs are 3^1, 3^2, 3^3, 3^4, and 3^5. the value of "k" keeps changing as +% we go through the loop \ No newline at end of file diff --git a/matlab_for_beginners/part_2(basic_looping)/program3.m b/matlab_for_beginners/part_2(basic_looping)/program3.m new file mode 100644 index 0000000..5926930 --- /dev/null +++ b/matlab_for_beginners/part_2(basic_looping)/program3.m @@ -0,0 +1,10 @@ +%% Ex.3 For loop: More on the dummy index + +sum1 = 0; +for k = 1:9 + sum1 = sum1+k; +end +sum1 +%Output: +% 45 +% Remark: this program performs the summation of 1+2+3+4+5+6+7+8+9 (= 45). \ No newline at end of file diff --git a/matlab_for_beginners/part_2(basic_looping)/program4.m b/matlab_for_beginners/part_2(basic_looping)/program4.m new file mode 100644 index 0000000..ce6198c --- /dev/null +++ b/matlab_for_beginners/part_2(basic_looping)/program4.m @@ -0,0 +1,16 @@ +%% Ex.4 Treatment of array within a loop + + +b = [3 8 9 4 7 5]; +sum1 = 0; +for k = 1:4 + sum1 = sum1+b(k); +end +sum1 + + +%Output: + % 24 + + % Remark: This program performs the summation of sum1 = b(1)+b(2)+b(3)+b(4) = +% 3+8+9+4 = 24 \ No newline at end of file diff --git a/matlab_for_beginners/part_2(basic_looping)/program5.m b/matlab_for_beginners/part_2(basic_looping)/program5.m new file mode 100644 index 0000000..809cf3b --- /dev/null +++ b/matlab_for_beginners/part_2(basic_looping)/program5.m @@ -0,0 +1,15 @@ +%% Ex. 5 Double loop + +sum1 = 0; +for n = 1:2 + for m = 1:3 + sum1 = sum1+n*m; + end +end +sum1 + + +%Output: + 18 +% Remark: this program performs the summation of +% Sum1 = 1*1+1*2+1*3 +2*1+2*2+2*3 = 18 \ No newline at end of file diff --git a/matlab_for_beginners/part_2(basic_looping)/program6.m b/matlab_for_beginners/part_2(basic_looping)/program6.m new file mode 100644 index 0000000..19f40ff --- /dev/null +++ b/matlab_for_beginners/part_2(basic_looping)/program6.m @@ -0,0 +1,16 @@ +%% Ex. 6 Double loop + +for n = 1:2 + for m = 1:3 + fprintf('n = %3u m = %3u \r', n, m) + end +end + + +%Output: +% n = 1 m = 1 +% n = 1 m = 2 +% n = 1 m = 3 +% n = 2 m = 1 +% n = 2 m = 2 +% n = 2 m = 3 \ No newline at end of file diff --git a/matlab_for_beginners/part_2(basic_looping)/program7.m b/matlab_for_beginners/part_2(basic_looping)/program7.m new file mode 100644 index 0000000..6b1986c --- /dev/null +++ b/matlab_for_beginners/part_2(basic_looping)/program7.m @@ -0,0 +1,17 @@ +%% Ex. 7 More complicated use of loop and index + + +b = [2 5 7 4 9 8 3]; +c = [2 3 5 7]; +sum1 = 0; +for k = 1:4 + sum1 = sum1+b(c(k)); +end +sum1 +%Output: +% 24 +% Remark: This program performs the summation of +% sum1 = b(c(1))+b(c(2))+b(c(3))+b(c(4)) +% = b(2)+b(3)+b(5)+b(7) +% = 5+7+9+3 +% = 24 \ No newline at end of file diff --git a/matlab_for_beginners/part_2(basic_looping)/wh_loop.m b/matlab_for_beginners/part_2(basic_looping)/wh_loop.m new file mode 100644 index 0000000..c557f60 --- /dev/null +++ b/matlab_for_beginners/part_2(basic_looping)/wh_loop.m @@ -0,0 +1,16 @@ +%% The while loop + + +x = 3; +while (x < 100) + x = x*3; +end +x + + +% Output: +% x = 243 +% Remark: One can think of a while loop as a combination of a for loop and an if +% statement. Here, the looping will keep going indefinitely as long as the condition, +% (x < 100), is satisfied. Therefore, the value of x progresses from 3, 9, 27, 81, to 243 +% when the loop is terminated. \ No newline at end of file diff --git a/matlab_for_beginners/part_3(basic_branching)/README.md b/matlab_for_beginners/part_3(basic_branching)/README.md new file mode 100644 index 0000000..a4a0e0f --- /dev/null +++ b/matlab_for_beginners/part_3(basic_branching)/README.md @@ -0,0 +1,22 @@ + +This part-3 makes you familiar with the basic branching concept which basically means using +loops,if, if else and basics of matlab. + +Just go through the programs in the given sequence. All required Information about the question is given in the program itself. + + +1 -program1 - (The if command) + + +2- program2 - (if - elseif - else (This example is self-explanatory. Try to change the given value + of num1 and observe the outcome.) + +3- program3- (An application - determine whether a given year is a leap year (try to change the + given value of nyear and observe the outcome) + + +4- program4- (Combine looping and branching) + + + +Thank you... \ No newline at end of file diff --git a/matlab_for_beginners/part_3(basic_branching)/program1.m b/matlab_for_beginners/part_3(basic_branching)/program1.m new file mode 100644 index 0000000..6898df0 --- /dev/null +++ b/matlab_for_beginners/part_3(basic_branching)/program1.m @@ -0,0 +1,19 @@ +%%Ex. 1 The if command + +num1 = 7; +if (num1 > 5) + fprintf('%4u is greater than 5 \r', num1) +else + fprintf('%4u is less than or equal to 5 \r', num1) +end + + +%Output: +% 7 is greater than 5 +% Same program, but change first line to "num1 = 3;" + +%Output: +% 3 is less than or equal to 5 +% Remark: In this program, if (num1 > 5) (num1 is greater than 5) is true, the statement +% "fprintf('%4u is greater than 5 \r', num1)" is executed. Otherwise, the statement +% "fprintf('%4u is less than or equal to 5 \r', num1)" is executed. \ No newline at end of file diff --git a/matlab_for_beginners/part_3(basic_branching)/program2.m b/matlab_for_beginners/part_3(basic_branching)/program2.m new file mode 100644 index 0000000..324dacc --- /dev/null +++ b/matlab_for_beginners/part_3(basic_branching)/program2.m @@ -0,0 +1,17 @@ +%%Ex 2 if - elseif - else (This example is self-explanatory. Try to change the given value +% of num1 and observe the outcome.) + + +num1 = 4; +if (num1 >= 5) + fprintf('%4i is greater than or equal to 5 \r', num1) +elseif (num1 > 1) + fprintf('%4i is less than 5 but greater than 1 \r', num1) +elseif (num1 == 1) + fprintf('%4i equals 1 \r', num1) +elseif (num1 > -3) + fprintf('%4i is less than 1 but greater than -3 \r', num1) +else + fprintf('%4i is less than or equal to -3 \r', num1) +end + diff --git a/matlab_for_beginners/part_3(basic_branching)/program3.m b/matlab_for_beginners/part_3(basic_branching)/program3.m new file mode 100644 index 0000000..30511f0 --- /dev/null +++ b/matlab_for_beginners/part_3(basic_branching)/program3.m @@ -0,0 +1,31 @@ +%% Ex 3 An application - determine whether a given year is a leap year (try to change the +% given value of nyear and observe the outcome) + + +nyear = 1975; +if (mod(nyear, 400) == 0) + fprintf('%6u is a leap year', nyear) +elseif (mod(nyear,4) == 0) & (mod(nyear,100) ~= 0) + fprintf('%6u is a leap year', nyear) +else + fprintf('%6u is not a leap year', nyear) +end + +% Output: + % 1975 is not a leap year +% Remarks: +% (1) In the elseif command (4th line), "&" means "AND". Both statements +% "(mod(nyaer,4) == 0)" and "(mod(nyear,100) ~= 0)" have to be true for Matlab to +% execute the command, "fprintf('%6u is a leap year', nyear)". Also commonly used in an +% if statement is "|" (a vertical line), which means "OR". +% (2) The symbols "~=" in line 4 means "NOT EQUAL TO". There are 6 commonly used +% expressions to compare two numbers in an if command: +% A > B A is greater than B +% A < B A is less than B +% A >= B A is greater than or equal to B +% A <= B A is less than or equal to B +% A == B A equals B +% A ~= B A does not equal B +%(3) The "mod(A,B)" function returns the remainder of A divided by B. For example, +% mod(7,2) = 1, mod(10,4) = 2, and mod(25,5) = 0. If A is divisible by B, mod(A,B) = 0. +% This is a very useful function in many applications related to numerical methods \ No newline at end of file diff --git a/matlab_for_beginners/part_3(basic_branching)/program4.m b/matlab_for_beginners/part_3(basic_branching)/program4.m new file mode 100644 index 0000000..e22eb37 --- /dev/null +++ b/matlab_for_beginners/part_3(basic_branching)/program4.m @@ -0,0 +1,19 @@ +%%Ex 4 Combine looping and branching + +sum1 = 0; +sum2 = 0; +N = 9 +for k = 1:N + sum1 = sum1+k; + if (mod(k,3) == 0) + sum2 = sum2+k; + end +end +sum1 +sum2 + + +% Output: +% sum1 = 45 +% sum2 = 18 +% Remark: Sum1 = 1+2+3+4+5+6+7+8+9, while sum2 = 3+6+9. \ No newline at end of file diff --git a/matlab_for_beginners/part_4(array_nd_matrix)/README.md b/matlab_for_beginners/part_4(array_nd_matrix)/README.md new file mode 100644 index 0000000..fdf6947 --- /dev/null +++ b/matlab_for_beginners/part_4(array_nd_matrix)/README.md @@ -0,0 +1,36 @@ + + + +This part-4 conatains array and matrix related problems. + +All the programs are arranged in the order just to give you the proper understanding of the array and matrix. + +Just Go through the programs.All the program's name and their work is specified inside the program itself. + + +1 - program1 - ( Assign the content of an array/matrix; Basic operations ) + + +2 - program2 - ( Assign the content of a matrix; Addition of two matrices ) + +3 - program3 - ( Multiplication involving a scalar and an array (or a matrix ) + +4 - program4 - ( Element-by-element multiplication involving two 1-D arrays or two matrices of the same dimension ) + +5 - program5 - ( Element-by-element multiplication of two matrices ) + +6 - program6 - ( Direct (not element-by-element) multiplication of two matrices ) + +7 - program7 - ( Elementary functions with a vectorial variable ) + +8 - program8 - ( Another example of elementary functions with a vectorial variable ) + +9 - program9 - ( An efficient way to assign the content of an array ) + +10 - program10 - ( Extracting the individual element(s) of a matrix ) + +11 - program11 - ( Another example for the usage of index for a matrix ) + +12 - program12 - ( Solving a system of linear equation ) + +Thank You... \ No newline at end of file diff --git a/matlab_for_beginners/part_4(array_nd_matrix)/program1.m b/matlab_for_beginners/part_4(array_nd_matrix)/program1.m new file mode 100644 index 0000000..ae056c4 --- /dev/null +++ b/matlab_for_beginners/part_4(array_nd_matrix)/program1.m @@ -0,0 +1,11 @@ +%% 1. Assign the content of an array/matrix; Basic operations + + +%Ex. 1 Assign the content of a (one-dimensional) array; Addition of two arrays + + +a = [2 12 25]; +b = [3 7 4]; +c = a+b +% Output: + % c = 5 19 29 \ No newline at end of file diff --git a/matlab_for_beginners/part_4(array_nd_matrix)/program10.m b/matlab_for_beginners/part_4(array_nd_matrix)/program10.m new file mode 100644 index 0000000..f09d29b --- /dev/null +++ b/matlab_for_beginners/part_4(array_nd_matrix)/program10.m @@ -0,0 +1,10 @@ +%% Ex 10. Extracting the individual element(s) of a matrix + + +A = [3 5; 2 4]; +c = A(2,2)+A(1,2) + +%Output: +% c= 9 +% Remark: With the given A matrix, we have A(1,1) = 3, A(1,2) = 5, A(2,1) = 2, and +% A(2,2) = 4. \ No newline at end of file diff --git a/matlab_for_beginners/part_4(array_nd_matrix)/program11.m b/matlab_for_beginners/part_4(array_nd_matrix)/program11.m new file mode 100644 index 0000000..2a73fc9 --- /dev/null +++ b/matlab_for_beginners/part_4(array_nd_matrix)/program11.m @@ -0,0 +1,16 @@ +%% Ex. 11 Another example for the usage of index for a matrix + + +A = [3 5; 2 4]; +norm1 = 0; +for m = 1:2 +for n = 1:2 + norm1 = norm1+A(m,n)^2; +end +end +norm1 = sqrt(norm1) + + +%Output: +% norm1 = 7.348 +% Remark: This program calculates the Euclidean norm of the A matrix \ No newline at end of file diff --git a/matlab_for_beginners/part_4(array_nd_matrix)/program12.m b/matlab_for_beginners/part_4(array_nd_matrix)/program12.m new file mode 100644 index 0000000..d511457 --- /dev/null +++ b/matlab_for_beginners/part_4(array_nd_matrix)/program12.m @@ -0,0 +1,20 @@ +%% Ex. 12 Solving a system of linear equation + + +A = [4 1 2; 0 3 1; 0 1 2]; +b = [17 ; 19 ; 13]; +x = inv(A)*b + + +%Output: +% x = 1 +% 5 +% 4 + + +% An alternative to Ex. 12 +% A = [4 1 2; 0 3 1; 0 1 2]; +% b = [17 ; 19 ; 13]; +% x = A\b +% The output is the same as Ex. 39. Here, A\b is essentially inv(A)*b. (The "\" is called +% "back divide" in Matlab documentations.) \ No newline at end of file diff --git a/matlab_for_beginners/part_4(array_nd_matrix)/program2.m b/matlab_for_beginners/part_4(array_nd_matrix)/program2.m new file mode 100644 index 0000000..bbfc477 --- /dev/null +++ b/matlab_for_beginners/part_4(array_nd_matrix)/program2.m @@ -0,0 +1,11 @@ +%% Ex. 2 Assign the content of a matrix; Addition of two matrices + +a = [3 4; 1 6]; +b = [5 2; 11 7]; +c = a+b + +%Output: +% c = 8 6 +% 12 13 + + diff --git a/matlab_for_beginners/part_4(array_nd_matrix)/program3.m b/matlab_for_beginners/part_4(array_nd_matrix)/program3.m new file mode 100644 index 0000000..d17b2e1 --- /dev/null +++ b/matlab_for_beginners/part_4(array_nd_matrix)/program3.m @@ -0,0 +1,9 @@ +%% Ex. 3 Multiplication involving a scalar and an array (or a matrix) + +a = [3 5; 1 4]; +b = 2*a + + +%Output: +% b = 6 10 +% 2 8 \ No newline at end of file diff --git a/matlab_for_beginners/part_4(array_nd_matrix)/program4.m b/matlab_for_beginners/part_4(array_nd_matrix)/program4.m new file mode 100644 index 0000000..962ab68 --- /dev/null +++ b/matlab_for_beginners/part_4(array_nd_matrix)/program4.m @@ -0,0 +1,15 @@ +%% Ex.4 Element-by-element multiplication involving two 1-D arrays or two matrices of the same dimension + + +a = [2 3 5]; +b = [2 4 9]; +c = a.*b + + +%Output: +% c = 4 12 45 + +% Remark: The period preceding the mathematical operation, "*", indicates that the +% operation will be performed element-by-element. In this case, the content of c is +% c = [a(1)*b(1) a(2)*b(2) a(3)*b(3)] +% Also, c is automatically assigned as a 1-D array with 3 elements \ No newline at end of file diff --git a/matlab_for_beginners/part_4(array_nd_matrix)/program5.m b/matlab_for_beginners/part_4(array_nd_matrix)/program5.m new file mode 100644 index 0000000..2611be5 --- /dev/null +++ b/matlab_for_beginners/part_4(array_nd_matrix)/program5.m @@ -0,0 +1,10 @@ +%% Ex. 5 Element-by-element multiplication of two matrices + +a = [2 3; 1 4]; +b = [5 1; 7 2]; +c = a.*b + + +%Output: +% c = 10 3 +% 7 8 \ No newline at end of file diff --git a/matlab_for_beginners/part_4(array_nd_matrix)/program6.m b/matlab_for_beginners/part_4(array_nd_matrix)/program6.m new file mode 100644 index 0000000..aa7af0d --- /dev/null +++ b/matlab_for_beginners/part_4(array_nd_matrix)/program6.m @@ -0,0 +1,12 @@ +%% Ex.6 Direct (not element-by-element) multiplication of two matrices + + +a = [2 3; 1 4]; +b = [5 1; 7 2]; +c = a*b +% Output: +% c = 31 8 + % 33 9 + + + %Remark: Observe how the outcome of this example differs from Ex. 32. \ No newline at end of file diff --git a/matlab_for_beginners/part_4(array_nd_matrix)/program7.m b/matlab_for_beginners/part_4(array_nd_matrix)/program7.m new file mode 100644 index 0000000..e2fcdfa --- /dev/null +++ b/matlab_for_beginners/part_4(array_nd_matrix)/program7.m @@ -0,0 +1,9 @@ +%% Ex. 7 Elementary functions with a vectorial variable + +a = [2 3 5]; +b = sin(a) +% Output: +% b = 0.9092 0.1411 -0.9589 + + +%Remark: The content of b is [sin(2) sin(3) sin(5)]. \ No newline at end of file diff --git a/matlab_for_beginners/part_4(array_nd_matrix)/program8.m b/matlab_for_beginners/part_4(array_nd_matrix)/program8.m new file mode 100644 index 0000000..015e34e --- /dev/null +++ b/matlab_for_beginners/part_4(array_nd_matrix)/program8.m @@ -0,0 +1,10 @@ +%% Ex. 8 Another example of elementary functions with a vectorial variable + +a = [2 3 5]; +b = 2*a.^2+3*a+4 + + +%Output: +% b = 18 31 69 +% Remark: The content of b is +% b = [2*(a(1))^2+3*a(1)+4 2*(a(2))^2+3*a(2)+4 2*(a(3))^2+3*a(3)+4]. \ No newline at end of file diff --git a/matlab_for_beginners/part_4(array_nd_matrix)/program9.m b/matlab_for_beginners/part_4(array_nd_matrix)/program9.m new file mode 100644 index 0000000..12c7590 --- /dev/null +++ b/matlab_for_beginners/part_4(array_nd_matrix)/program9.m @@ -0,0 +1,8 @@ +%% Ex. 9 An efficient way to assign the content of an array + + +a = [0:0.5:4]; +a + +%Output: +% a = 0 0.5 1 1.5 2 2.5 3 3.5 4 \ No newline at end of file