Unknown
Unknown
PART 2
ANDREW NORMAN
Last Block
• Variables – names start with a letter, can hold any type of value.
• Simple arithmetic = + - / * ^
• Setting up (initialising) arrays (vectors and matrices)
• Selecting (indexing) parts of arrays.
• Array calculations – “dot multiply” .* etc.
• Transposing with '
• Ranges – start:delta:end or linspace(start,end,points);
• Trig functions (sin, cos, sind, cosd, etc).
• save, load, diary
• Importing data – xlsread, csvread
• Getting help
Exercise – Part One
g = 9.81;
angle = linspace(0, pi/2, 21);
u = 100 * cos (angle);
v = 100 * sin (angle);
impact = 2 * v/g;
distance = u .* impact;
plot(angle, impact)
plot(angle, distance)
Graph for Part Two
Exercise – Part Two
9. Plotting data
10.Review problems (plotting electricity usage and
frequency analysis)
11. MATLAB scripts
12.Logical arrays
13.Programming (if/else, loops)
14.Final project
Matlab Scripts
ax = gca;
ax.XLim = [0 1100];
ax.YLim = [0 350];
ax.XTick = 0:100:1100;
grid
grid minor
Other useful plotting commands
• polyfit(x,y,n)
• Given data in x and y, returns a polynomial of order n,
as a vector.
x = 0:10;
r = randn(size(x));
y = 3 * x + 4 + r;
plot(x,y,'r+');
ax = gca;
ax.YLim = [0 35];
Fitting a line
hold on
fit_poly = polyfit(x,y,1);
disp(fit_poly);
3.0439 3.8173
fitted = polyval(fit_poly, x);
plot(x,fitted,':');
"Programming" - logic
• Logical operators:
• == (comparison) – not to be confused with =
(assignment)
• ~= (not equal to) – some programming languages
use !=
• > < >= <=
• The two things being compared can be arrays, in
which case the result is a logical array (1=true,
0=false).
Logic
• Can't be chained
• x = 4;
•2 < x < 5
• Evaluates 2 < x to be 1 (true) and then 1 < 5 to be
1 (true)
• Returns true, but try with x = 1;
•2 < 1 < 5
• 2 < 1 is 0 (false), 0 < 5 is 1 (true)
• Need to use && (AND) or || (OR):
• (2 < x) && (x < 5)
if / else
if <condition1>
(Do something if condition1 is true)
elseif <condition2>
(Do something if condition2 is true)
elseif <condition3>
(Do something if condition3 is true)
else
(Do something if none of the previous conditions is true)
end
if / else
if <condition>
(Do something if condition is true)
end
• elseif is rare:
if <condition>
(Do something if condition is true)
else
(Do something if condition is false)
end
if / else
if rem(x,2) == 0
disp('Number is even');
if rem(x,3) == 0
disp('Number is divisible by 3');
end
else
disp('Number is odd');
end
Indentation
if rem(x,2) == 0
disp('Number is even');
if rem(x,3) == 0
disp('Number is divisible by 3');
end
else
disp('Number is odd');
end
Back to the if statement:
if rem(x,2) == 0
disp('Number is even');
if rem(x,3) == 0
disp('Number is divisible by 3');
end
else
disp('Number is odd');
end
Not just true or false?
switch x
case 3
disp 'Three'
case 2
disp 'Two'
case 1
disp 'One'
otherwise
disp 'Don''t know!'
end
Repetition
• while
• Repeat while a condition is true
• Formatted printing:
• fprintf(<format string>, <variable list>);
• Example:
• fprintf('The value of x is %d\n', x);
• Prints the value of x in place of %d
• Can have several format specifiers and several variables:
• fprintf('x is %d and y is %d\n', x, y);
• End with \n (newline)
• Strings in MATLAB use single quotes ' (most other programming
languages use double quotes " or allow both).
fprintf
• Formats:
• %d = print an integer (decimal)
• %x = print an integer (hexadecimal)
• %f = print a floating point number
• e.g. 31.6228 (square root of 1000)
• %e = print a floating point number in exponential
format
• e.g. 3.1623e+01
• %.3f = print a floating point number to 3 decimal
places
• e.g. 31.623
• for
• Loops over a range
for x = 1:2:10
fprintf('x is %d\n', x);
end
x is 1
x is 3
x is 5
x is 7
x is 9
For loop - example
% Coordinates of a peak
peakx = 38;
peaky = 72;
surf(A);
Output
Stopping a loop
• break
• Stops the loop and ends it
• continue
• Stops the loop and moves to the next iteration (cycle)