matlab:matlab_examples
This is an old revision of the document!
Table of Contents
MATLAB m-file Examples
Simple script
Function m-file
- traparea.m
function area = traparea(a, b, h) % traparea(a, b, h) Computes the area of a trapezoid given % the dimensions a, b and h, where a and b % are the lengths of the parallel sides and % h is the distance between these sides % Compute the area, but suppress printing of the result area = 0.5 * (a + b) * h; end
m-file with "main" function
- compute_trapezoid.m
function main area1 = traparea(1,2,3) disp(area1) end function area = traparea(a, b, h) % traparea(a, b, h) Computes the area of a trapezoid given % the dimensions a, b and h, where a and b % are the lengths of the parallel sides and % h is the distance between these sides % Compute the area, but suppress printing of the result area = 0.5 * (a + b) * h; end
Returning multiple values
- c_to_p.m
function main [r, th] = cart2plr(30, 40); disp(r); disp(th); end function [r, theta] = cart2plr(x, y) % cart2plr Convert Cartesian coordinates to polar coordinates % % [r, theta] = cart2plr(x, y) computes r and theta with % % r = sqrt(x^2 + y^2); % theta = atan2(y,x); r = sqrt(x^2 + y^2); theta = atan2(y,x); end
Nested loop
matlab/matlab_examples.1462074018.txt.gz · Last modified: 2016/05/01 03:40 by mithat