Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 407
reset #Exercise 1. Make a function convert_angle(t, p) that takes an angle t and parameter p. Zero value of p indicates that the angle t is in degrees, and the function should return the value of the angle in radians; value p = 1 indicates that the angle t is in radians, and the function should return the measure of this angle in degrees. def convert_angle(t,p): if p==0: angle=t*(pi/180) else: angle=t*(180/pi) return angle
<function reset at 0x7f38ea66e0c8>
res=convert_angle(90,0);res.n()
1.57079632679490
convert_angle(pi,1)
180
######################## #Exercise 4. Make a figure similar to Fig. 10.4 in Chapter 10 for the sum cos(3 Pi*t)+cos(2.7 Pi*t). #The exercise is similar to the example about beats in Chapter 10. f(t)=2*cos(5.7*pi*t/2)*cos(.15*pi*t) g(t)=2*cos(.15*pi*t) p=plot(f(t),-10,10,figsize=[6,5]) q=plot(g(t),-10,10,figsize=[6,5],color='red') r=plot(-g(t),-10,10,figsize=[6,5],color='red');p+q+r
reset
<function reset at 0x7f88094c20c8>
######################## #Exercise 6. Make a CAS function acoef(f(x), n) that takes two inputs, a piecewise differentiable 2π-periodic function f and an integer n and returns a list of n+1 Fourier coefficients a[k], k=0, 1, ..., n, of the function f. #Use the formula for a[k] calculation in Chapter 10. def acoef(f,n,t0,T): L=[integral(f(t)*cos(k*t),t,-pi,pi) for k in range(n) ] return [N(c/pi) for c in L]
f(t)=t^2 acoef(f,6,-pi,pi)
[6.57973626739290, -4.00000000000000, 1.00000000000000, -0.444444444444444, 0.250000000000000, -0.160000000000000]
#My example f(t)=t^2 acoef(f,6,-pi,pi)
[6.57973626739290, -4.00000000000000, 1.00000000000000, -0.444444444444444, 0.250000000000000, -0.160000000000000]
#Your example: g(t)=t for 0<t<=2*pi acoef(t,4,0,2*pi)
[0.000000000000000, 0.000000000000000, 0.000000000000000, 0.000000000000000]
####################### #Exercise 7. Make a CAS function bcoef(f(x), n) that takes two arguments, a piecewise differentiable 2π-periodic function and an integer n and returns a list of n Fourier coefficients b[k], k=1, 2, ..., n, of the function f. #Use the formula for b[k] calculation in Chapter 10. #Similar to Exercise 6. def bcoef(f,n,t0,T): L=[integral(f*sin(k*t),t,t0,T) for k in range(n+1) ] return [l/pi for l in L] g(t)=t bcoef(g,6,-pi,pi)
[0, 2, -1, 2/3, -1/2, 2/5, -1/3]
####################### #Exercise 8. Consider the 2π-periodic function f(t) = abs(t), t in [-Pi, Pi] (the triangular wave function). #(a) Find three partial sums S__n, n=3, 4, 6, of the Fourier series for this function. #(b) Make three figures, each with the pairs of graphs f and one of the partial sums S__n, n=3, 4, 6, over three periods. #Be careful, the formula for the function for t beyond the interval [-pi,pi] is different from abs(t).
#a f(t)=abs(t) f1(t)=abs(t-2*pi) f2(t)=abs(t-4*pi) L=acoef(f,7,-pi,pi) S_3(t)=L[0]/2+sum(L[k]*cos(k*t) for k in range(1,4)) S_4(t)=L[0]/2+sum(L[k]*cos(k*t) for k in range(1,5)) S_6(t)=L[0]/2+sum(L[k]*cos(k*t) for k in range(1,7))
L
[3.14159265358979, -1.27323954473516, 0.000000000000000, -0.141471060526129, 0.000000000000000, -0.0509295817894065, 0.000000000000000]
#b s=plot(f,-pi,pi,color='red',figsize=[4,3]) #s_p3=piecewise(-pi<=t<=pi,f(t),pi<t<=3*pi,f(t-2*pi),3*pi<t<=5*pi,f(t-4*pi)) s1=plot(f1,pi,3*pi,color='red',figsize=[4,3]) s2=plot(f2,3*pi,5*pi,color='red',figsize=[4,3]) p=plot(S_3,-pi,5*pi,figsize=[4,3]) q=plot(S_4,-pi,5*pi,figsize=[4,3]) r=plot(S_6,-pi,5*pi,figsize=[4,3])
s+s1+s2+p s+s1+s2+q s+s1+s2+r
####################### #Exercise 10. #(a) Construct the Fourier series for the function g(t) = sin(t) for 0<=t<pi, g(t)=0 for pi<= t < 2*Pi. #Directions: Use your functions in Exercise 6 and Exercise 7 to find several Fourier coefficients a[k] and b[k]. By visual inspection, determine the pattern and write the Fourier series. #(b) Plot the function and one of its partial Fourier series in one figure.
reset
<function reset at 0x7f88094c20c8>
def acoef(f,n,t0,T): L=[integral(f(t)*cos(k*t),t,-pi,pi) for k in range(n) ] return [N(c/pi) for c in L]
g(t)=
A=acoef(g,3,0,pi);A
[0.000000000000000, 0.000000000000000, 0.000000000000000]
B=
#piecewise? var('t') s=piecewise([([0,pi],sin(t)),((pi,2*pi),0)]) #Just for plotting
t
f_plot=plot(s,(t,0,2*pi),color='red',figsize=[4,2]);f_plot
# Integration of a piecewise function creates a problem: #test=acoef(s,3,0,2*pi);test
# To overcome the problem, I used the specific feature of the function: it is zero for pi<=t<=2*pi. A=acoef(s,3,0,2*pi);A
Error in lines 1-1 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1191, in execute flags=compile_flags), namespace, locals) File "", line 1, in <module> File "", line 2, in acoef File "/ext/sage/sage-8.9_1804/local/lib/python2.7/site-packages/sage/misc/functional.py", line 753, in integral return x.integral(*args, **kwds) File "sage/symbolic/expression.pyx", line 12680, in sage.symbolic.expression._eval_on_operands.new_f (build/cythonized/sage/symbolic/expression.cpp:66926) return f(ex, *new_args, **kwds) File "/ext/sage/sage-8.9_1804/local/lib/python2.7/site-packages/sage/functions/piecewise.py", line 822, in integral return F(b) - F(a) File "sage/symbolic/expression.pyx", line 12680, in sage.symbolic.expression._eval_on_operands.new_f (build/cythonized/sage/symbolic/expression.cpp:66926) return f(ex, *new_args, **kwds) File "/ext/sage/sage-8.9_1804/local/lib/python2.7/site-packages/sage/functions/piecewise.py", line 473, in __call__ return self.subs(substitution) File "sage/symbolic/expression.pyx", line 5321, in sage.symbolic.expression.Expression.substitute (build/cythonized/sage/symbolic/expression.cpp:30443) res = self._gobj.subs_map(smap, 0) File "/ext/sage/sage-8.9_1804/local/lib/python2.7/site-packages/sage/functions/piecewise.py", line 227, in _subs_ point = point.pyobject() File "sage/symbolic/expression.pyx", line 374, in sage.symbolic.expression.Expression.pyobject (build/cythonized/sage/symbolic/expression.cpp:6104) cpdef object pyobject(self): File "sage/symbolic/expression.pyx", line 430, in sage.symbolic.expression.Expression.pyobject (build/cythonized/sage/symbolic/expression.cpp:6030) raise TypeError("self must be a numeric expression") TypeError: self must be a numeric expression
B=bcoef(?);B
[0, 1/2, 0, 0]
S5(t)=?
s5_plot=plot(?,(t,?,?), linestyle="--")
(f_plot+s5_plot).show()