









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An outline of functions in matlab, including their definition, syntax, subfunctions, anonymous functions, examples such as the factorial function and approximating sine function, and performance measuring using the sieve of eratosthenes algorithm. Students can use this document as study notes, summaries, or cheat sheets to understand the concepts of functions in matlab.
Typology: Slides
1 / 15
This page cannot be seen from the preview
Don't miss anything!










function [y1,..,yN] = func name(x1,..,xM) % Help text written here and it will be % shown until the first non−comment line
% Do stuff
end % optional
function z = fname (x,y) % This file has to be named fname.m z = x + y; end
% This file can have any valid filename a = input('Enter x: '); b = input('Enter y: '); c = fname(a,b); disp(c)
function z = fname (x,y) % This file has to be named fname.m z = x + y; end
x = 5; disp(x); a = input('Enter x: '); b = input('Enter y: '); c = fname(a,b); disp(c); disp(x);
myfunc = @(x) (xˆ2); y = myfunc(3);
n=
function s = approx sin (x, k) n = 0; s = 0; while n < (2k+1); s = s + (−1)ˆn + xˆn /factorial(n); n = n + 1; end
function p = primes1 (N) p = []; % creates an empty array for j = 1:N if isprime(j) % built−in isprime p = [p, j]; % expands the array end end end
function p = primes2 (N) if N>1, p = [2]; else p = []; end % check only odd numbers for j = 3:2:N if isprime(j) p = [p, j]; end end end
function p = primes2 (N) if N>1, p = [2]; else p = []; end % there are unnecessary checks for j = 3:2:N if isprime(j) p = [p, j]; end end end
% Create an array X of all 1's of length N % Set X(1) to 0 % Find position k of next 1 in the X array % If k is less than or equal to sqrt(N) % Set X(2k), X(3k), X(4*k) ... to zero % Go back to finding k % Else % Find the indices of all 1's in X array % These indices are prime numbers