09 18 2020 - Mathlab Intro 2
09 18 2020 - Mathlab Intro 2
Doosun Kang
Kyung Hee University
Water Resources Systems Engineering (Wk3) 2
2-D Plotting
Water Resources Systems Engineering (Wk3) 3
2-dimensional plot
Y axis label
Text label
Line
Marker
X axis label
Water Resources Systems Engineering (Wk3) 4
>> y = sin(x);
Black k Pentagon p
>> plot(x, y, '--r∗ ′, 'linewidth', 2, 'markersize', 12)
White w Hexagon h
Water Resources Systems Engineering (Wk3) 5
>> x = [1988:1:1994];
>> y = [8 12 20 22 18 24 27];
Try other
>> plot(x, y, '--r∗ ′, 'linewidth', 2, 'markersize', 12)
properties
Water Resources Systems Engineering (Wk3) 6
>> x = [-2:0.01:4];
>> y1 = 3∗x.^3-26∗x+6;
>> y2 = 9∗x.^2-26;
>> y3 = 18∗x;
x,y2
Water Resources Systems Engineering (Wk3) 7
Loading Data
&
Creating Plots
Water Resources Systems Engineering (Wk3) 9
Load Leaf-River Data:
Water Resources Systems Engineering (Wk3) 10
P as Bar Plot
ylabel('PP (mm)');
title('Leaf River Daily Precipitation Data (WY 1948)');
P as Bar
Plot
ET (mm)
subplot(4,1,3);
area(Period,Flow,'edgecolor','k', …
'facecolor',0.95*[0.6 0.9 1]); hold on;
Creates an area plot (blue area) in the subplot area
plot(Period, Flow,'ko','linewidth',1.5, …
'markersize',4); hold off;
Creates a marker plot (black circles) in the subplot area
ylabel('Q (cms)');
legend('Q as o-symbol','Q as area plot',0);
Q as o-symbol
Q as area plot
Q (cms)
Q (cms)
Time (Days)
Figure(1) … Subplot(4,1,4) … Semilogy Line plot … 14
Water Resources Systems Engineering (Wk3) 14
subplot(4,1,4);
semilogy(Period, Flow,'k-',Period, Flow,'r.', …
'linewidth',1,'markersize',10);
Creates a semilog line plot (black line) in the subplot
area with a log-scale y-axis
Time (Days)
Final Plot: 15
Water Resources Systems Engineering (Wk3) 15
P as Bar
Plot
ET (mm)
Q as o-symbol
Q as area plot
Q (cms)
Q (cms)
Time (Days)
Another Simple Example - Scatterplot:
Water Resources Systems Engineering (Wk3) 16
figure(2);
plot(Precip,Flow,'b.','markersize',15);
axis('square');
xlabel('Precip (mm)');
ylabel('Flow (cms)');
title('Scatterplot Leaf River Data(WY 1948)');
legend('Data as dot-markers',0);
grid on;
Other types of Plots:
Water Resources Systems Engineering (Wk3) 17
0.9
0.8
0.7
0.6
kq
0.5
0.4
0.3
0.2
0.1
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
alpha
Contour plots
Mesh plots (3-D)
Surface plots (3-D)
Histograms
… etc …
Water Resources Systems Engineering (Wk3) 18
Assignment #2
Use the ‘demo’, ‘help’ and ‘doc’ features of Matlab to find the
kinds of plots you want, and to learn the correct syntax.
MATLAB Scripts
Water Resources Systems Engineering (Wk3) 21
>> edit;
>> edit;
>> ExampleScript
Example – “Scatterplot.m”
Water Resources Systems Engineering (Wk3) 23
clear all;
close all;
load LeafCatch.dat;
figure(1)
plot(Precip,Flow,'b.','markersize',15);
axis('square');
xlabel('Precip (mm)');
ylabel('Flow (cms)');
title('Scatterplot Leaf River Data(WY 1948)');
legend('Data as dot-markers',0);
grid on;
Water Resources Systems Engineering (Wk3) 24
%Initialization:
clear all, close all, clc(optional)
Loading data files if you have to
%Calculations:
Command structure for all calculations
M-file operation
ScriptTest1.m
% Water Resources System Engineering
game1 = 75;
game2 = 93;
game3 = 68;
Command window
>> ScriptTest1
>> ave_points
ave_points =
78.6667
Water Resources Systems Engineering (Wk3) 26
ScriptTest2.m
% Water Resources System Engineering
game1 = input(‘1st game score =’);
game2 = input(‘2nd game score =’);
game3 = input(‘3rd game score =’);
ave_points = (game1 + game2 + game3)/3;
Command window
>> ScriptTest2.m
1st game score = 67 User type-in
2nd game score = 91
3rd game score = 70
>> ave_points
ave_points =
78.6667
Water Resources Systems Engineering (Wk3) 28
ScriptTest3.m
% Water Resources System Engineering
game1 = input(‘1st game score =’);
game2 = input(‘2nd game score =’);
game3 = input(‘3rd game score =’);
ave_points = (game1 + game2 + game3)/3; Command window
disp(‘ ’); >> ScriptTest3
tableYP(:,1) = yr’;
YEAR POPULATION
tableYP(:,2) = pop’;
(MILLIONS)
disp(‘ YEAR POPULATION’)
2007 127
disp(‘ (MILLIONS)’ )
2008 130
disp(‘ ’) 2009 136
disp(tableYP) 2010 145
2011 158
2012 178
2013 211
Water Resources Systems Engineering (Wk3) 30
“input” function
Variable_name = input(ʹUser input textʹ)
“disp” function
disp(‘User input text') or disp(variable_name)
“load” function
load file_name or load('file_name')
TestData.txt
Save to “var”
Save to “TestData”
3
Water Resources Systems Engineering (Wk3) 4
“fprintf” function
1 2 3
file open Write file close
“fopen” “fprintf” “fclose”
1
fid = fopen('file_name', ‘w’) Testfprintf.m
file id 'w' = write x = 1:10;
y = [x; exp(x)];
2
fprintf(fid, ‘writing_format', value) 1 fid = fopen('exp.txt', 'w');
2 fprintf(fid, '%5i%10.2f\n', y);
format is important
3 fclose(fid);
3
fclose(fid)
3
Water Resources Systems Engineering (Wk3) 5
fclose(fid); Integer
exp.txt
fprintf(fid, '%5i%10.2f\n', y);
Field width
Precision
Floating-point
number
Water Resources Systems Engineering (Wk3) 36
“xlswrite” function
xlswrite(filename, variable_name, sheet, range) test2.xls
clear all
tic; clear all
for i=1:100000 tic;
b1(i)=i^2; vs. b2(1:100000)=[1:100000].^2;
end time2=toc;
time1=toc;
➔ Same results
but, computation times are very different
4
Water Resources Systems Engineering (Wk3) 2
i = First
i = i + 1
b(i) = i^2
end Y i == Last ? N
Water Resources Systems Engineering (Wk3) 43
if expression1
% run if expression1 is True otherwise skip
elseif expression2
% run if expression2 is True otherwise skip
elseif expression3
% run if expression3 is True otherwise skip
elseif expression4
% run if expression4 is True otherwise skip
else
% run if none of the above expressions is True
end
Water Resources Systems Engineering (Wk3) 47
end A=2
A=1 end
Water Resources Systems Engineering (Wk3) 48
Function M-files
5
Water Resources Systems Engineering (Wk3) 1
x=2
Input value
Function
y=4
5
Water Resources Systems Engineering (Wk3) 2
function m-file
a) the very first word is "function"
b) defines the function name
c) lists the inputs to the function
d) lists the outputs created by the function
The word "function“ appearing in the function definition line, must always
be typed in lower case.
5
Water Resources Systems Engineering (Wk3) 3
O1
I1
calc(.) O2
I2 O3
Notes:
a) The orders of the input and output variables are
important
5
Water Resources Systems Engineering (Wk3) 5
“function” example
Main.m
5
Water Resources Systems Engineering (Wk3) 6
Program structures
main f1 f2
f3 f1 f2
f2
f1 f2
5
Water Resources Systems Engineering (Wk3) 7
call func.m
% 2nd calculation
call func.m
% 2nd calculation
VS % 3rd calculation
call func.m
% 3rd calculation
“function” Exercises
Water Resources Systems Engineering (Wk3) 60
“function” exercise
stat.m
function [stat1,stat2,stat3] = stat(x)
stat1 = length(x);
stat2 = sum(x)/stat1;
S = sort(x);
stat3 = S(1);
>> a = [0 -1 2 3 -4];
>> [S1,S2,S3] = stat(a)
MATLAB returns: S1 = 5
S2 = 0
S3 = -4
Water Resources Systems Engineering (Wk3) 61
“function” exercise
mean_st.m
function [mean,stdv] = mean_st(x)
n = length(x);
mean = sum(x)/n;
stdv = sqrt(sum(x.^2)/n – mean.^2);
>> a = [1:10];
>> [m,s] = mean_st(a)
Types of errors
Water Resources Systems Engineering (Wk3) 63
Types of errors
Syntax errors:
▪ Misspelled variables or function names
▪ Missing quotes or parentheses
▪ Errors are found and displayed in command window when
MATLAB evaluates an expression
▪ MATLAB flags these errors immediately and provides
feedback about
▪ the type of error encountered
▪ the line number in the m-file where it occurs.
▪ Given this feedback, such errors are usually easy to spot.