 CoCalc Public FilesAssignments / graded-AssignmentCh10 / pbsetCh10student_version.sagews
Author: Edgar Arevalo
Views : 67
#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=pi*t/180
else:
return angle


convert_angle(90,0)

1/2*pi


########################
#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)
fplot=plot(f,-11,11,color='red',thickness=3)
top=plot(-2*cos(.15*pi*t),-11,11,color='blue',thickness=2)
bottom=plot(2*cos(.15*pi*t),-11,11,color='blue',thickness=2)
(fplot+top+bottom).show() reset

<function reset at 0x7f4cd92ae0c8>
########################
#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*cos(k*t),t,t0,T) for k in range(n+1) ]
return [c/pi for c in L]

#integral?

#My example
f(t)=t^2
acoef(f,6,-pi,pi)

[2/3*pi^2, -4, 1, -4/9, 1/4, -4/25, 1/9]
#Your example: g(t)=t for 0<t<=2*pi ##Incorrect example
f(t)=t^2
acoef(f,2,-pi,pi)


[2/3*pi^2, -4, 1]


#######################
#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.
f(t)=abs(t)
acoef(f,6,-pi,pi)


[pi, -4/pi, 0, -4/9/pi, 0, -4/25/pi, 0]
#(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).


#######################
#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.


#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(?,?,?,pi);A

[2/pi, 0, -2/3/pi, 0]
B=bcoef(?);B

[0, 1/2, 0, 0]
S5(t)=?

s5_plot=plot(?,(t,?,?), linestyle="--")

(f_plot+s5_plot).show() ## Exercises 8 and 10 not done; Exercises 6: incorrect example (-0.5); Exercise 1: conversion from radians to degrees incorrect (-0.5). Total: 4/6
︠6a4d89f6-85a6-48e7-ae52-2c97af795e46︠