-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp02_Coding.tex
More file actions
374 lines (330 loc) · 9.66 KB
/
App02_Coding.tex
File metadata and controls
374 lines (330 loc) · 9.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
\chapter{MATLAB Basics}\label{app:MATLAB}
In this appendix we'll go through a few of the basics in MATLAB. This is by no means
meant to be an all-encompassing resource for MATLAB programming. A few more thorough
resources for MATLAB are listed here.
\begin{itemize}
\item \href{https://www.mathworks.com/help/pdf_doc/matlab/matlab_prog.pdf}{https://www.mathworks.com/help/pdf\_doc/matlab/matlab\_prog.pdf}
\item
\href{https://www.mathworks.com/products/matlab/examples.html}{https://www.mathworks.com/products/matlab/examples.html}
\item
\href{https://en.wikibooks.org/wiki/MATLAB_Programming}{https://en.wikibooks.org/wiki/MATLAB\_Programming}
\item
\href{http://gribblelab.org/scicomp/scicomp.pdf}{http://gribblelab.org/scicomp/scicomp.pdf}
(this is a personal favorite)
\end{itemize}
In this appendix we'll give examples of some of the more common coding practices that the
reader will run into while working through the exercises and problems in these notes.
\section{Vectors and Matrices}
\begin{example}
Write the vectors $\bv = \begin{pmatrix} 1 \\ 2 \\ 3 \end{pmatrix} \text{ and } \bw =
\begin{pmatrix} 4 & 5 & 6 & 7 \end{pmatrix}$ using MATLAB.\\
{\bf Solution:}
\begin{lstlisting}
v = [1 ; 2 ; 3]
w = [4 , 5 , 6 , 7]
w = 4:7 % this is shorthand for writing a sequence as a row vector
\end{lstlisting}
\end{example}
\begin{example}
Consider the matrices and vectors
\[ A = \begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 0 \end{pmatrix} \quad B =
\begin{pmatrix} 3 & 5 & 7 \\ 9 & 1 & 3 \\ 5 & 7 & 11 \end{pmatrix} \quad
\text{and} \quad \bb = \begin{pmatrix} 4 & 3 & -1 \end{pmatrix} \]
\begin{itemize}
\item Calculate the product $AB$ using regular matrix multiplication
\begin{lstlisting}
A = [1 , 2 , 3;
4 , 5 , 6;
7 , 8 , 0]
B = [3 , 5 , 7;
9 , 1 , 3;
5 , 7 , 11]
Product = A*B
\end{lstlisting}
\item Calculate the element-by-element multiplication of $A$ and $B$
\begin{lstlisting}
ElementWiseProduct = A .* B
\end{lstlisting}
\item Calculate the inverse of $A$
\begin{lstlisting}
Ainv = A^(-1)
Ainv = inverse(A) % alternative
\end{lstlisting}
\item Calculate the transpose of $B$
\begin{lstlisting}
Atranspose = transpose(A)
% or as an alternative:
Atranspose = A' % actually the conjugate transpose but if A is real then ok
\end{lstlisting}
\item Solve the system of equations $A\bx = \bb$
\begin{lstlisting}
b = [4 ; 3 ; -1]
x = A \ b
\end{lstlisting}
\end{itemize}
\end{example}
\begin{example}
Code for a matrix of zeros
\begin{lstlisting}
Z = zeros(5,5) % 5 x 5 matrix of all zeros
\end{lstlisting}
\end{example}
\begin{example}
Code for an identity matrix
\begin{lstlisting}
Ident = eye(5,5) % 5 x 5 identity matrix
\end{lstlisting}
\end{example}
\begin{example}
Code for random matrices.
\begin{itemize}
\item random matrix from a uniform distribution on $[0,1]$
\begin{lstlisting}
R = rand(5,5) % random 5 x 5 matrix
\end{lstlisting}
\item random matrix from the standard normal distribution
\begin{lstlisting}
R = randn(5,5) % random 5 x 5 matrix
\end{lstlisting}
\end{itemize}
\end{example}
\begin{example}
A linearly spaced sequence
\begin{lstlisting}
List = linspace(0,10,100)
% a list of 100 equally spaced numbers from 0 to 10
\end{lstlisting}
\end{example}
\section{Looping}
A loop is used when a process needs to be repeated several times.
\subsection{For Loops}
A \texttt{for loop} is code that repeats across a pre-defined sequence.
\begin{example}
Write a loop that produces the squares of the first 10 integers.
\begin{lstlisting}
for j = 1:10
j^2
end
\end{lstlisting}
The output of this code will be
\begin{verbatim}
1
4
9
16
25
36
49
64
81
100
\end{verbatim}
\end{example}
\begin{example}
Plot the functions $f(x) = \sin(kx)$ for $k=1, 1.5, 2, 2.5, \ldots, 5$ on the domain $x \in
[0,2\pi]$.
\begin{lstlisting}
x = linspace(0,2*pi,1000);
for k = 1:0.5:5
plot(x , sin(k*x) )
hold on
end
\end{lstlisting}
\end{example}
\subsection{The While Loop}
A \texttt{while loop} is a process that only repeats while a conditional statement is
true. Be careful with \texttt{while loops} since it is possible to create a loop that
runs forever.
\begin{example}
Build the Fibonnaci sequence up until the last term is greater than 1000.
\begin{lstlisting}
F(1) = 1; % first term
F(2) = 1; % second term
n = 3;
while F(end)<1000
F(n) = F(n-1) + F(n-2);
n=n+1;
end
\end{lstlisting}
\end{example}
\begin{example}
An example of a while loop that runs forever.
\begin{lstlisting}
a = 1;
while a>0
a=a+1;
end
\end{lstlisting}
\end{example}
\begin{example}
An example of a while loop that runs forever but with a failsafe step that stops the
loop after 1000 steps.
\begin{lstlisting}
a = 1;
counter=1;
while a>0
a=a+1;
if counter >= 1000
break
end
counter = counter+1;
end
\end{lstlisting}
\end{example}
\section{Conditional Statements}
Conditional statements are used to check if something is true or false. The output of a
conditional statement is a boolean value; true (1) or false (0).
\subsection{If Statements}
\begin{example}
Loop over the integers up to 100 and output only the multiples of three.
\begin{lstlisting}
for j = 1:100
if mod(j,3) == 0
j
end
end
\end{lstlisting}
\end{example}
\begin{example}
Check the signs of two function values and determine if they are opposite.
\begin{lstlisting}
f = @(x) x^3*(x-3);
a = 2;
b = 4;
if f(a)*f(b) < 0
fprintf('The function values are opposite sign\n')
elseif f(a)*f(b) >0
fprintf('The function values are the same sign\n')
else
fprintf('The function values are both zero\n')
end
\end{lstlisting}
\end{example}
\subsection{Case-Switch Statements}
\begin{example}
Evaluate over several cases.
\begin{lstlisting}
n = 3
switch n
case 1 % if n == 1
fprintf('n is 1\n')
case 2 % if n == 2
fprintf('n is 2\n')
case 3 % if n == 3
fprintf('n is 3\n')
end
\end{lstlisting}
\end{example}
\section{Functions}
A mathematical function has a single output for every input, and in
some sense a computer function is the same: one single executed process for each
collection of inputs.
\begin{example}
Define the function $f(x) = \sin(x^2)$ so that it can accept any type of input
(symbol, number, or list of numbers).
\begin{lstlisting}
f = @(x) sin(x.^2) % defines the function
f(3) % evaluates the function at x=3
x=linspace(0,pi,100);
f(x) % evaluates f at 100 points equally spaced from 0 to pi
\end{lstlisting}
\end{example}
\begin{example}
Write a computer function that accepts two numbers as inputs and outputs the sum plus
the product of the two numbers. \\
First write a file with the following contents.
\begin{lstlisting}
function MyOutput = MyFunctionName(a,b)
MyOutput = a + b + a*b;
end
\end{lstlisting}
Be sure that the file name is the same as the function name.\\
Then you can call the function by name in a script or another function.
\begin{lstlisting}
SumPlusProduct = MyFunctionName(3,4)
\end{lstlisting}
which will output the number $19$.
\end{example}
\begin{example}
Write a function with three inputs that outputs the sum of the three. The third input
should be optional and the default should be set to 5.
\begin{lstlisting}
function AwesomeOutput = SumOfThree(a,b,c)
if nargin < 3
c = 5;
end
AwesomeOutput = a+b+c;
end
\end{lstlisting}
You can call this function with
\begin{lstlisting}
SumOfThree(17,23)
\end{lstlisting}
which will output $17+25+5 = 47$. Notice that the third input was left off and a 5 was
used in its place.
\end{example}
\section{Plotting}
In numerical analysis we are typically plotting numerically computed lists of numbers so
as such we will give a few examples of this type of plotting here. We will not, however,
give examples of symbolic plotting.
The \mcode{plot} command in MATLAB accepts a list of $x$ values followed by a list of $y$
values then followed by color and symbol options.\\
\mcode{plot(xlist , ylist , color options)}
\begin{example}
Plot the function $f(x) = \sin(x^2)$ on the interval $[0,2\pi]$ with 1000 equally
spaced points. Make the plot color blue.
\begin{lstlisting}
x = linspace(0,2*pi,1000);
f = @(x) sin(x.^2);
plot(x , f(x) , 'b')
\end{lstlisting}
Alternatively
\begin{lstlisting}
x = linspace(0,2*pi,1000);
y = sin(x.^2);
plot(x, y, 'b')
\end{lstlisting}
\end{example}
\begin{example}
Make a $2\times 2$ array of 4 plots of $f(x) = \sin(k x^2)$ for $k=1, 2, 3, 4$.
\begin{lstlisting}
x = linspace(0,2*pi,1000);
for k=1:4
subplot(2,2,k)
plot(x , sin(k*x.^2) , 'b')
end
\end{lstlisting}
\end{example}
\begin{example}
Plot $f(x) = \sin(kx^2)$ for $k=1, 2, \ldots, 10$ all on the same plot.
\begin{lstlisting}
x = linspace(0,2*pi,1000);
for k=1:10
plot(x, sin(k*x.^2))
hold on % this holds the figure window open so you can write on top of it
end
\end{lstlisting}
\end{example}
\begin{example}
Plot the function $f(x) = e^{-x} \sin(x)$ and put a mark at the local max at $x =
\pi/4$.
\begin{lstlisting}
x = linspace(0,2*pi,1000); % set up the domain
f = @(x) exp(-x) .* sin(x);
plot(x,f(x),'b',pi/4,f(pi/4),'ro')
\end{lstlisting}
\end{example}
\section{Animations}
\begin{example}
Plot $f(x) = \sin(kx^2)$ for $k=1$ to $k= 10$ by small increments with a short pause
in between each step.
\begin{lstlisting}
x = linspace(0,2*pi,1000);
for k=1:0.01:10 % 1 to 10 by 0.01
plot(x, sin(k*x.^2))
hold on % this holds the figure window open so you can write on top of it
drawnow % draws the plot
% the last line gives the illusion of animation
end
\end{lstlisting}
\end{example}