Exercises with Maple
Calculate Amplitude and Phase of a Harmonic Oscillation
Write a Maple Procedure that calculates numerically the resulting amplitude and phase of a sum of sin-oscillations,
which all have the same frequency but possibly different amplitudes and phases, i.e., calculate A and
,
so that for a given number N and given
it holds:
(w is a fixed angular frequency, t a time parameter).
A Solution:
The array a[1] .. a[N] contains the amplitudes of the given superposition,
the array phi[1]..phi[N ] the corresponding phase shifts. The procedure result
gives the resulting amplitude A and the corresponding phase
of the given
superposition. We calculate the resulting phase in three different ways.
Explain the different phase results in the examples
> | restart: |
> | result:=proc(a,phi) |
> | local N::integer,s1,s2; global Amplitude,Phase; |
> | N:=linalg[vectdim](a); |
> | s1:=evalf(sum(a[k]*cos(phi[k]),k=1..N)):print('s1'=evalf(s1)); |
> | s2:=evalf(sum(a[k]*sin(phi[k]),k=1..N)):print('s2'=evalf(s2)); |
> | Amplitude:=evalf(sqrt(s1^2+s2^2)):print('Amplitude'=Amplitude); |
> | #Now you can calculate the phase using the arccos |
> | #We first consider the cases of phase 0 and phase Pi, |
> | #then the other cases |
> | if Amplitude=0 or s1=Amplitude then Phase:=0 elif s1=-Amplitude then Phase:=Pi |
> | else Phase:=evalf(signum(s2)*arccos(s1/Amplitude)) |
> | end if: |
> | print('Phase'=Phase); |
> | # alternatively you can use the arctan using the theorem: arccos(x)=P/2-arctan(x/sqrt(1-x^2)) |
> | if Amplitude=0 or s1=Amplitude then Phase:=0 elif s1=-Amplitude then Phase:=Pi |
> | else Phase:=evalf(signum(s2)*(Pi/2-arctan(s1/sqrt(Amplitude^2-s1^2)))) |
> | end if: print('Phase'=Phase); |
> | # 3rd formula again using the arctan |
> | if Amplitude=0 then Phase:=0 elif s1=0 then Phase:=signum(s2)*Pi/2 |
> | elif s1>0 then Phase:=evalf(arctan(s2/s1)) |
> | elif s2>=0 then Phase:= evalf(arctan(s2/s1)+Pi) |
> | else Phase:= evalf(arctan(s2/s1)-Pi) |
> | end if: |
> | print('Phase'=Phase); |
> | end; |
At first, test the cases A=0, phi=0, phi=Pi, Phi=Pi/2, phi=-Pi/2 :
> | a:=vector([1,-1]);phi:=vector([1,1]); #given amplitudes and phases in the superposition |
> | result(a,phi); #Amplitude and Phase of the resulting sine wave |
> |
> | a:=vector([1,1]);phi:=vector([0,0]); #given amplitudes and phases in the superposition |
> | result(a,phi); #Amplitude and Phase of the resulting sine wave |
> | a:=vector([1,4]);phi:=vector([Pi,Pi]); #given amplitudes and phases in the superposition |
> | result(a,phi); #Amplitude and Phase of the resulting sine wave |
> |
> | a:=vector([1,1]);phi:=vector([Pi/2,Pi/2]); #given amplitudes and phases in the superposition |
> | result(a,phi); #Amplitude and Phase of the resulting sine wave |
> |
> | a:=vector([1,1]);phi:=vector([-Pi/2,-Pi/2]); #given amplitudes and phases in the superposition |
> | result(a,phi); #Amplitude and Phase of the resulting sine wave |
Then test other examples
Example 1: We calculate the resulting amplitude and phase for
2*sin(w*t)+2.5*cos(w*t+Pi/3)-4*sin(w*t-Pi/4).
To apply the above procedure we note that cos(w*t+Pi/3) = sin(w*t+5*Pi/6) .
> | a:=vector([2,2.5,-4]);phi:=vector([0,5*Pi/6,-Pi/4]); |
> | result(a,phi); |
> | Amplitude;Phase; |
Example 2: We calculate the resulting amplitude and phase for
cos(w*t)+sqrt(3)*cos(w*t+Pi/2).
> | b:=vector([1,sqrt(3)]);psi:=vector([Pi/2,Pi]); |
> | result(b,psi); |
Example 3: We calculate the resulting amplitude and phase for
sin(w*t+Pi/4)+3*sin(w*t-3*Pi/4).
> | c:=vector([1,3]);alpha:=vector([Pi/4,-3*Pi/4]); |
> | result(c,alpha); |
> |