Script M-file

Functions on Functions

Suppose a function y = demoFun(x) has been defined in a function M-file demoFun.m

fplot(@demoFun,[a b]) Plots the function for a ≤ x ≤ b without setting up arrays
fzero(@demoFun,[a b])

Finds one value of x for demoFun(x) = 0 provided that the signs of demoFun(a) and demoFun(b) are opposite.

fzero(@demoFun,c) Finds one value of x for demoFun(x) = 0 by starting a search at x = c
fminbnd(@demoFun,a,b)

Finds the coordinates of a minimum point of demoFun(x) at the interval
a≤x≤b

quadl(@demoFun,a,b)

Finds an accurate value for

Note: quadl uses arrays, so therefore you must set your function up treating x as an array and so using the dot notation for operations.

 

Exercise: Using Functions

1. Create a function M-file called myCubic.m whose output is the value of the function

Input: x

Output: y

> Verify that this function works by checking that myCubic(-5)=-58 and myCubic(5)=142

2. Create a script M-file called cubicExercise.m that contains the following five cells with code that:

a) plots myCubic(x) between the values of [-5, 5],

b) finds a local minimum of myCubic(x), located between 0 and 5,

c) finds the all three roots of myCubic(x) using appropriate intervals

[a,b]

d) finds the value of the definite integral of myCubic(x) between -5 and 5.

Hint: You will need to use the array dot notation so you can use the quadl function to calculate the integral of y.

3. Make sure cubicExercise.m is marked up using cell formatting and publish it.