from sympy import *
from dtumathtools import *
init_printing()
Solution Suggestion for HW2#
Assignment 2 in Mathematics 1b
By David Thomas Hart DTU Compute
If you have any questions/corrections/improvements - davidthart03@gmail.com
Note that the following answers are not representative of the “perfect” or “ideal” student solution. It is only meant to show correct solutions or at least example methods to the questions.
Problem 1#
A function \(f:\mathbb{R}\rightarrow\mathbb{R}\) is given by the expression
a)#
We want to find the approximating polynomial \(P_{4,f,x_0}\) with expansion point \(x_0=0\). In other words, the 4th order Taylor polynomial. We first need to find the derivatives evaluated at \(x_0=0\):
So, naturally when we use this in the definition of Taylor polynomials in Definition 4.2.1, we get
We can verify this:
x = symbols('x')
def P(f,x0,n):
return f.series(x,x0,n+1).removeO()
f = cos(3*x)
P(f,0,4)
![../_images/b5f37f1095563a7e4ebb5612ff20f410f6f9dadad537b2991d408dbe55dd1b2f.png](../_images/b5f37f1095563a7e4ebb5612ff20f410f6f9dadad537b2991d408dbe55dd1b2f.png)
Alternatively, one could use the well known Maclaurin series for \(\cos{(y)}\) and substitute \(y=3x\), giving the same thing.
b)#
The value of \(\cos(\frac12)\) can be approximated with this Taylor polynomial by using \(x=\frac16\), since our Taylor approximation is for \(\cos{(3x)}\), so \(3x=\frac{1}{2}\Rightarrow x=\frac{1}{6}\). Computing \(\cos{(\frac{1}{2})}\approx P_{4,f,0}(\frac{1}{6})\):
P(f,0,4).subs(x,1/6)
![../_images/dd5742e8accf9fe9e93c81ec5bca403fe8e367e4acaaaefe9028aac7457b0f7a.png](../_images/dd5742e8accf9fe9e93c81ec5bca403fe8e367e4acaaaefe9028aac7457b0f7a.png)
We will now estimate the error that is incurred here, for which we need:
Using the fact that \(\sin(t)\leq1\) for any \(t\), we estimate the error by use of the remainder term:
243./(factorial(5)*6**5)
![../_images/07b7df19782cc953fbf49b83df38660e0a2e1def23bb3950353ffe00b9943cfc.png](../_images/07b7df19782cc953fbf49b83df38660e0a2e1def23bb3950353ffe00b9943cfc.png)
So, the maximum incurred error is no larger than \(0.026\,\%\).
An even better error estimate is found by considering \(\xi\in[x_0,x]=[0,\frac16]\). This shows that \(\sin(3\xi)\in[\sin(0),\sin(\frac12)]\), and in this interval we have \(\sin(t)<t\). So, using \(\sin(3\xi)\leq\frac12\) we have:
243./(factorial(5)*6**5*2)
![../_images/062aff59356d667c966fea432d2ee62df4a7f67f8b217d757981178ccd92526e.png](../_images/062aff59356d667c966fea432d2ee62df4a7f67f8b217d757981178ccd92526e.png)
So, the maximum incurred error is no larger than \(0.013\,\%\).
As a check, we can find the absolute and relative errors of this approximation using a computer value for \(\cos(\frac12)\):
abs(cos(1/2)-P(f,0,4).subs(x,1/6)),100*abs(cos(1/2)-P(f,0,4).subs(x,1/6))/abs(cos(1/2))
![../_images/5187368afde20c1124dba2f316b7a481d8d8a3fafeef062106c7ec57c44fd3a2.png](../_images/5187368afde20c1124dba2f316b7a481d8d8a3fafeef062106c7ec57c44fd3a2.png)
So, \(|(\cos{(\frac{1}{2})}-P_{4,f,0}(\frac{1}{6}))|\approx2.16\cdot10^{-5}\) and the computer-evaluated percentage error is \(2.46\cdot10^{-3}\:\%\), which is within bounds of the estimated error above.
c)#
We are informed that a function \(g\) has the following properties:
We can use this to quite easily find \(P_{7,g,x_0}\) with expansion point \(x_0=0\). We know that odd \(n\) will have no contribution, and therefore:
Which can be reduced to:
This pattern looks quite similar to the function in question a), but instead of \(3^n\) and an alternating sign, it is \(2^n\). The function in question is \(g(x)=\cos{(2x)}\). We can double check:
g=cos(2*x)
P(g,0,7)
![../_images/51164b683888d255a929ecd72ab24bbf841a263ae45dd4294b07691d06fdc106.png](../_images/51164b683888d255a929ecd72ab24bbf841a263ae45dd4294b07691d06fdc106.png)
Problem 2#
Let \(B\) be the set \(B=\{(x_1,x_2)\in\mathbb{R}^2|x_1^2+x_2^2\leq1\wedge x_1\geq0\}\). This is the filled semicircle of radius \(r=1\) encompassing the 1st and 4th quadrant, \(\theta\in[-\frac{\pi}{2},\frac{\pi}{2}]\), if looking at the \((x_1,x_2)\)-plane. A function \(f:B\rightarrow\mathbb{R}\) is given by
To find the image of this function, we want to find the global maximum and minimum. The function consists of elementary functions that are differentiable everwhere and so there are no exceptional points. This means we only need to investigate the boundary and potential stationary points. The stationary point can be found manually quite simply or with the following:
x1,x2 = symbols('x1 x2')
V = Matrix([x1,x2])
f=x1**2*x2**2+3*x1*x2+x2-4
g = dtutools.gradient(f,V)
eq = Eq(g[0],0),Eq(g[1],0)
solve(eq)
![../_images/1a167c56a4aabfeb8a339752d7031931bb9fc969b9ee1271c31a41717be209f5.png](../_images/1a167c56a4aabfeb8a339752d7031931bb9fc969b9ee1271c31a41717be209f5.png)
So, the only stationary point is \(\left(-\frac{1}{3},0\right)\). This has the function value:
f.subs({x1:-1/3,x2:0})
![../_images/c3d4583d6d425eebcc527a15f8d9120dd82bd8e16aad36e9cdc8fcf1028353b3.png](../_images/c3d4583d6d425eebcc527a15f8d9120dd82bd8e16aad36e9cdc8fcf1028353b3.png)
To investigate the boundary, we can make some restrictions on the functions, using parametrisation. There are two segments of the boundary, notably the diameter and the semicircular arc. They can each be parameterised in the following ways:
We start by investigating the diameter:
The lowest value this takes in when \(u=-1\) and the highest is when \(u=1\), giving \(-5\) and \(-3\) respectively. Then for the semicircular arc:
We can find the maxima and minima along this restriction:
u = symbols('u',real=True)
fa = cos(u)**2*sin(u)**2+3*cos(u)*sin(u)+sin(u)-4
S = solve(fa.diff(u),u)
N(S[0]),N(S[1]),N(S[2]),N(S[3])
![../_images/8ee147cd598639c8fc72530261016d68718fff8a2ab2e5080fbaea20c7813d1c.png](../_images/8ee147cd598639c8fc72530261016d68718fff8a2ab2e5080fbaea20c7813d1c.png)
Where only \(-0.933\) and \(0.867\) are within our interval of \(u\). We can visualise this:
p = plot(fa,(u,-pi/2,pi/2),show=False,title="Height along arc boundary")
p[0].line_color = 'purple'
p.show()
![../_images/f11f6ce7774249531a7c5373d58cb201ddc8f0aa451847c08b63d2a00b6ec803.png](../_images/f11f6ce7774249531a7c5373d58cb201ddc8f0aa451847c08b63d2a00b6ec803.png)
We can also add the boundaries to the 3D plot and visualise further:
pf = dtuplot.plot3d(f,(x1,0,1),(x2,-1,1),show=False,rendering_kw={"alpha":0.4,"color":"green"})
l1 = dtuplot.plot3d_parametric_line(0,u,f.subs({x1:0,x2:u}),(u,-1,1),
use_cm=False,show=False,rendering_kw={"color":"purple","linewidth":3})
l2 = dtuplot.plot3d_parametric_line(cos(u),sin(u),f.subs({x1:cos(u),x2:sin(u)}),(u,-pi/2,pi/2),
use_cm=False,show=False,rendering_kw={"color":"purple","linewidth":3})
combined = (pf+l1+l2)
combined.camera = {"azim" : -120, "elev" : 30}
combined.legend = False
combined.show()
![../_images/24f346106ef511cd8bb9b4db88ec08178b48272e1401090b09394f509520c111.png](../_images/24f346106ef511cd8bb9b4db88ec08178b48272e1401090b09394f509520c111.png)
We can use the semicircular arc boundary minimum and maximum \(u\) values to find the height at those points.
fa.subs(u,N(S[1])),fa.subs(u,N(S[2]))
![../_images/983eb7aef230a31cc7dc606e91a07f11b7319b78fd2c544f8d796ccc4e73dab9.png](../_images/983eb7aef230a31cc7dc606e91a07f11b7319b78fd2c544f8d796ccc4e73dab9.png)
So, our maximum function value along the arc is \(-1.51\) and the minimum is \(-6.01\). Not only are this the extrema along the arc, but the lower bound here is the global minimum of \(f\) and the upper bound is the global maximum of \(f\). Therefore:
At least rounded to three significant figures.
Problem 3#
A function \(f:\mathbb{R}^2\rightarrow\mathbb{R}\) is given by
a)#
To determine the stationary points of \(f\), we need to set the gradient equal to \(\boldsymbol{0}\) and solve for \(x_1\) and \(x_2\).
x1,x2 = symbols('x1 x2')
f=x1**2-2*x1+3*x2**5-5*x2**3
g=Matrix([diff(f,x1),diff(f,x2)])
eq = Eq(g[0],0),Eq(g[1],0)
solve(eq)
![../_images/ad1cd82fefeea0a0fdaf6e31212f67f89f8cf5ced7c1747828c55ebebd9b3317.png](../_images/ad1cd82fefeea0a0fdaf6e31212f67f89f8cf5ced7c1747828c55ebebd9b3317.png)
b)#
To determine whether the stationary points are local minima, maxima or just saddle points, we can use the Hessian matrix evaluated at the stationary points and use Theorem 5.2.4.
def HessianEval(f,y1,y2):
V = Matrix([x1,x2])
return hessian(f,V).subs({x1:y1,x2:y2})
HessianEval(f,1,-1),HessianEval(f,1,0),HessianEval(f,1,1)
The point at \((x_1,x_2)=(1,-1)\) is a saddle point as we have both a positive and a negative eigenvalue. We also see that the point \((x_1,x_2)=(1,1)\) is a strict local minimum as there are only positive eigenvalues. We need to do a special investigation on the last point \((1,0)\) since \(0\) is an eigenvalue in that case and the Hessian matrix thus does not give us any conclusion.
We restrict the function to a chosen straight line \(x_1=1\):
h=f.subs(x1,1)
h
![../_images/34fe70895b487757c950f7f81b5b52ec8788d8563029b13fa6a3bd6d0c54dd73.png](../_images/34fe70895b487757c950f7f81b5b52ec8788d8563029b13fa6a3bd6d0c54dd73.png)
We find all stationary points along this restriction:
k=diff(h,x2)
solveset(Eq(k,0))
![../_images/3761f6120e2945a9a161bc8215675f9797efa7aca2d4ef97fd10dd448ff4376a.png](../_images/3761f6120e2945a9a161bc8215675f9797efa7aca2d4ef97fd10dd448ff4376a.png)
There are three stationary points along this restriction, the one at \(x_2=0\) coinciding with the \((1,0)\) we are investigating. The function value at this point as well as of chosen points within the subintervals on either side are computed:
h.subs(x2,-1/2),h.subs(x2,0),h.subs(x2,1/2)
![../_images/49a82c083d43866d4b8183dc0b746d9eb8938afbc873a96bb102f2948864ebe1.png](../_images/49a82c083d43866d4b8183dc0b746d9eb8938afbc873a96bb102f2948864ebe1.png)
Since this restriction is continuous and with no exceptional points, and since there are no further stationary points within the subintervals \([-\frac12,0[\) and \(]0,\frac12]\), we conclude that the restriction is locally increasing from \(x_2=0\) in the positive direction (for greater values of \(x_2\)) and locally decreasing in the negative direction (for smaller values of \(x_2\)). The stationary point is thus a saddle point along this restriction, and hence \((1,0)\) is a saddle point on \(f\).
The function values at all points are:
f.subs({x1:1,x2:-1}),f.subs({x1:1,x2:0}),f.subs({x1:1,x2:1})
![../_images/ba2bd561ea3f4735131f47416f405becde6f3019c3b2437f7c7bf9ccbf071933.png](../_images/ba2bd561ea3f4735131f47416f405becde6f3019c3b2437f7c7bf9ccbf071933.png)
We can quickly plot them on \(f\):
SPs = {(1,-1),(1,0),(1,1)}
Points = [Matrix([x0,y0,f.subs({x1:x0,x2:y0})]) for (x0,y0) in SPs]
pS1=dtuplot.scatter(Points,show=False,rendering_kw={"s" : 100,"color":"red"})
pf = dtuplot.plot3d(f,(x1,0,2),(x2,-1.2,1.2),show=False,rendering_kw={"alpha":0.8,"color":"green"})
combined = (pS1+pf)
combined.camera = {"azim" : 150, "elev" : 20}
combined.legend = False
combined.title = "Function and stationary points"
combined.show()
![../_images/ff6a77aeb95d009151c72b14e365e7c14ff4ac4e25be84d9ea8cbb782ca2f486.png](../_images/ff6a77aeb95d009151c72b14e365e7c14ff4ac4e25be84d9ea8cbb782ca2f486.png)
c)#
We can now plot the function \(f\) along with its second degree Taylor polynomial with the three different expansion points, corresponding to the stationary points.
def evaluateFunction(f,x,x0):
for i in range(len(x)):
f = f.subs({x[i]:x0[i]})
return f
def taylorN(f,x,x0):
N = len(x)
const = evaluateFunction(f,x,x0)
J = Matrix([f]).jacobian(x).T
J0 = evaluateFunction(J,x,x0)
first = J0.T*(x-x0)
H = hessian(f,x)
H0 = evaluateFunction(H,x,x0)
second = 1/2*(x-x0).T*H0*(x-x0)
Pk = simplify(Matrix([const]) + first + second)[0]
return Pk
SPs=nonlinsolve
P1 = taylorN(f,V,Matrix([1,-1]))
P2 = taylorN(f,V,Matrix([1,0]))
P3 = taylorN(f,V,Matrix([1,1]))
pP1 = dtuplot.plot3d(P1,(x1,0,2),(x2,-2,0),show=False,rendering_kw={"alpha":0.7,"color":"purple"})
pP2 = dtuplot.plot3d(P2,(x1,0,2),(x2,-1,1),show=False,rendering_kw={"alpha":0.7,"color":"purple"})
pP3 = dtuplot.plot3d(P3,(x1,0,2),(x2,0,2),show=False,rendering_kw={"alpha":0.7,"color":"purple"})
pf1 = dtuplot.plot3d(f,(x1,0,2),(x2,-2,0),show=False,rendering_kw={"alpha":0.4,"color":"green"})
pf2 = dtuplot.plot3d(f,(x1,0,2),(x2,-1,1),show=False,rendering_kw={"alpha":0.4,"color":"green"})
pf3 = dtuplot.plot3d(f,(x1,0,2),(x2,0,2),show=False,rendering_kw={"alpha":0.4,"color":"green"})
pS1 = dtuplot.scatter(Matrix([1,-1,1]),show=False,rendering_kw={"s" : 100,"color":"red"})
pS2 = dtuplot.scatter(Matrix([1,0,-1]),show=False,rendering_kw={"s" : 100,"color":"red"})
pS3 = dtuplot.scatter(Matrix([1,1,-3]),show=False,rendering_kw={"s" : 100,"color":"red"})
combined1 = (pf1+pP1+pS1)
combined1.camera = {"azim" : 190, "elev" : 10}
combined1.legend = False
combined1.title = "$x_0=(1,-1)$"
combined1.show()
combined2 = (pf2+pP2+pS2)
combined2.camera = {"azim" : 180, "elev" : 10}
combined2.legend = False
combined2.title = "$x_0=(1,0)$"
combined2.show()
combined3 = (pf3+pP3+pS3)
combined3.camera = {"azim" : 160, "elev" : 10}
combined3.legend = False
combined3.title = "$x_0=(1,1)$"
combined3.show()
![../_images/e0e011dd86ac55453f959ebfa9a91ef6cdeb1be736bf1a279d9b3fd8c58d8980.png](../_images/e0e011dd86ac55453f959ebfa9a91ef6cdeb1be736bf1a279d9b3fd8c58d8980.png)
![../_images/50354ba8ec2f87f1a063af47597d34ea098d349f94312a125c06d596837e63c0.png](../_images/50354ba8ec2f87f1a063af47597d34ea098d349f94312a125c06d596837e63c0.png)
![../_images/7611b92ba5ae30cad9662fac161a4ef9be2f01ecc8715ce036203f080736cbb6.png](../_images/7611b92ba5ae30cad9662fac161a4ef9be2f01ecc8715ce036203f080736cbb6.png)
It all looks quite neat. Nice!
Problem 4#
A function \(f:[0,5]\rightarrow\mathbb{R}\) is given by the expression \(f(x)=2x+3\).
a)#
We want to find the Riemann sum over the interval \([0,5]\) with 30 subintervals, starting with a left Riemann sum.
f = 2*x+3
def LeftSum(f,a,b,J):
w = (b - a)/J
x_l = a
result = 0
for i in range(J):
x_r = x_l + w
result += f.subs(x,x_l)
x_l = x_r
result *= w
return result
LeftSum(f,0,5,30)
![../_images/c1b9e4ba9f1ae262b7b3259b0584f32242dc7fb0edde29cba5761afa06fefa08.png](../_images/c1b9e4ba9f1ae262b7b3259b0584f32242dc7fb0edde29cba5761afa06fefa08.png)
def RightSum(f,a,b,J):
w = (b - a)/J
x_l = a
result = 0
for i in range(J):
x_r = x_l + w
result += f.subs(x,x_r)
x_l = x_r
result *= w
return result
RightSum(f,0,5,30)
![../_images/89b0caf7e0c5b228eae325ae15e8f73b0f4b5b42de572c045172bf5743daf42f.png](../_images/89b0caf7e0c5b228eae325ae15e8f73b0f4b5b42de572c045172bf5743daf42f.png)
b)#
In this question, we need to find both the left and right Riemann sums over the previous interval for this specific function, but generalised to \(n\) subintervals. Generally, for a left Riemann sum:
Where \(w=\frac{b-a}{n}\). Which for this specific function, is given by:
The exact same can be done for the right Riemann sum, giving:
c)#
The expression for the errors of these approximations can be found in the following way:
And the right Riemann sum turns out to have the same error, just as an overestimate instead of underestimate.
d)#
It does in fact not matter which point we choose in our subinterval due to the following: