Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

MATLAB Functions: Definition, Syntax, Subfunctions, Anonymous Functions, Slides of Computer Programming

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

2012/2013

Uploaded on 08/17/2013

zaid
zaid šŸ‡®šŸ‡³

4.5

(2)

62 documents

1 / 15

Toggle sidebar

Related documents


Partial preview of the text

Download MATLAB Functions: Definition, Syntax, Subfunctions, Anonymous Functions and more Slides Computer Programming in PDF only on Docsity! Outline Functions M-files Subfunctions Anonymous functions Examples Factorial Function Approximating Sine function Sieve of Eratosthenes Lecture 05 Functions docsity.com Functions Syntax 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 Lecture 05 Functions docsity.com Variable Scope Function Scope function z = fname (x,y) % This file has to be named fname.m z = x + y; end Global Scope x = 5; disp(x); a = input('Enter x: '); b = input('Enter y: '); c = fname(a,b); disp(c); disp(x); Lecture 05 Functions docsity.com Subfunctions Functions within functions function y = myfunc(x) y = sub1(sqrt(x)) + sub2(x) function t = sub1(x) t = log(x); end function r = sub2(p) r = 2*p; end end Lecture 05 Functions docsity.com Anonymous Functions not stored in a file myfunc = @(x) (xĖ†2); y = myfunc(3); Lecture 05 Functions docsity.com Primes Function Question What are all prime numbers ā‰¤ N? Using what we know 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 Lecture 05 Functions docsity.com A Better Primes Function Add knowledge All prime numbers, except 2, are odd numbers. Updated code 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 Lecture 05 Functions docsity.com Measuring Performance tic/toc tic starts the timer, toc returns the elapsed time. Comparing primes functions N = input('Enter N: '); tic % Start timer p0 = primes(N); % Call builtāˆ’in primes t0 = toc; % Stop timer and % store elapsed time % Let's also measure our functions tic; p1 = primes1(N); t1 = toc; tic; p2 = primes2(N); t2 = toc; Lecture 05 Functions docsity.com
Docsity logo



Copyright Ā© 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved