User Tools

Site Tools


matlab:introduction_to_matlab

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
matlab:introduction_to_matlab [2016/12/04 23:05] – [3D plots] mithatmatlab:introduction_to_matlab [2021/12/07 19:10] – [With "main" function] mithat
Line 3: Line 3:
 ====== Introduction to MATLAB ====== ====== Introduction to MATLAB ======
 Mithat Konar\\ Mithat Konar\\
-2016-04-30+2019-04-26 
 ===== What is it? ===== ===== What is it? =====
-  * A freaking huge graphing calculator+  * A //high-level//, //interpreted// language and environment targeting scientific computing. 
 +  * A freaking huge, feature filled graphing calculator
   * Comprehensive   * Comprehensive
   * Expensive   * Expensive
Line 32: Line 34:
  
 ===== Variables ===== ===== Variables =====
-  * MATLAB has only one numeric type+  * Numbers are double precision floats by default
-  * Strings are OK too.+  * Strings exist too.
     * Use single quotes.     * Use single quotes.
     * For a literal single quote in a string, use two single quotes.     * For a literal single quote in a string, use two single quotes.
Line 46: Line 48:
  
 ===== Comments ===== ===== Comments =====
-  * Use percent sign.+  * Use the percent sign.
 <code matlab> <code matlab>
-like this+a single line comment
 a = 99; % this works too a = 99; % this works too
  
 %{ %{
-foo = 'This is a'; +foo = 'multiple line comments'; 
-baz = 'multiple line comment';+baz = 'use curly brackets inside'; 
 +qux = 'a pair of percent signs.';
 %} %}
 </code> </code>
Line 103: Line 106:
 <code matlab> <code matlab>
 % separate matrix rows with ';' % separate matrix rows with ';'
 +
 a = [1 2 3; 4 5 6; 7 8 9]; a = [1 2 3; 4 5 6; 7 8 9];
-= [9 8 7; 6 5 4; 3 2 1];+= [9 8 7; 6 5 4; 3 2 1];
 </code> </code>
  
Line 110: Line 114:
  
 <code matlab> <code matlab>
-matrix multiplication +r = 1.25 * a; scalar multiplication 
-r = a * b; +r = a * b;    matrix multiplication 
- +r = a .* b;   % elementwise multiplication 
-elementwise multiplication +r = a ./ b;   % elementwise division
-r = a .* b; +
- +
-% elementwise division +
-r = a ./ b;+
 </code> </code>
  
Line 253: Line 253:
  
   * ''switch_expr'' doesn't need to be a number   * ''switch_expr'' doesn't need to be a number
-  * ''case'' can match multiple vales+  * ''case'' can match multiple values
   * No "break" is needed.   * No "break" is needed.
  
Line 264: Line 264:
 </code> </code>
  
-Una línea:+On one line:
 <code matlab> <code matlab>
 while n < 10,  y = y * n;  n = n + 1; end while n < 10,  y = y * n;  n = n + 1; end
Line 276: Line 276:
 </code> </code>
  
-一条线+On one line:
 <code matlab> <code matlab>
 for n = 0 : 0.5 : 2, disp(n); end for n = 0 : 0.5 : 2, disp(n); end
Line 304: Line 304:
  
 ===== User functions ===== ===== User functions =====
 +  * Functions can be defined in a ''.m'' file with a ''main'' function.
 +    * Several functions in one file.
 +    * Functions aren't shared.
   * Functions can be defined in files that also end in ''.m''   * Functions can be defined in files that also end in ''.m''
     * One function per file.     * One function per file.
     * File name must match function name.     * File name must match function name.
     * Functions can be called by any other ''.m'' file or interactively.     * Functions can be called by any other ''.m'' file or interactively.
-  * Functions can be defined in a ''.m'' file with a ''main'' function. 
-    * Several functions in one file. 
-    * Functions aren't shared. 
  
-===== Example function =====+ 
 +===== With "main" function ===== 
 +  * Minimum syntax: 
 +<file matlab main.m> 
 +function main() 
 +  area1 = traparea(1, 2, 3); 
 +  disp(area1); 
 +end 
 + 
 +function area = traparea(a, b, h) 
 +  area = 0.5 * (a + b) * h; 
 +end 
 +</file> 
 + 
 +===== Stand alone function =====
   * Minimum syntax:((Recktenwald, Gerald. "MATLAB Functions -- Basic Features." MATLAB Functions -- Basic Features. Accessed April 30, 2016. http://web.cecs.pdx.edu/~gerry/MATLAB/programming/basics.html.\\   * Minimum syntax:((Recktenwald, Gerald. "MATLAB Functions -- Basic Features." MATLAB Functions -- Basic Features. Accessed April 30, 2016. http://web.cecs.pdx.edu/~gerry/MATLAB/programming/basics.html.\\
 All examples in in the "User functions" section are from Recktenwald.)) All examples in in the "User functions" section are from Recktenwald.))
Line 325: Line 339:
 area1 = traparea(1, 2, 3); area1 = traparea(1, 2, 3);
 disp(area1); disp(area1);
-</file> 
- 
-===== With "main" function ===== 
-  * Minimum syntax: 
-<file matlab my_script.m> 
-function main 
-  area1 = traparea(1, 2, 3); 
-  disp(area1); 
-end 
- 
-function area = traparea(a, b, h) 
-  area = 0.5 * (a + b) * h; 
-end 
 </file> </file>
  
matlab/introduction_to_matlab.txt · Last modified: 2021/12/07 19:11 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki