Question Sheet 6 - Challenge Problem

Note that, while in maths a function is typically something similar to f(x)=(x^2). When coding, a function is just something that has a dependence on a variable. So for example, the following plots, that are dependent on the value of ‘n’ in the argument of sign, can be defined as functions of n.

1 a) A student has defined two functions below. Which form is more appropriate and why?

f1[n_]:=Plot[Sin[n*x],{x,0,2π}]

g1[n_]=Plot[Sin[n*x],{x,0,2π}]

b) Evaluate the above code, and then plot f1(3) and g1(3) to test your predictions. Compare your prediction with the output and explain any differences between the output of the two functions.

2 a) Define two functions that plot (x^2) over an arbitrary range a to b (analogous to those in question 1). One should use ‘=’ and the other should use ‘:=’ to define the function.

f2

g2

b) Do you expect either/both of the functions to work? Why/why not?

c) Specify some values for a and b and evaluate both functions to test if they work.

d) Did your predictions match the results? Comment on any discrepancies.

3. A student has produced the code below to plot sin(n) for a range of values of n. Evaluate the code and comment on any errors the student has made.

f3[x_]:=Plot[Sin[n*x],{x,0,2π}]

f3[3]