Fight club 12 Sept.

a = "Already Defined"

g[_] = a

Table[g[i],{i,1,4}]

Attempt to write a function that takes and argument and squares it.

this attempt failed in this case

g[a_] = a

g[12]

what we wanted was the function to “wait” and not evaluate the rhs immediately (so to make the function “wait”, we delayed set)

delayed set := doesn’t evaluate until you use it later

h[a_] := a^2

h[q]

h[3]

Table[h[i],{i,1,4}]

Lesson---the “canonical way” to define function is this:

f[x_] := x^2

(let’s read this as mathematica does.
1) the unevaluated expression x^2 is set to  f[x_]
2) when f[x] is called, the argument becomes a named pattern, this pattern is “x”
3) f[x] then evaluted the rhs and uses the pattern

t[n_] := Table[h[i],{i,1,n}]

t[5]

t[12]

Let’s try to make function that make a list of i^m and list has a length n

method 1, all in...

ourfunction[length_, power_] := Table[i^power,{i,1,length}]

ourfunction[12,4]

method 2, in steps (instructive, but not useful in this case..)

idea is to write simple things, test them, and put together later

powfun[i_,m_] := i^m

powfun[2,4]

ourfunction2[len_,pow_] := Table[powfun[i,pow],{i,1,len}]

ourfunction[14,2]

Use patterns to select cases in lists

Create a list where each element is one or two times yes or no...

Creating an example of aList which this “actual” example will be used....

aList = Table[ RandomInteger[{1,4}] RandomChoice[{Yes,No}],{i,1,20}]

Objective is to select those cases with a Yes...

Cases[aList,a_ Yes]

Objective is to get anything that is “2” times something

Cases[aList,2 *  _]

“advanced ideas..... pattern restriction....

Our objective is to find anything which is even multiplied by Yes.  This means we need to test the pattern (i.e., a_) to make sure it is even

EvenQ[4]

Table[EvenQ[i],{i,1,10}]

Pattern restriction goes like this : a_/;

aList

b

Cases[aList,(a_/;EvenQ[a]) No]

Back to the question of why isn’t aList changing because it was constructed from random things

aList = Table[ RandomInteger[{1,4}] RandomChoice[{Yes,No}],{i,1,20}]

aList

aList = Table[ RandomInteger[{1,4}] RandomChoice[{Yes,No}],{i,1,20}]

Suppose we wanted the list to always be random each time it is used....

randomList :=  Table[ RandomInteger[{1,4}] RandomChoice[{Yes,No}],{i,1,20}]

randomList

randomList

Examples of delayed set (:= ) and set (=)

pastDate = DateString[]

currentDate := DateString[]

pastDate

currentDate

Miss Loynachan’s Question

D[x Log[x],x]

Miss Loynachan’s REAL Question

lhs = D[y[x], x] + y[x]

rhs = Sin[x]

ode = lhs==  rhs

Clear[b]

ode

dsol = DSolve[{ode, y[0]== b},y[x],x]

choices /.

ysol = y[x]/.dsol

ysol = y[x]/.dsol[[1]]

ySOL[x_, b_] := -(1/2) (E^(-x)) (-1-2 b+(E^x) Cos[x]-(E^x) Sin[x])

ySOL[.2,.1]

Plot[ySOL[x,.1],{x,0,1}]

manSols = Table[ySOL[x,b],{b,0,1,.1}]

Plot[manSols,{x,0,1}]

Manipulate[Plot[ySOL[x,b],{x,0,1}],{b,-1,1}]

How to solve a quadratic equation

Clear[a,b,c]

rhs = a x^2+ b x + c

thesols = Solve[a x^2+ b x + c == 0,x]

rhs/.thesols

This gave me two instances of the solution, because there were two solutions in thesols

Simplify[rhs/.thesols]

How manipulates work (there is a long long tutorial on this, but let’s cut to the chase...)
Manipuate is just like table, but the iterator change the position in the table

Table[i^2, {i, 1, 10}]

Manipulate[i^2, {i, 1, 10,1}]

Manipulate[
Table[Factorial[j],{j,1,i}],
 {i, 1, 100,1}]

Manipulate[
Plot3D[Sin[ 2 Pi a x] Cos[2 Pi b y],{x,0,1},{y,0,1}] ,
{a,1, 20},
{b, 1, 20}
]

Combining graphics

Plot[{x, x^2, x^3},{x,0,2}]

p1 = Plot[x,{x,0,2},PlotStyle-> Blue]

p2 = Plot[x^2,{x,0,2}, PlotStyle-> Green]

p3 = Plot[x^3,{x,0,2}, PlotStyle-> {Pink,Thick}]

show concatenates

Show[p1,p2,p3]