 CoCalc Public FilesLabs / Lab4 / Lab 4.ipynb
Author: Dzoara Ruiz
Views : 46
Compute Environment: Ubuntu 20.04 (Default)

# Rates of Change

The average speed of a moving object over some time interval is the distance between positions divided by the time it took to get from one position to the next. More generally, the average rate of change of a function of time is the change in the value of the function divided by the length of the time interval.

In :
# Code for weird_function(), which will be used in the following exercise:
def weird_function():
pieces = [sin(x), cos(x), arctan(x), ln(x), sqrt(x), exp(x)]
f(x)=prod([choice(pieces) for i in srange(5)]) + prod([choice(pieces) for i in srange(5)])
return f


Exercise 1. Evaluate the cell in your turn-in sheet that contains the function weird_function() and run the command h(x)=weird_function() to create a random function called $h(x)$.

In :
h(x)=weird_function()

In :
h(x)

sqrt(x)*arctan(x)*cos(x)*e^(2*x) + sqrt(x)*e^(3*x)*sin(x)

Exercise 2. Compute the average rate of change of $h(x)$ between $x = 3.5$ and $x = 5.5.$

In :
h(3.5)
h(5.5)

-2.41032357770457e7
In :
(h(5.5)-h(3.5))/2

-1.20384601651182e7
In :
dh=(h(5.5)-h(3.5))
dx=5.5-3.5
dh/dx

-1.20384601651182e7

We know the average rate of change of the function between $x = 3.5$ and $x = 5.5$. Now, we want its rate of change at $x = 3.5$.

Exercise 3. Create a list of average rates of change for $h(x)$ for $\Delta x$ values of 1, 0.1, 0.01, and 0.001. That is find the average rate of change between $x=3.5$ and $x+\Delta x$. (You can do this by hand or use a loop.) Use the result to estimate the function’s rate of change at $x = 3.5$. Why do you think the number you gave is the rate of change at that value of $x$? Please make sure you get decimal answers.

In :
list3=[]
dXlist=[1,0.1,0.01,0.001,0.00001,0.000001]
for dX in dXlist:
list3.append((h(3.5+dX)-h(3.5))/dX)
print(list3)

[-1.49114143308672e6, -178059.452419901, -146178.117651879, -143368.536319776, -143063.337517015, -143060.566453642]

The rate of change of a function at a point is called its instantaneous rate of change at that point.

Exercise 4. Estimate the instantaneous rate of change at another point (as you did in Exercise 3).

In :
list4=[]
dXlist=[1,0.1,0.01,0.001,0.00001,0.000001]
for dX in dXlist:
list4.append((h(9.5+dX)-h(9.5))/dX)
print(list4)

[-1.35972091760917e14, -1.18637700543916e13, -9.26646977109924e12, -9.04098760759570e12, -9.01653098889160e12, -9.01630896264648e12]

At this point, you might be wondering why we bother with different values of $\Delta x$ at all. Why not just pick a small value of $\Delta x$ and always use that to compute rates of change? The answer won’t be mathematically precise, but real-world measurements always have some error anyway. The following series of exercises will explore this question.

Exercise 5. Find the average rates of change for the function $f(t) = t^3-3t^2+4t+1$ at $t = 1$ using 1, 0.1, 0.01, and 0.001 as your values of $\Delta t$. Then, estimate the function's rate of change at $t = 1$. Feel free to use more $\Delta t$ values if you want.

In :
var("t")
f(t)=t^3-3*t^2+4*t+t
dtlist=[1.,0.1,0.01,0.001,0.0001,0.00001]
for dt in dtlist:
df=f(1+dt)-f(1)
print(dt,df/dt)

1.00000000000000 3.00000000000000 0.100000000000000 2.01000000000000 0.0100000000000000 2.00010000000002 0.00100000000000000 2.00000099999986 0.000100000000000000 2.00000000999623 0.0000100000000000000 2.00000000005751

Exercise 6. Repeat this procedure for $g(t) = t^3-4t^2+6t+2$. What is this function's rate of change at $t = 1$?

In :
var("t")
f(t)=t^3-4*t^2+6*t+2
dtlist=[1.,0.1,0.01,0.001,0.0001,0.00001]
for dt in dtlist:
df=f(1+dt)-f(1)
print(dt,df/dt)
#t=1 is 1

1.00000000000000 1.00000000000000 0.100000000000000 0.910000000000011 0.0100000000000000 0.990100000000105 0.00100000000000000 0.999001000001165 0.000100000000000000 0.999900010008048 0.0000100000000000000 0.999990000138950

Exercise 7. Drawing on your results, briefly explain why limits are necessary when computing derivatives. Why don't we just pick a small value of $\Delta t$ and always use that?

In [ ]:
#There needs to be limits when computing derivatives because it allows us to get as close to the value in order to be more percise.


## Symbolic Calculations in Sage

One of Sage’s most powerful features is its ability to do symbolic computations – calculations with variables rather than numbers. Such calculations require you to use a particular kind of variable called a symbolic variable. Symbolic variables can be manipulated algebraically without having numerical values. The variable $x$ is a built-in symbolic variable.

Exercise 8. Compute $x^3x^7$.

In :
x^3*x^7

x^10

Exercise 9. Find the type of $x$.

In :
type(x)

<class 'sage.symbolic.expression.Expression'>

The variable $x$ is a built-in symbolic variable. To use other symbolic variables, like $N$ or $P$, you'll need to create them. This is called declaring the variables and is done using the var function. This function takes a string of variable names, separated by commas, as input and generates the appropriate symbolic variables.

In :
var("a")

a
In :
(a^3)/a

a^2

Note that Sage assumes that $a \neq 0$. Symbolic variables don't have to be single letters.

In :
var("sheep, rabbits")

(sheep, rabbits)
In :
2*sheep+3*rabbits

3*rabbits + 2*sheep

Once a symbolic variable has been declared, you can use it in subsequent calculations without re-declaring it until you close your worksheet.

In :
a*a

a^2

The command factor allows you to factor polynomials.

In :
factor(a^2 + 5*a + 6)

(a + 3)*(a + 2)

Exercise 10. Factor $x^2 + 7x + 6$.

In :
factor(x^2+7*x+6)

(x + 6)*(x + 1)

Exercise 11. Factor $k^2 - 5k + 6.$

In :
var("k")
factor(k^2-5*k+6)

(k - 2)*(k - 3)

Plotting uses symbolic variables, so if you want to plot a function of a variable other than $x$, you have to declare it before using it.

In :
var("p")
plot(log(p), (p,0,4)) Exercise 12. Plot the function $f(n) = 2^n$ using $n$ as the independent variable, for values of $n$ between -30 and 30. You don't need to define the function before plotting it.

In :
var("n")
plot((2^n),(n,-30,30)) Once you assign a numeric value to a symbolic variable, it’s no longer a symbolic variable. Instead, it's now a number. A common plotting error results from assigning a value to some variable, usually $x$, and later using that variable in a plot. The tell-tale symptom of this problem is a graph consisting of one or more horizontal lines. Execute the cell below to see the issue.

In :
x=2
plot(x^3, (x,-3,3), color="red") Exercise 13. Why is the horizontal line $y=2$ rather than some other value of $y$?

In [ ]:
#y=2^3 which is y=8 being plot on the y axis it is running the number instead of the symbolic varibale 5


To fix this problem, just declare $x$ to be a symbolic variable again.

In :
var("x")
plot(x^3, (x,-3,3), color="red") In [ ]: