CPSC 215 — Assignment 4
Expressions

Winter 2002 — March (6^th), 2002 — C. Jacob

<your name here>
<your student ID here>

Instructions for this Assignment

Welcome to the fourth assignment for CPSC 215 — Introduction to Programming with Mathematica.

(1) In the following sections, enter your answers as Mathematica commands in the Input cells provided after each problem description or question.

(2) After you have solved all the problems, go to section "How to Submit Your Notebook" and follow the instructions.

(1) Expression Representation of Simple Inputs

Give the internal expression representation for the following Mathematica inputs. Write your solution in the provided Input cells.

Test your answer by evaluating the expression and comparing it to the given inputs.

Expression 1: A Polynomial Expression

\(\(\(	\)\(\((a + b)\)\^4\)\)\)

Expression Representation:

Power[Plus[a, b], 4]

Expression 2: Another Polynomial Expression

\(\(\(	\)\(a\^2 - 2  a\ b + b\^2\)\)\)

Expression Representation:

Plus[Power[a, 2], Times[-2, a, b], Power[b, 2]]

Expression 3: A Derivative

x'[t]

Expression Representation:

Derivative[1][x][t]

Expression 4: A Partial Derivative

D[f[x,y],x]

Expression Representation:

Derivative[1, 0][f][x, y]

Note: Here you might get an output that is different from the above input.

Expression 5: A List of Expressions

\(\(\(	\)\({a\^7, b - c, 
      d\^4\/\@e + 4, \((f + g - h)\)\^3}\)\)\)

Expression Representation:

List[Power[a, 7], Plus[b, Times[-1, c]], 
      Plus[4, Times[Power[d, 4], Power[e, Rational[-1, 2]]]], 
      Power[Plus[f, g, Times[-1, h]], 3]]

(2) Expression Representation of Assignments and Function Definitions

Give the internal expression representation for the following Mathematica inputs. Write your solution in the provided Input cells.

Show that your answer is correct! You may also add comments about how you check the correctness of your answers.

Input 1: Compound Expressions and Assignments

\(\(\(	\)\(a = 3; b = 2\ a; c := b\^2\)\)\)

Expression Representation:

Note: Check the description of CompoundExpression in the Help Browser!

CompoundExpression[Set[a, 3], Set[b, Times[2, a]], 
      SetDelayed[c, Power[b, 2]]]

Here is my Check that this Answer is Correct: (Add a comment, if you like.)

Comment: The values of a, b, and c are initialized correctly, as the following list shows.

{a, b, c}

Input 2: An Assignment

