︠1dbcee2f-181a-4874-b0fa-47a85ea01896i︠ %html
This worksheet will provide you with examples of code to:
This worksheet will provide you with examples of code to:
\nType in a function and a range of values (if $(-\infty,\infty)$, then put in two finite values). The code below will evaluate the function at the endpoints of your interval (hopefully showing one value is positive while the other is negative). Then it will compute the derivative and find where it is zero. If the derivative is never zero on the interval, then you immediately know that the function cannot have two zeros, otherwise Rolle's theorem would require that $f'=0$ somewhere on the interval.
︡3db3ffc5-866b-4668-9887-109a153197f1︡{"html": "Type in a function and a range of values (if $(-\\infty,\\infty)$, then put in two finite values). The code below will evaluate the function at the endpoints of your interval (hopefully showing one value is positive while the other is negative). Then it will compute the derivative and find where it is zero. If the derivative is never zero on the interval, then you immediately know that the function cannot have two zeros, otherwise Rolle's theorem would require that $f'=0$ somewhere on the interval.
"}︡ ︠bca9a491-08df-4c2e-955e-027ee87afaff︠ f(x)=x^3+3*x+2 xrange=(-10,10) Df=diff(f,x) html.table([ ["f",f(x)], ["f at left endpoint", f(xrange[0])], ["f at right endpoint", f(xrange[1])], ["f'",Df(x)==Df(x).factor()], ["f'=0",solve(Df(x)==0,x)], ["Graph",plot(f,xrange)] ]) ︡6150f112-7529-410f-bbfd-870d1c3919bc︡{"html": "\nf | \nx^{3} + 3 \\, x + 2 | \n
f at left endpoint | \n-1028 | \n
f at right endpoint | \n1032 | \n
f' | \n3 \\, x^{2} + 3 = 3 \\, x^{2} + 3 | \n
f'=0 | \n\\text{[\nx == -I,\nx == I\n]} | \n
Graph | \n![]() | \n
Recall that the mean value theorem guarantees the existence of a value $c$ in the interval $(a,b)$ such that $$f'(c)=\frac{f(b)-f(a)}{b-a}.$$ The code below will help you find that value of $c$.
︡9ad21743-ef25-45bb-8f18-1b2452a24e64︡{"html": "Recall that the mean value theorem guarantees the existence of a value $c$ in the interval $(a,b)$ such that $$f'(c)=\\frac{f(b)-f(a)}{b-a}.$$ The code below will help you find that value of $c$.
"}︡ ︠df964bb7-01e3-4e1f-8e69-366abbe71c39︠ f(x)=x^3-3*x xrange=(-3,3) Df=diff(f,x) a=xrange[0] #grab the left endpoint b=xrange[1] #grab the right endpoint m=(f(b)-f(a))/(b-a) #use m to simplify typing the average slope sol=solve(Df(x)==m,x) #find where f'(c)=m p=plot(f,xrange) #plot f p+=plot(f(a)+m*(x-a),xrange,color='green') #add the line connecting the endpoints sols=[] #Get rid of solutions not in [a,b], and graph them. for cp in sol: c=x.subs_expr(cp) if(c>a): if(c\nf | \nx^{3} - 3 \\, x | \n
f' | \n3 \\, x^{2} - 3 | \n
m=(f(b)-f(a))/(b-a) | \n6 | \n
c such that f'(c)=m | \n\\left[x = -\\sqrt{3}, x = \\sqrt{3}\\right] | \n
Graph | \n![]() | \n
The code can be much shorter, but then the output is not as nice.
︡07a16425-36b3-493d-8a1b-4edd0a1cfd98︡{"html": "The code can be much shorter, but then the output is not as nice.
"}︡ ︠e52e4191-aef4-4598-af90-0addef70fc38︠ f(x)=x^3-3*x xrange=(-3,3) Df=diff(f,x) a=xrange[0] b=xrange[1] m=(f(b)-f(a))/(b-a) sol=solve(Df(x)==m,x) sol ︡aad188ef-f18f-4e6c-becd-9b8f51f378b5︡{"stdout": "[x == -sqrt(3), x == sqrt(3)]"}︡ ︠c4bff321-149e-4283-9282-e16fc2721921i︠ %htmlTo compute antiderivatives (indefinite integrals), use the integrate command. Notice that the computer does not include the $+C$. You have to do that yourself.
︡e0d52cdd-17a8-4949-9d0b-80ea1e8bebe2︡{"html": "To compute antiderivatives (indefinite integrals), use the integrate command. Notice that the computer does not include the $+C$. You have to do that yourself.
"}︡ ︠51fc7347-58b9-4da9-8618-9a4df3d4ce26︠ integrate(x^2,x) ︡730ad452-19cb-413b-a897-4b43e3164422︡{"stdout": "1/3*x^3"}︡ ︠25972b39-4839-48bd-8222-bf1b8914e6c8︠ f=x^2-sqrt(x)+sin(4*x)-1/(1+x^2) integrate(f,x).show() ︡6c1adc90-e975-4734-81dc-904a05534b73︡{"html": "If you know the velocity of an object, then all you have to do is integrate to find the position. If the initial position is $s_0$ at time $t_0$, then you can replace each $s$ with $s_0$ and $t$ with $t_0$ to find the constant $C$ from integration.
︡a0911a79-8717-4a97-b444-1de9f5fc6c5e︡{"html": "If you know the velocity of an object, then all you have to do is integrate to find the position. If the initial position is $s_0$ at time $t_0$, then you can replace each $s$ with $s_0$ and $t$ with $t_0$ to find the constant $C$ from integration.
"}︡ ︠72205de7-141a-4bb5-a460-427437806f5f︠ var('t,s,v,C') v=t^2+exp(t) t0=0 s0=2 s=integrate(v,t)+C sol=solve(s(t=t0)==s0,C) html.table([ ["a",a], ["v",v], ["s = integral of v",s], ["[t0,s0]",[t0,s0]], ["Equation to find C",s0==s(t=t0)], ["s",s.subs_expr(sol[0])] ]) ︡5804a527-185c-42e9-9db3-82e89418b3bc︡{"html": "\na | \n-3 | \n
v | \nt^{2} + e^{t} | \n
s = integral of v | \n\\frac{1}{3} \\, t^{3} + C + e^{t} | \n
[t0,s0] | \n\\left[0, 2\\right] | \n
Equation to find C | \n2 = C + 1 | \n
s | \n\\frac{1}{3} \\, t^{3} + e^{t} + 1 | \n
If you know the acceleration at any time $t$, as well as the velocity and position at a single time $t$, then you can use the following code to find equations for the velocity and position. The table output at the end shows all the work involved.
︡9de9933a-f3e1-4615-b98d-5906efb0600c︡{"html": "If you know the acceleration at any time $t$, as well as the velocity and position at a single time $t$, then you can use the following code to find equations for the velocity and position. The table output at the end shows all the work involved.
"}︡ ︠34ed5e58-fe9f-440f-84ad-e3e3a70f045a︠ var('t,s,v,a,C,D') #we have to start by defining our variables a=t^2+exp(t) t0=0 s0=2 v0=3 #Find v first. v=integrate(a,t)+C solC=solve(v(t=t0)==v0,C) vn=v.subs_expr(solC[0]) #Now find s. s=integrate(vn,t)+D solD=solve(s0==s(t=t0),D) sn=s.subs_expr(solD[0]) #Now format the output to look really nice. html.table([ ["a",a], ["v = integral of a",v], ["[t0,v0]",[t0,v0]], ["Equation to find C",v0==v(t=t0)], ["v",vn], ["s = integral of v",s], ["[t0,s0]",[t0,s0]], ["Equations to find D",s0==s(t=t0)], ["s",sn] ]) ︡788ff83c-8118-4cdb-875a-4f200b708b34︡{"html": "\na | \nt^{2} + e^{t} | \n
v = integral of a | \n\\frac{1}{3} \\, t^{3} + C + e^{t} | \n
[t0,v0] | \n\\left[0, 3\\right] | \n
Equation to find C | \n3 = C + 1 | \n
v | \n\\frac{1}{3} \\, t^{3} + e^{t} + 2 | \n
s = integral of v | \n\\frac{1}{12} \\, t^{4} + D + 2 \\, t + e^{t} | \n
[t0,s0] | \n\\left[0, 2\\right] | \n
Equations to find D | \n2 = D + 1 | \n
s | \n\\frac{1}{12} \\, t^{4} + 2 \\, t + e^{t} + 1 | \n
The derivative gives us the slope of a tangent line. If you increase the $x$ value of a function from $x$ to $x+dx$, then how much does the $y$ value increase? The notation $\dfrac{dy}{dx}=f'(x)$ suggests that we can find a small change in $y$ by multiplying both sides by $dx$ to obtain the differential $$dy = f'(x)dx.$$ Think of $dy$ as a small change in $y$. Think of $dx$ as a small change in $dx$. The quantities $dy$ and $dx$ are called differentials. You can use differentials to estimate how much $y$ will change if $x$ changes by $dx$. The example below illustates how to do this. In high dimensional calculus, the differentials become vectors and the derivative becomes a matrix. This differential equation is the key equation needed to extend calculus to higher dimensions.
︡f4d046bb-7913-4b8d-a2f9-e42fbb8d2a58︡{"html": "The derivative gives us the slope of a tangent line. If you increase the $x$ value of a function from $x$ to $x+dx$, then how much does the $y$ value increase? The notation $\\dfrac{dy}{dx}=f'(x)$ suggests that we can find a small change in $y$ by multiplying both sides by $dx$ to obtain the differential $$dy = f'(x)dx.$$ Think of $dy$ as a small change in $y$. Think of $dx$ as a small change in $dx$. The quantities $dy$ and $dx$ are called differentials. You can use differentials to estimate how much $y$ will change if $x$ changes by $dx$. The example below illustates how to do this. In high dimensional calculus, the differentials become vectors and the derivative becomes a matrix. This differential equation is the key equation needed to extend calculus to higher dimensions.
"}︡ ︠92c2f6fe-9978-41cc-bb51-1d32f01fd58a︠ var('x,dx') f=x^2 dy=diff(f)*dx dy ︡7b90d421-7b0a-43e6-983d-2456c3a7b8ec︡{"stdout": "2*dx*x"}︡ ︠79b3acd8-0c29-43a3-be9a-28abcf62a08a︠ xvalue=2 dx=1/2 dy(x=xvalue,dx=dx) ︡561c7995-9859-45a1-847c-75bad8d23357︡{"stdout": "2"}︡ ︠98284601-af25-45be-834b-07920930310ei︠ %htmlThe picture below illustrates graphically how the differential comes from using the tangent line to approximate a function.
︡ed38f5b4-2686-4a97-a7da-95e56a069d30︡{"html": "The picture below illustrates graphically how the differential comes from using the tangent line to approximate a function.
"}︡ ︠095c57aa-27ac-4ca8-bfbd-2ec19e14da28︠ xrange=(x,xvalue-2*dx,xvalue+2*dx) p=plot(f,xrange) p+=point((xvalue,f(x=xvalue)),pointsize=30) p+=plot(f(x=xvalue)+diff(f)(x=xvalue)*(x-xvalue),xrange,color='red') p+=line([(xvalue,f(x=xvalue)),(xvalue+dx,f(x=xvalue))],rgbcolor='purple') p+=line([(xvalue+dx,f(x=xvalue)),(xvalue+dx,f(x=xvalue)+dy(x=xvalue,dx=dx))],rgbcolor='purple') p+=line([(xvalue,f(x=xvalue)),(xvalue,f(x=xvalue+dx))],rgbcolor='green') p+=line([(xvalue,f(x=xvalue+dx)),(xvalue+dx,f(x=xvalue+dx))],rgbcolor='green') p+=point((xvalue+dx,f(x=xvalue)+dy(x=xvalue,dx=dx)),rgbcolor='purple',pointsize=20) p+=point((xvalue+dx,f(x=xvalue+dx)),rgbcolor='green',pointsize=20) p.show() ︡d53ef9d1-6eca-4029-8a03-e8a0290e2753︡{"html": "The code below illustrates numerically how $dy$ is approximately the same as $\Delta y = f(x+dx)-f(x)$.
︡6f570f2a-2ab1-4c39-b6eb-0ab62c2542ee︡{"html": "The code below illustrates numerically how $dy$ is approximately the same as $\\Delta y = f(x+dx)-f(x)$.
"}︡ ︠8c584f00-8af0-4fb0-8a23-b374a0e809a5︠ var('x,dx') f(x)=x^2 xValue=1 dxValue=.01 Df=diff(f,x) dy=Df(x)*dx html.table([ ["f",f(x)], ["f'",Df(x)], ["dy",dy], ["dy evaluated at x and dx",dy(x=xValue,dx=dxValue).n()], ["f at the x value",f(xValue)], ["f at x+dx",f(xValue+dxValue)], ["The actual change in y",f(xValue+dxValue)-f(xValue)], ["The approximate change in y = dy",dy(x=xValue,dx=dxValue).n()], ]) ︡805aa9f4-da96-4c9f-9dd3-ec79dc93f41f︡{"html": "\nf | \nx^{2} | \n
f' | \n2 \\, x | \n
dy | \n2 \\, \\mbox{dx} x | \n
dy evaluated at x and dx | \n0.0200000000000000 | \n
f at the x value | \n1 | \n
f at x+dx | \n1.02010000000000 | \n
The actual change in y | \n0.0201000000000000 | \n
The approximate change in y = dy | \n0.0200000000000000 | \n
The linearization of $f(x)$ is just another name for the tangent line. The code below compute the linearization of $(1+x)^k$ for any real number $k$.
︡5eb7f4bf-9ccb-45ce-86db-318a50b102af︡{"html": "The linearization of $f(x)$ is just another name for the tangent line. The code below compute the linearization of $(1+x)^k$ for any real number $k$.
"}︡ ︠b54f3b73-a3d6-4f3a-b98d-243745c0f174︠ var('k') f(x)=(1+x)^k c=0 Df=diff(f,x) L(x)=f(c)+Df(c)*(x-c) L(x) ︡4836f758-8e6d-4cf9-a598-0f35b54a339c︡{"stdout": "k*x + 1"}︡ ︠59392a64-1eed-4479-b484-19820a9c2442i︠ %htmlIf you cannot find a limit because it is of the form $\dfrac{0}{0}$ or $\dfrac{\infty}{\infty}$, then L'Hopital's rule can often help. The idea is to take the derivative of the top and bottom separately, and then find the limit. If is exists, then the original limit equals the limit you found. You can repeat this as many times as needed, provide at each stage the limit is still of the form $\dfrac{0}{0}$ or $\dfrac{\infty}{\infty}$. The example below illustrates this process for $\dfrac{1-\cos(x)}{x^2}$. Notice that after putting in zero in the top and bottom in both the original and after taking derivatives, you get 0/0 in both cases. Only after 2 derivatives do you get 1/2.
︡bfad95ac-4feb-4bf1-9cba-4511af96ab35︡{"html": "If you cannot find a limit because it is of the form $\\dfrac{0}{0}$ or $\\dfrac{\\infty}{\\infty}$, then L'Hopital's rule can often help. The idea is to take the derivative of the top and bottom separately, and then find the limit. If is exists, then the original limit equals the limit you found. You can repeat this as many times as needed, provide at each stage the limit is still of the form $\\dfrac{0}{0}$ or $\\dfrac{\\infty}{\\infty}$. The example below illustrates this process for $\\dfrac{1-\\cos(x)}{x^2}$. Notice that after putting in zero in the top and bottom in both the original and after taking derivatives, you get 0/0 in both cases. Only after 2 derivatives do you get 1/2.
"}︡ ︠6fdf3d90-55d6-4da0-840b-08573efad014︠ f(x) = (1-cos(x))/x^2 c=0 num=numerator(f(x)) den=denominator(f(x)) html.table([ ["top",num,limit(num,x=c),"bottom",den,limit(den,x=c)], ["top",diff(num),limit(diff(num),x=c),"bottom",diff(den),limit(diff(den),x=c)], ["top",diff(num,2),limit(diff(num,2),x=c),"bottom",diff(den,2),limit(diff(den,2),x=c)], ]) limit(f(x),x=c) ︡806d4fde-38c8-4ab9-9a33-f924fda4c121︡{"html": "\ntop | \n-\\cos\\left(x\\right) + 1 | \n0 | \nbottom | \nx^{2} | \n0 | \n
top | \n\\sin\\left(x\\right) | \n0 | \nbottom | \n2 \\, x | \n0 | \n
top | \n\\cos\\left(x\\right) | \n1 | \nbottom | \n2 | \n2 | \n
You can also use this rule when $c=\infty$. In the example below, the second line gives $\dfrac{0}{1}=0$, so you not take 2 derivatives.
︡009bce8d-387a-4a75-b4a0-3cbbe692d796︡{"html": "You can also use this rule when $c=\\infty$. In the example below, the second line gives $\\dfrac{0}{1}=0$, so you not take 2 derivatives.
"}︡ ︠b9994e33-f29b-49ed-9b88-154e7da4d483︠ f(x) = log(x)/x c=infinity num=numerator(f(x)) den=denominator(f(x)) html.table([ ["top",num,limit(num,x=c),"bottom",den,limit(den,x=c)], ["top",diff(num),limit(diff(num),x=c),"bottom",diff(den),limit(diff(den),x=c)], ["top",diff(num,2),limit(diff(num,2),x=c),"bottom",diff(den,2),limit(diff(den,2),x=c)], ]) limit(f(x),x=c) ︡c99997eb-819c-4435-af54-86e1f64a4911︡{"html": "\ntop | \n\\ln\\left(x\\right) | \n+\\infty | \nbottom | \nx | \n+\\infty | \n
top | \n\\frac{1}{x} | \n0 | \nbottom | \n1 | \n1 | \n
top | \n-\\frac{1}{x^{2}} | \n0 | \nbottom | \n0 | \n0 | \n