% HW_01: due July 6th, 2016
% EXCEL EA-1
clear % Delete all variables
clc % Clear the Command Window
close all % Close any open figures
format compact % Remove blank lines in Command Window output
% Follow the instructions in each cell. Lines marked "%..." indicate where
% you should insert code.
% You must add a comment to EVERY LINE OF CODE. This comment can either
% come on the line before the code, or on the same line at the end. For
% example:
r = 1.32; % radius of the cylinder in centimeters
h = 50; % height of the cylinder in millimeters
% Compute the volume of the cylinder in cm^3
cyl_volume = pi*r^2 * (h/10)
% Compute the surface area of the cylinder in cm^2
cyl_area = 2*pi*r^2 + 2*pi*r*(h/10)
%% (a) Matrix creation and indexing
clear; clc
% Create the matrix M given in HW_01_part1.pdf. Use a ';' to suppress output
%... M =
% Individual elements of a matrix can be addressed using the index
% operator. To get the element in the Ith row and Jth column of a matrix
% M, write M(I,J). I and J must be integers (or lists of integers)
% Use index notation to display the number 14 from the matrix M
%...
% Use index notation to add the numbers 14 and 6 from the matrix M
%...
% You can use lists to extract multiple rows and/or columns. For example:
M([1, 2, 3], 5) % first three rows of M, in the 5th column
M(2:4, [1, 3]) % rows 2, 3, and 4, in columns 1 and 3.
% Use index and list notation to display the first three rows of M
%...
% Use index notation to extract the sub-matrix [5, 14; 12, 21]
%...
% The sum() command calculates the sum of all the elements of a column or
% row vector. For example, sum([1, 3, 4]) returns 8.
% Use the sum() command to find the sum of the first row of M
sum_r1 = %...
% Now find the sums of all the other rows and columns
sum_r2 = %...
sum_r3 = %...
sum_r4 = %...
sum_r5 = %...
sum_c1 = %...
sum_c2 = %...
sum_c3 = %...
sum_c4 = %...
sum_r5 = %...
% M is called a "magic square." Mathematicians discovered magic squares
% thousands of years ago. Learn more at: en.wikipedia.org/wiki/Magic_square
%% (b) Gentle introduction to for loops
% a "for loop" is a structure which allows you to execute the same code
% many times, and keep a counter of which repetition you are currently on.
% This loop will run 5 times, with i being equal to 1, 2, 3, 4, then 5.
for i = 1:5
i % display i
end
% Create a for loop to display the first 5 perfect squares (1^2, 2^2,
% 3^2, ...)
for i = %...
%...
end