\(\(\(	\)\(c := \((b - 3)\)\^5 // Expand\)\)\)

Expression Representation:

SetDelayed[c, Expand[Power[Plus[b, -3], 5]]]

Here is my Check that this Answer is Correct: (Add a comment, if you like.)

Comment: By entering c, we in fact get the expansion of the polynomial (((b - 3))^5).

c

Input 3: A Function Definition

funcOne[a_] := a + 3

Expression Representation:

SetDelayed[
funcOne[
        Pattern[a, Blank[]]], 
Plus[a, 
        3]
]

Here is my Check that this Answer is Correct: (Add a comment, if you like.)

Comment: By asking Mathematica what is known about funcOne, we see that the desired definition for funcOne is available.

?funcOne

Input 4: A Function Definition with Several Variables and Type Patterns

funcTwo[str_String,int_Integer,lst_List]:= Join[Take[Characters[str],int],lst]

Expression Representation:

SetDelayed[
funcTwo[
Pattern[str, 
          Blank[String]], 
Pattern[int, 
          Blank[Integer]], 
Pattern[lst, 
          Blank[List]]], 
Join[Take[Characters[str], int], 
        lst]
]

Here is my Check that this Answer is Correct: (Add a comment, if you like.)

Comment: We first check the correctness of the definition for funcTwo. We also test the function for specific inputs: a string, a number, and a list.

?funcTwo

funcT["computer", 5, {1, 2}]

Input 5: A Functional Expression

f /@ {5,3,2,1,4} // Sort

Expression Representation

Sort[Map[f, List[5, 3, 2, 1, 4]]]

Here is my Check that this Answer is Correct: (Add a comment, if you like.)

Comment: This is an almost one-to-one transformation from the prefix-/postfix-notation to the expression representation. One possible check is to apply FullForm to get the internal (expression) notation. However, one has to prevent the evaluation of the expression by wrapping Hold around it.

Hold[f /@  {5, 3, 2, 1, 4} // Sort] // FullForm

(3) Programming a Notebook

(1) Building a Notebook Step-by-Step

Note: Before you attempt to solve this exercise, check the following functions in the Help Browser: 
	- NotebookCreate, 
	- NotebookWrite, 
	- SelectionMove, 
	- SelectionEvaluate.

Write down the commands that have the following effects: (Enter your solution in the provided Input cells.)

(1) Create an empty notebook.

(2) Add a Title cell to the notebook with the text "A Sine Plot".

(3) Add a Text cell that contains the text "The following command creates a plot of the Sine function.".

(4) Add an Input cell that contains the command to plot the function Sin[x] in the interval between 0 and 2 π.

(5) Move the cursor selection to this last Input cell.

(6) Invoke the evaluation of this selected Input cell, so that the graphic of the sine function appears in the notebook.

Solution

nb = NotebookCreate[]

NotebookWrite[nb, Cell["A Sine Plot", "Title"]]

\(NotebookWrite[nb, 
      Cell["The following command creates a plot of the Sine function.", "Text"]]\)

NotebookWrite[nb, 
      Cell["Plot[Sin[x],{x,0,2π}]", "Input"]]

SelectionMove[nb, Previous, Cell]

SelectionEvaluate[nb]

(2) A Function for Generating a Plot in a Notebook

(2.1) Generating a Function Plot in a New Notebook

Assume we have given a list of functions:

functionList = {Sin, Cos, Tan, Exp, Sqrt};

Write a function notebookPlot[fun_] that, given one of the functions from functionList, opens a new notebook, and generates a plot of the selected function in this notebook. You may assume that all plots have the same plot interval from 0 to 2 π.

So, for example, if you called the function

notebookPlot[Sin]

it would open a new notebook, that (apart from some introductory headings) contains the plot command and the following resulting graphic:

Note: At some point you might need the functions StringJoin and ToString. Check them out in the Help Browser!

Enter your solution here:

notebookPlot[fun_]:=

Solution:

\(notebookPlot[
        fun_] := 
Module[{nb = 
            NotebookCreate[]}, 
NotebookWrite[nb, 
          Cell["A Function Plot", "Title"]]; 
NotebookWrite[nb, 
          Cell["The following command creates a plot of a function.", "Text"]]; 

NotebookWrite[nb, 
          Cell[StringJoin["Plot[", 
              ToString[
                fun], "[x],{x,0,2π}];"], "Input"]]; 
SelectionMove[nb, Previous, 
          Cell]; 
SelectionEvaluate[
          nb]
]\)

(2.2) Generating a Plot of a Randomly Selected Function in a New Notebook

Finally, use the function notebookPlot from Part 1 to implement a new function randomNotebookPlot. This function should generate a plot of one of the functions that is randomly selected from functionList.

That is, whenever the command

randomNotebookPlot

is entered, one of the functions from functionList would be plotted in a newly created notebook.

Enter your solution here:

Solution:

functionList = {Sin, Cos, Tan, Exp, Sqrt};

randomSelect[lst_] := 
      lst[[Random[
          Integer, {1, Length[lst]}]]]

randomNotebookPlot := 
      notebookPlot[randomSelect[functionList]]

How to Submit Your Notebook

Step 1

Save this notebook under the filename Ass4-<your student id>.nb.

For example, if your student ID is 123456, you would save this notebook as Ass4-123456.nb

Step 2

Send your notebook by email to your Teaching Assistant (TA).