Some sample questions for test 1.

We need

using MTH229

calculator

Evaluate each expression. For each, write the answer and the julia commands you used to produce the answer:

10^9 / 8*7 - 6 + 5
8.74999999e8
sin(pi/3) * sin(pi/4)^2 * asin(pi/5)
0.2941844678207861
cosd(75) - cos(75), cos(75*pi/180) - cos(75)
(-0.6629322246222286,-0.6629322246222286)
exp(1/2 * (3-1)^2), e^(1/2*(3-1)^2)
(7.38905609893065,7.38905609893065)

*

$$~ \frac{\sin(x) + \cos(x)}{\sin(x) - \cos(x)}, \text{ when } x = 1. ~$$
x = 1
(sin(x) - cos(x)) / (sin(x) - cos(x))
1.0

*

$$~ \sin^2 \left(\frac{x - 2\pi}{x + 2\pi}\right) \text{ when } x = \pi/4. ~$$
x = pi/4
inner = (x-2pi) / (x + 2pi)
sin(inner)^2
0.4923799093888391

Order of operations

Which of these three expressions will be different from the other two?

2/3/4, (2/3)/4, 2/(3/4)  # left-right associativity, so last one
(0.16666666666666666,0.16666666666666666,2.6666666666666665)
4^3^2, (4^3)/2, 4^(3^2)   # powers have right-left associativity, so middle
(262144,32.0,262144)
2 - 3 - 4, (2-3)-4, 2 - (3-4)  # most things-like subtraction-associate left right, so right one
(-5,-5,3)

Integers can cause some issues.

Are these expressions identical

ANS: 2^(-1) is an error. Integer bases and negative exponents are not defined (as the answer is expected to be an integer, not a rational, for technical reasons.)

ANS: Yes (at least on 64 bit machines). This is due to integer wraparound, rather than checking for overflow. (Technical, and not really essential to know.)

Floating point can cause small issues.

Are these expressions identical?

ANS: No. Try it. Why? You can't express .1, .2 and .3 exactly in floating point

ANS: No, not quite, but super close.

ANS: Yes

Defining functions

For each function, write a function in Julia and compute $f(1/2)$. For each, your answer should have the command to define the function and the value at $1/2$.

f(x) = x^4 - 4x^2 + 2
f(1/2)
1.0625
f(x) = sqrt(x / (x + 1))
f(1/2)
0.5773502691896257
f(x) = log(x + 1) - x  ## NOT ln(x +1)!!!
f(1/2)
-0.09453489189183562
f(x) = acos(x)  # not cos^(-1)(x) which won't work at all!
f(1/2)
1.0471975511965979

*

$$~ f(x) = \begin{cases} x^2 & x < 1\\ x & \text{otherwise} \end{cases} ~$$
f(x) = x < 1 ? x^2 : x
f(1/2)
0.25
x0,y0 = -1, -pi
x1, y1 = 2, pi
m = (y1 - y0) / (x1 - x0)
f(x) =  y0 + m * (x - x0)  # point-slope form!
f(1/2)
0.0

Plotting

f(x) = x * (x-1) / (x-2) / (x-3)
plot(f, -10, 10)

Can see VAs at 2, 3; HA seems clear and is 1

f(x) = cos(x) * cosh(x)
plot(f, 1, 5)
g(x) = 1
plot!(g, 1, 5)

Looks lke 1 out near 4.7.

f(x) = sin(x)
g(x) = x - x^3/6
plot(f, -pi/2, pi/2)
plot!(g, -pi/2, pi/2)

Does this help? Well let's plot the difference:

h(x) = f(x) - g(x)
plot(h, -pi/2, pi/2)

Since h is increasing it can only cross $0$ once, though it is hard to tell from a graph.

f(x) = x^5 - x + 1
plot(f, -5, 5)

Well, clearly if $f$ is decreasing, it must happen between $-2.5$ and $2.5$:

plot(f, -2.5, 2.5)

Err, between -1 and 1!

plot(f, -1, 1)

That's better. It is between $[-0.67, 0.67]$.

p1(x) = x < 5 ? 40 : 40 + 2(x-5)
p2(x) = 75
plot(p1, 0, 20)
plot!(p2, 0, 20)

Darn, not big enough. Let's adjust. It looks like for sure they will cross by $x=30$:

plot(p1, 0, 30)
plot!(p2, 0, 30)

And voila! Around 22.7