Reputation: 47
I am trying to plot the periodic functions in matlab of the following equation:
using the following code:
tn= 25;
kn= 7;
time=0:1:200;
f=0;
for t= (-tn):1:(tn)
for k = (-kn):1:(kn)
s1=((1j)*k*exp(-abs(k)));
s2=exp((1j)*k*((2*pi)/50));
f=f+(s1*s2);
end
tval = (f*exp(t));
fs(1,t+1) = tval;
end
I am having trouble understanding why I am not able to see the plot. Is there another way to plot complex numbers in Matlab?
Am I going about this the wrong way? I am simply trying to plot fs against time (could be from 0-200 or -100 - 100 for all I care) and see the periodic function so I can go ahead and manipulate it but I can't seem to even get the correct plot.
I tried using the symsum function in matlab but could not figure it out. I understand C and C++ and felt like this approach was more intuitive for me.
edit:
x(1:101)=0;
t(1:101)=0;
for n=0:1:100
t(n+1)=n;
for k=-100:1:100
x(n+1)=x(n+1)+abs(sin((k*pi)/2))*exp(1j*k*((2*pi)/50)*n);
end;
end;
I plotted the function using the following code. Why do our imaginary plots look different?
Upvotes: 1
Views: 1010
Reputation: 7941
I don't have Matlab, but here the same in Python (using Numpy & Matplotlib which are very similar to Matlab):
import numpy as np
import matplotlib.pyplot as p
%matplotlib inline
t= np.arange(0,100,0.1)
s=np.zeros(len(t))
for k in range(40):
s=s+ np.abs(np.sin (k*np.pi/2)) * np.exp( 1j*k *2*np.pi/50.0*t) # pos k
s=s+ np.abs(np.sin (-k*np.pi/2)) * np.exp(- 1j*k *2*np.pi/50.0*t) # negative k
# the cosine is symmetric so it survives adding the negatives,
# the sines get cancelled as they are antisymmetric for the negative k,
# so the imaginary part is identically zero.
p.subplot(311)
p.plot(np.real(s))
p.subplot(312)
p.plot(np.imag(s),'r')
p.subplot(313)
p.plot(np.abs(s))
Upvotes: 0