programs
Logic Games
Challenges to programming which can be “circumvented” using logic commands
Thinking about the program
Working around the problem
Lateral Thinking Example: Cake Cut
Can easily cut cake in to 8 even pieces (4 cuts) …
How can we get 8 even pieces with 3 cuts?
ahem …
Lots of ways to get around things
Projectile Motion – tend issue
Goal was to have the projectile plot STOP when the object reaches the ground again (y = 0)
Calculate tend from quadratic equation and limit the range of the time vector (t = 0: step: tend)
• do while • for + if • calculate to t = 1,000 and then “cut” the vector • calculate to t = 1,000 and then plot only to y = 0
while • variant on do-loop • condition is “while this is satisfied”
n = 10 ; f = n ; while n > 1
n = n-1 ; f = f*n ;
end disp(['n! = ’, num2str(f)])
factorials. 6! = 6 * 5* 4* 3* 2 * 1
apply the while construct here to avoid using the quadratic
equation to find tend
Compare some Approaches
tend from quadratic equation is probably the
most “elegant” of the options
(but they all work)
Logic Commands
Reduce things to TRUE vs. NOT-TRUE
= = (exactly equal to) ~= (not equal to) <= (less than or equal to)
Can we use these in a subtle manner? – capitalizing on the output (1 for true, 0 for false)
Recall ….
• <, >, ==, ~=
• Command window: x = 5; y = 1; x<y
logical 0
derp.
how is this useful?
Challenge Problem
Code to plot [ sin(x)/x ] from -4pi to + 4pi
This function shows up in a lot of engineering projects
• radar systems • filtering of signals • probability theory • Fourier transforms
what problems does this code have?
the old “divide by zero” issue …
classic
OK – fix it. (i.e. how can we use a logic structure to repair it?)
Ran “fine” without – but we couldn’t operate on y at all because of NaN
Now is good
-15 -10 -5 0 5 10 15 -0.4
-0.2
0
0.2
0.4
0.6
0.8
1
Challenge Problem
• Plot [ tan(x) ] from -3/2 pi to + 3/2 pi
• Seems simple enough.
I love San Francisco
Looks really easy to write …
Looks like poo ….
-5 -4 -3 -2 -1 0 1 2 3 4 5 -6
-4
-2
0
2
4
6 10 15
We “know” tan(x) and this isnt’ it
-5 -4 -3 -2 -1 0 1 2 3 4 5 -6
-4
-2
0
2
4
6 10 15
What is the problem with this code and how can we fix it?
-5 -4 -3 -2 -1 0 1 2 3 4 5 -6
-4
-2
0
2
4
6 10 15
goal is to use logic structures ”inline” with the code
Share out ….
Education is a crucible for your transformation
as a result … you should see world differently
scribbles evolve
• keep at it