| 1 | <h2>Strings</h2> |
| 2 | <pre><code>myString = 'Hello, world'; |
| 3 | otherString = 'You''re right';</code></pre> |
| 4 | |
| 5 | <h2>Comments</h2> |
| 6 | <pre><code>% Single line comment |
| 7 | %{ Multi-line |
| 8 | comment }%</code></pre> |
| 9 | |
| 10 | <h2>Numbers</h2> |
| 11 | <pre><code>x = 325.499 |
| 12 | realmax + .0001e+308 |
| 13 | e = 1 - 3*(4/3 - 1) |
| 14 | b = 1e-16 + 1 - 1e-16; |
| 15 | x = 2 + 3i; |
| 16 | z = |
| 17 | 4.7842 -1.0921i 0.8648 -1.5931i 1.2616 -2.2753i |
| 18 | 2.6130 -0.0941i 4.8987 -2.3898i 4.3787 -3.7538i |
| 19 | 4.4007 -7.1512i 1.3572 -5.2915i 3.6865 -0.5182i |
| 20 | </code></pre> |
| 21 | |
| 22 | <h2>Control flow</h2> |
| 23 | <pre><code>if rem(a, 2) == 0 |
| 24 | disp('a is even') |
| 25 | b = a/2; |
| 26 | end |
| 27 | switch dayString |
| 28 | case 'Monday' |
| 29 | disp('Start of the work week') |
| 30 | case 'Tuesday' |
| 31 | disp('Day 2') |
| 32 | case 'Wednesday' |
| 33 | disp('Day 3') |
| 34 | case 'Thursday' |
| 35 | disp('Day 4') |
| 36 | case 'Friday' |
| 37 | disp('Last day of the work week') |
| 38 | otherwise |
| 39 | disp('Weekend!') |
| 40 | end |
| 41 | n = 1; |
| 42 | nFactorial = 1; |
| 43 | while nFactorial < 1e100 |
| 44 | n = n + 1; |
| 45 | nFactorial = nFactorial * n; |
| 46 | end</code></pre> |
| 47 | |
| 48 | <h2>Functions</h2> |
| 49 | <pre><code>q = integral(sqr,0,1); |
| 50 | y = parabola(x) |
| 51 | mygrid = @(x,y) ndgrid((-x:x/c:x),(-y:y/c:y)); |
| 52 | [x,y] = mygrid(pi,2*pi);</code></pre> |