Agenda 23 August 2013 Functions

%

% Concept: Procedural Abstraction

%

% Functions:

% - text files with a .m extension

% - naming convention

% - do you know how sqrt works?

% - do you care?

%

% - calling functions

% - user defined functions

% - testing functions (with a script!)

**********Cylinder Function *****************

function volume = cylinder(height, radius) % Function Call is a must

base = pi * radius.^2 % pi is a constant, area of circle

volume = base * height % volume is area time height

end

**********Use Script to Test Cylinder Function *****************

% This is the way one can use a script to test functions

clc

clear

Input1 = cylinder(1, 1);

Input2 = cylinder(2, 2);

Input3 = cylinder(3, 3);

Input4 = cylinder(4, 4);

**** Write a Function that returns the root of a quadratic equation *****

Understand the Problem

Form: 0 = Ax2 + Bx + C

Formula: -B +- sqrt(B2 – 4AC) / 2A

Inputs: A, B, C

Outputs: Root1, Root2

Design Solution:

1. Define A, B, C

2. Find/Output Root1 using Formula

3. Find/Output Root2 using Formula

4. Build Function

Implement Solution:

A = 1

B = 3

C = 2

Root1 = (-B + sqrt(B.^2 - 4.*A.*C))./(2.*A);

Root2 = (-B - sqrt(B.^2 - 4.*A.*C))./(2.*A);

Test Script

function [Root1 Root2] = myRoots(A, B, C)

% find roots of quadratic

% usage: [Root1 Root2] = myRoots(A, B, C)

% Roots = -B +- sqrt(B^2 - 4AC)/2A

Root1 = (-B + sqrt(B.^2 - 4.*A.*C))./(2.*A);

Root2 = (-B - sqrt(B.^2 - 4.*A.*C))./(2.*A);

end

Test Function

[Root1a Root2a] = myRoots(1, 0, -4)

[Root1b Root2b] = myRoots(1, 3, 2)

[Root1c Root2c] = myRoots(1, 3, 2)

Root1 = myRoots(A, B, C)

[~, Root2] = myRoots(A, B, C)

***************** Problem Odd and Divisible **************************

function is = oddAndDivisible(num, dv1, dv2)

% Inputs (3): - (double) a number to check

% - (double) first divisor

% - (double) second divisor

% Outputs (1): - (logical) logical to see if the first input is odd and

% divisible by the second and third inputs

%

% Function Description:

% Write a function, oddAndDivisible, that checks to see if the first

% input is odd and divisible by the second and third inputs. If all the

% conditions are met, the function should output true. If even

% one of these conditions are not met, then the function should output

% false.

%

% Example:

% If the function call was:

%

% log = oddAndDivisible(35, 5, 7)

%

% First, it checks to see if 35 is odd (which it is). It would then

% check to see if 35 is divisible by 5 (which it is). It would then

% check to see if 35 is divisible by 7 (which it is). Because all of

% these conditions are true, then:

%

% log => true

isOdd = mod(num,2) == 1;

isDiv1 = mod(num/dv1, 1) == 0;

isDiv2 = mod(num/dv2, 1) == 0;

is = isOdd & isDiv1 & isDiv2;

end

Test Cases:

% log1 = oddAndDivisible(35, 5, 7)

% log1 => true

% log2 = oddAndDivisible(30, 5, 6)

% log1 => false

Hints:

% You will need the following information:

% *NOTE* Logical Comparisons ALWAYS return logicals (true or false)

% - Double Equal Comparison ( == )

% ~ Double Equal comparison is a way to compare if one statement

% equivalates to another. For example, if I wanted to know if

% A is equal to B, then I can make the following call:

%

% C = A == B

%

% In which C will be either:

% - true, if A is the same as B

% - false, if A is NOT the same as B

%

% - AND Comparison ( & )

% ~ AND comparison is as simple as it sounds. It compares

% multiple logicals, or logical statements. If every logical or

% logical statement is true, then a true will be returned.

% If any or all the statements are false, then a false will

% be returned. So consider the following statement:

%

% C = A & B

%

% Then C will be either:

% - true, if A and B are both true

% - false, if either A or B or both are false

********** Problem Who wants Candy ***********

%Test Cases:

% bagSize1 = 512;

% numKids1 = 22;

%

% [pieces1, wasted1] = candy(bagSize1, numKids1);

% pieces1 => 23

% wasted1 => 6

%

% bagSize2 = 300;

% numKids2 = 17;

%

% [pieces2, wasted2] = candy(bagSize2, numKids2);

% pieces2 => 17

% wasted2 => 11

function [pieces1, wasted1] = candy1(bagSize1, numKids1);

pieces = bagSize1/numKids1;

pieces1 = floor(pieces)

wasted1 = bagSize1 - (pieces1 * numKids1)

end