matlab:introduction_to_matlab
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
matlab:introduction_to_matlab [2017/04/24 01:42] – [Introduction to MATLAB] mithat | matlab:introduction_to_matlab [2019/05/07 00:53] – [What is it?] mithat | ||
---|---|---|---|
Line 3: | Line 3: | ||
====== Introduction to MATLAB ====== | ====== Introduction to MATLAB ====== | ||
Mithat Konar\\ | Mithat Konar\\ | ||
- | 2017-04-22 | + | 2019-04-26 |
===== What is it? ===== | ===== What is it? ===== | ||
- | * A freaking huge 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 | + | * Strings |
* 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 a 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.'; | ||
%} | %} | ||
</ | </ | ||
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]; | ||
- | c = [9 8 7; 6 5 4; 3 2 1]; | + | b = [9 8 7; 6 5 4; 3 2 1]; |
</ | </ | ||
Line 110: | Line 114: | ||
<code matlab> | <code matlab> | ||
- | % matrix | + | r = 1.25 * a; % scalar |
- | r = a * b; | + | r = a * b; % matrix |
- | + | r = a .* b; | |
- | % elementwise | + | r = a ./ b; % elementwise division |
- | r = a .* b; | + | |
- | + | ||
- | % elementwise | + | |
- | r = a ./ b; | + | |
</ | </ | ||
Line 264: | Line 264: | ||
</ | </ | ||
- | 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: | ||
</ | </ | ||
- | 一条线 | + | 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 '' | ||
+ | * Several functions in one file. | ||
+ | * Functions aren't shared. | ||
* Functions can be defined in files that also end in '' | * Functions can be defined in files that also end in '' | ||
* 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 '' | * Functions can be called by any other '' | ||
- | * Functions can be defined in a '' | ||
- | * Several functions in one file. | ||
- | * Functions aren't shared. | ||
- | ===== Example function ===== | ||
- | * Minimum syntax: | ||
- | All examples in in the "User functions" | ||
- | <file matlab traparea.m> | ||
- | function area = traparea(a, b, h) | ||
- | area = 0.5 * (a + b) * h; | ||
- | end | ||
- | </ | ||
- | |||
- | <file matlab calc_trap_area.m> | ||
- | % Uses traparea function defined in traparea.m | ||
- | area1 = traparea(1, 2, 3); | ||
- | disp(area1); | ||
- | </ | ||
===== With " | ===== With " | ||
Line 338: | Line 324: | ||
area = 0.5 * (a + b) * h; | area = 0.5 * (a + b) * h; | ||
end | end | ||
+ | </ | ||
+ | |||
+ | ===== Stand alone function ===== | ||
+ | * Minimum syntax: | ||
+ | All examples in in the "User functions" | ||
+ | <file matlab traparea.m> | ||
+ | function area = traparea(a, b, h) | ||
+ | area = 0.5 * (a + b) * h; | ||
+ | end | ||
+ | </ | ||
+ | |||
+ | <file matlab calc_trap_area.m> | ||
+ | % Uses traparea function defined in traparea.m | ||
+ | area1 = traparea(1, 2, 3); | ||
+ | disp(area1); | ||
</ | </ | ||
matlab/introduction_to_matlab.txt · Last modified: 2021/12/07 19:11 by mithat