matlab:matlab_examples
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
matlab:matlab_examples [2016/05/01 03:40] – created mithat | matlab:matlab_examples [2021/12/07 19:41] (current) – [m-files with "main" function] mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | [[http:// | + | ====== MATLAB m-file Examples ====== |
===== Simple script ===== | ===== Simple script ===== | ||
Line 16: | Line 16: | ||
===== Function m-file ===== | ===== Function m-file ===== | ||
+ | Ref.((Recktenwald, | ||
<file matlab traparea.m> | <file matlab traparea.m> | ||
function area = traparea(a, b, h) | function area = traparea(a, b, h) | ||
Line 29: | Line 29: | ||
</ | </ | ||
- | ===== m-file with " | + | ===== m-files with " |
+ | Ref.((Recktenwald, | ||
+ | |||
+ | Note: On some platforms you may receive a warning if the name of the file (without extension) doesn' | ||
<file matlab compute_trapezoid.m> | <file matlab compute_trapezoid.m> | ||
- | function main | + | function main() |
area1 = traparea(1, | area1 = traparea(1, | ||
disp(area1) | disp(area1) | ||
Line 45: | Line 48: | ||
% Compute the area, but suppress printing of the result | % Compute the area, but suppress printing of the result | ||
area = 0.5 * (a + b) * h; | area = 0.5 * (a + b) * h; | ||
+ | end | ||
+ | </ | ||
+ | |||
+ | Ref.((Unknown)) | ||
+ | <file matlab quads.m> | ||
+ | function main() | ||
+ | [root1, root2] = quadratic(1, | ||
+ | disp(root1); | ||
+ | disp(root2); | ||
+ | end | ||
+ | |||
+ | function [x1, x2] = quadratic(a, | ||
+ | % [x1, x2] = quadratic(a, | ||
+ | % Return the roots of a*x^2 + b*x + c. | ||
+ | % Roots may be complex. | ||
+ | |||
+ | d = disc(a, b, c); | ||
+ | x1 = (-b + sqrt(d)) / (2*a); | ||
+ | x2 = (-b - sqrt(d)) / (2*a); | ||
+ | end | ||
+ | |||
+ | function dis = disc(a, | ||
+ | % dis = disc(a,b,c) | ||
+ | % Return the discriminant of a, b, c. | ||
+ | dis = b^2 - 4*a*c; | ||
end | end | ||
</ | </ | ||
===== Returning multiple values ===== | ===== Returning multiple values ===== | ||
+ | Ref.((Recktenwald, | ||
<file matlab c_to_p.m> | <file matlab c_to_p.m> | ||
function main | function main |
matlab/matlab_examples.1462074018.txt.gz · Last modified: 2016/05/01 03:40 by mithat