<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/253645</link>
    <title>MATLAB Central Newsreader - Negative indices in arrays</title>
    <description>Feed for thread: Negative indices in arrays</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Sat, 13 Jun 2009 23:57:01 -0400</pubDate>
      <title>Negative indices in arrays</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/253645#657103</link>
      <author>Chris G</author>
      <description>Since matlab will not allow negative numbers or 0 of indices in arrays, how can I manipulate it to produce numbers for those negative indexes?  I'm needing to calculate and plot on a stem plot an array going from -10 to 10 for a specified equation.  </description>
    </item>
    <item>
      <pubDate>Sun, 14 Jun 2009 01:26:01 -0400</pubDate>
      <title>Re: Negative indices in arrays</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/253645#657108</link>
      <author>Kian </author>
      <description>You need to be more specific. Give an example. This should be a easy problem to solve, but you need to give more clear.</description>
    </item>
    <item>
      <pubDate>Sun, 14 Jun 2009 02:38:01 -0400</pubDate>
      <title>Re: Negative indices in arrays</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/253645#657117</link>
      <author>Chris G</author>
      <description>Alright, welll. For an assignment I'm trying to calculate and plot the values for n for when they are zero and negative for the magnitude and phase plot at the very end of the Prob2.m for Dn. The negative indices are giving me the most difficulties :&lt;br&gt;
&lt;br&gt;
Prob2.m:&lt;br&gt;
&lt;br&gt;
clc&lt;br&gt;
clear all&lt;br&gt;
close all&lt;br&gt;
format compact&lt;br&gt;
%format short eng&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
% Create a waveform from t=Tbegin to t=Tend&lt;br&gt;
Tbegin = 0.0;&lt;br&gt;
Tend = 5;&lt;br&gt;
&lt;br&gt;
N = 2^15; Nm1=N-1; Np1=N+1; Nd2=N/2;&lt;br&gt;
&lt;br&gt;
t=[0:Nm1]';&lt;br&gt;
t=Tbegin + t*Tend/Nm1;&lt;br&gt;
tsample = t(2)-t(1);&lt;br&gt;
&lt;br&gt;
T0 = (Tend - Tbegin);&lt;br&gt;
f0 = 1/T0;&lt;br&gt;
w0 = 2*pi*f0;&lt;br&gt;
fnyq = f0/2; wnyq=2*pi*fnyq;&lt;br&gt;
&lt;br&gt;
NNN=100;&lt;br&gt;
&lt;br&gt;
t1 = 2;&lt;br&gt;
x = ((-2*t).*(rect_fcn((t-1.5)/3)))+(rect_fcn((t-4)/2)) ;&lt;br&gt;
%x(t&amp;lt;=t1) = t(t&amp;lt;=t1);&lt;br&gt;
&lt;br&gt;
% evaluate the analytic expression&lt;br&gt;
n = 1:NNN;&lt;br&gt;
Dn = ((((exp(-j*n*(6/5)*pi)).*(2-(2*j*n*(6/5)*pi)))-2)./((n.^2)*(4/5)*(pi^2)))+(((exp(-j*n*2*pi))-(exp(-j*n*(6/5)*pi)))./(-j*n*2*pi));&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
[a0,a,b]=fourier_series_coef(t,x,NNN);&lt;br&gt;
% Uncomment the following line to see the coeficients values.&lt;br&gt;
%a0, [a b];&lt;br&gt;
&lt;br&gt;
Dn2a = 2*real(Dn);&lt;br&gt;
Dn2b = -2*imag(Dn);&lt;br&gt;
% had to make N=2^10 larger at the beginning to get better&lt;br&gt;
% numerical integration results&lt;br&gt;
NNNd = 10;&lt;br&gt;
nnd=(1:NNNd);&lt;br&gt;
&lt;br&gt;
disp(' a_n coef ')&lt;br&gt;
[nnd' a(1:NNNd) Dn2a(1:NNNd)']&lt;br&gt;
disp(' ')&lt;br&gt;
disp(' b_n coef ')&lt;br&gt;
[nnd' b(1:NNNd) Dn2b(1:NNNd)']&lt;br&gt;
disp(' ')&lt;br&gt;
disp(' Dn')&lt;br&gt;
[nnd' Dn(1:NNNd).']&lt;br&gt;
disp(' ')&lt;br&gt;
disp(' |Dn|')&lt;br&gt;
[nnd' abs(Dn(1:NNNd))']&lt;br&gt;
disp(' ')&lt;br&gt;
disp(' arg(Dn)')&lt;br&gt;
[nnd' angle(Dn(1:NNNd))']&lt;br&gt;
&lt;br&gt;
% use the FFT to compute ~Dn&lt;br&gt;
XDn = fft(x)/N;&lt;br&gt;
disp(' ')&lt;br&gt;
disp(' ')&lt;br&gt;
disp(' analytic Dn FFT Dn')&lt;br&gt;
[Dn(1:NNNd).' XDn(2:NNNd+1)]&lt;br&gt;
&lt;br&gt;
% Generate a waveform using the Fourier Series&lt;br&gt;
xg = fourier_series_gen(t,T0,a0,a,b);&lt;br&gt;
subplot(2,1,1),plot(t,x), grid on&lt;br&gt;
ylabel('\it{x(t)}')&lt;br&gt;
title('One Period of \it{x(t)}');&lt;br&gt;
&lt;br&gt;
subplot(2,1,2), plot(t,xg), grid on&lt;br&gt;
xlabel('t (sec)')&lt;br&gt;
ylabel('\it{x^''(t)}');&lt;br&gt;
titlestr = ['Fourier Series Approximation to {\itx(t)} (' ...&lt;br&gt;
num2str(NNN) ' terms)'];&lt;br&gt;
title(titlestr);&lt;br&gt;
&lt;br&gt;
% Compare using partial sums of 5, 15 and 50 coeficients&lt;br&gt;
&lt;br&gt;
Np=3;&lt;br&gt;
tp=[0:(Np*N-1)]';&lt;br&gt;
tp=Tbegin + tp*Np*Tend/(Np*N-1);&lt;br&gt;
&lt;br&gt;
% 5&lt;br&gt;
Ncoef = 5;&lt;br&gt;
[a0,a,b]=fourier_series_coef(t,x,Ncoef);&lt;br&gt;
amin = min([a0; a]); amax = max([a0; a]);&lt;br&gt;
bmin = min(b); bmax = max(b);&lt;br&gt;
xg5 = fourier_series_gen(tp,T0,a0,a,b);&lt;br&gt;
&lt;br&gt;
% Plot Coefficients&lt;br&gt;
f = [f0:f0:(Ncoef*f0)]';&lt;br&gt;
figure&lt;br&gt;
&lt;br&gt;
% a's&lt;br&gt;
%subplot(2,1,1),stem([0; f],[a0; a],'r','LineWidth',3,'MarkerSize',5), grid&lt;br&gt;
%xlabel('f (Hz)');&lt;br&gt;
%ylabel('a_m');&lt;br&gt;
%title('Cosine Coefficients');&lt;br&gt;
%axis([-f0 (Ncoef+1)*f0 amin amax]);&lt;br&gt;
% b's&lt;br&gt;
%subplot(2,1,2), stem(f,b,'LineWidth',3,'MarkerSize',5), grid&lt;br&gt;
%xlabel('f (Hz)');&lt;br&gt;
%ylabel('b_m');&lt;br&gt;
%title('Sine Coefficients');&lt;br&gt;
%axis([-f0 (Ncoef+1)*f0 bmin bmax]);&lt;br&gt;
&lt;br&gt;
% 15&lt;br&gt;
Ncoef = 15;&lt;br&gt;
[a0,a,b]=fourier_series_coef(t,x,Ncoef);&lt;br&gt;
xg15 = fourier_series_gen(tp,T0,a0,a,b);&lt;br&gt;
&lt;br&gt;
% 50&lt;br&gt;
Ncoef = 50;&lt;br&gt;
[a0,a,b]=fourier_series_coef(t,x,Ncoef);&lt;br&gt;
xg50 = fourier_series_gen(tp,T0,a0,a,b);&lt;br&gt;
figure, plot(tp,xg5,tp,xg15,tp,xg50), grid on&lt;br&gt;
xlabel('t (sec)');&lt;br&gt;
ylabel('\it{x''(t)}')&lt;br&gt;
title('Partial Sum Approximations (5, 15, 50 terms)');&lt;br&gt;
legend('5','15','50')&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
%plotting Dn&lt;br&gt;
%n=[-10:10];&lt;br&gt;
subplot(2,1,1),stem([fftshift(Dn)],'r','LineWidth',3,'MarkerSize',5), grid&lt;br&gt;
xlabel('n');&lt;br&gt;
ylabel('magnitude');&lt;br&gt;
title('magnitude of Dn');&lt;br&gt;
axis([-f0 (Ncoef+1)*f0 amin amax]);&lt;br&gt;
&lt;br&gt;
subplot(2,1,2), stem([angle(Dn(1:NNNd))],'LineWidth',3,'MarkerSize',5), grid&lt;br&gt;
xlabel('n');&lt;br&gt;
ylabel('argument');&lt;br&gt;
title('argument of Dn');&lt;br&gt;
axis([-f0 (Ncoef+1)*f0 bmin bmax]);&lt;br&gt;
&lt;br&gt;
Function m-files:&lt;br&gt;
fourier_series_coef.m&lt;br&gt;
&lt;br&gt;
function [a0, a, b]=fourier_series_coef(t,x,numterms)&lt;br&gt;
[nrt nct]=size(t);&lt;br&gt;
[nrx ncx]=size(x);&lt;br&gt;
&lt;br&gt;
% Check for input errors&lt;br&gt;
if (nrt~=nrx)|(nct~=ncx)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;error('t and x must be the same size')&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
if min(nrt,nct)~=1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;error('t and x must be row or column vectors')&lt;br&gt;
end&lt;br&gt;
N = length(t);&lt;br&gt;
&lt;br&gt;
td = diff(t);&lt;br&gt;
if any(td&amp;lt;0)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;error('t must be increasing')&lt;br&gt;
end&lt;br&gt;
tdd=diff(td);&lt;br&gt;
&lt;br&gt;
dt = t(2)-t(1);&lt;br&gt;
T0 = t(N)-t(1);&lt;br&gt;
f0 = 1/T0;&lt;br&gt;
w0 = 2*pi*f0;&lt;br&gt;
&lt;br&gt;
% a0 = 1/T0 * integ(x(t)) over T0&lt;br&gt;
&lt;br&gt;
a0 = (1/T0)*trapz(t,x);&lt;br&gt;
&lt;br&gt;
% am = 2/T0 * integ(x(t)*cos(m*w0*t)) over T0&lt;br&gt;
% bm = 2/T0 * integ(x(t)*sin(m*w0*t)) over T0&lt;br&gt;
&lt;br&gt;
a = zeros(numterms,1);&lt;br&gt;
b = zeros(numterms,1);&lt;br&gt;
for m=1:numterms&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;a(m) = (2/T0)*trapz(t,(x.*cos(m*w0*t)));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;b(m) = (2/T0)*trapz(t,(x.*sin(m*w0*t)));&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
return&lt;br&gt;
&lt;br&gt;
fourier_series_gen.m&lt;br&gt;
&lt;br&gt;
function [x] = fourier_series_gen(t,T0,a0,a,b)&lt;br&gt;
% generates a waveform using the partial sum of the fourier series&lt;br&gt;
&lt;br&gt;
[nra nca]=size(a);&lt;br&gt;
[nrb ncb]=size(b);&lt;br&gt;
Na = length(a);&lt;br&gt;
Nb = length(b);&lt;br&gt;
&lt;br&gt;
x = a0 + zeros(size(t));&lt;br&gt;
w0=2*pi/T0;&lt;br&gt;
for m=1:Na&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;x = x + a(m)*cos(m*w0*t);&lt;br&gt;
end&lt;br&gt;
for m=1:Nb&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;x = x + b(m)*sin(m*w0*t);&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
return&lt;br&gt;
&lt;br&gt;
rect_fcn.m&lt;br&gt;
&lt;br&gt;
function y = rect_fcn(t)&lt;br&gt;
% Continuous-time rect function&lt;br&gt;
% y = 0.0 for t&amp;lt;-0.5 and t&amp;gt;+0.5,&lt;br&gt;
% y = 1.0 for -0.5&amp;lt; t &amp;lt;+0.5&lt;br&gt;
% y = 0.5 for |t| = 0.5&lt;br&gt;
&lt;br&gt;
y = zeros(size(t));&lt;br&gt;
y((t&amp;gt;-0.5)&amp;(t&amp;lt;+0.5)) = 1.0;&lt;br&gt;
y((t==-0.5)|(t==+0.5)) = 0.5;&lt;br&gt;
return </description>
    </item>
    <item>
      <pubDate>Sun, 14 Jun 2009 02:46:01 -0400</pubDate>
      <title>Re: Negative indices in arrays</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/253645#657119</link>
      <author>Chris G</author>
      <description>&quot;Chris G&quot; &amp;lt;n4cag@yahoo.com&amp;gt; wrote in message &amp;lt;h11nq9$73l$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&lt;br&gt;
MADE A SLIGHT CORRECTION, but still same problem:&lt;br&gt;
&lt;br&gt;
&amp;gt; Alright, welll. For an assignment I'm trying to calculate and plot the values for n for when they are zero and negative for the magnitude and phase plot at the very end of the Prob2.m for Dn. The negative indices are giving me the most difficulties :&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Prob2.m:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; clc&lt;br&gt;
&amp;gt; clear all&lt;br&gt;
&amp;gt; close all&lt;br&gt;
&amp;gt; format compact&lt;br&gt;
&amp;gt; %format short eng&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % Create a waveform from t=Tbegin to t=Tend&lt;br&gt;
&amp;gt; Tbegin = 0.0;&lt;br&gt;
&amp;gt; Tend = 5;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; N = 2^15; Nm1=N-1; Np1=N+1; Nd2=N/2;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; t=[0:Nm1]';&lt;br&gt;
&amp;gt; t=Tbegin + t*Tend/Nm1;&lt;br&gt;
&amp;gt; tsample = t(2)-t(1);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; T0 = (Tend - Tbegin);&lt;br&gt;
&amp;gt; f0 = 1/T0;&lt;br&gt;
&amp;gt; w0 = 2*pi*f0;&lt;br&gt;
&amp;gt; fnyq = f0/2; wnyq=2*pi*fnyq;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; NNN=100;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; t1 = 2;&lt;br&gt;
&amp;gt; x = ((-2*t).*(rect_fcn((t-1.5)/3)))+(rect_fcn((t-4)/2)) ;&lt;br&gt;
&amp;gt; %x(t&amp;lt;=t1) = t(t&amp;lt;=t1);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % evaluate the analytic expression&lt;br&gt;
&amp;gt; n = 1:NNN;&lt;br&gt;
&amp;gt; Dn = ((((exp(-j*n*(6/5)*pi)).*(2-(2*j*n*(6/5)*pi)))-2)./((n.^2)*(4/5)*(pi^2)))+(((exp(-j*n*2*pi))-(exp(-j*n*(6/5)*pi)))./(-j*n*2*pi));&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; [a0,a,b]=fourier_series_coef(t,x,NNN);&lt;br&gt;
&amp;gt; % Uncomment the following line to see the coeficients values.&lt;br&gt;
&amp;gt; %a0, [a b];&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Dn2a = 2*real(Dn);&lt;br&gt;
&amp;gt; Dn2b = -2*imag(Dn);&lt;br&gt;
&amp;gt; % had to make N=2^10 larger at the beginning to get better&lt;br&gt;
&amp;gt; % numerical integration results&lt;br&gt;
&amp;gt; NNNd = 10;&lt;br&gt;
&amp;gt; nnd=(1:NNNd);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; disp(' a_n coef ')&lt;br&gt;
&amp;gt; [nnd' a(1:NNNd) Dn2a(1:NNNd)']&lt;br&gt;
&amp;gt; disp(' ')&lt;br&gt;
&amp;gt; disp(' b_n coef ')&lt;br&gt;
&amp;gt; [nnd' b(1:NNNd) Dn2b(1:NNNd)']&lt;br&gt;
&amp;gt; disp(' ')&lt;br&gt;
&amp;gt; disp(' Dn')&lt;br&gt;
&amp;gt; [nnd' Dn(1:NNNd).']&lt;br&gt;
&amp;gt; disp(' ')&lt;br&gt;
&amp;gt; disp(' |Dn|')&lt;br&gt;
&amp;gt; [nnd' abs(Dn(1:NNNd))']&lt;br&gt;
&amp;gt; disp(' ')&lt;br&gt;
&amp;gt; disp(' arg(Dn)')&lt;br&gt;
&amp;gt; [nnd' angle(Dn(1:NNNd))']&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % use the FFT to compute ~Dn&lt;br&gt;
&amp;gt; XDn = fft(x)/N;&lt;br&gt;
&amp;gt; disp(' ')&lt;br&gt;
&amp;gt; disp(' ')&lt;br&gt;
&amp;gt; disp(' analytic Dn FFT Dn')&lt;br&gt;
&amp;gt; [Dn(1:NNNd).' XDn(2:NNNd+1)]&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % Generate a waveform using the Fourier Series&lt;br&gt;
&amp;gt; xg = fourier_series_gen(t,T0,a0,a,b);&lt;br&gt;
&amp;gt; subplot(2,1,1),plot(t,x), grid on&lt;br&gt;
&amp;gt; ylabel('\it{x(t)}')&lt;br&gt;
&amp;gt; title('One Period of \it{x(t)}');&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; subplot(2,1,2), plot(t,xg), grid on&lt;br&gt;
&amp;gt; xlabel('t (sec)')&lt;br&gt;
&amp;gt; ylabel('\it{x^''(t)}');&lt;br&gt;
&amp;gt; titlestr = ['Fourier Series Approximation to {\itx(t)} (' ...&lt;br&gt;
&amp;gt; num2str(NNN) ' terms)'];&lt;br&gt;
&amp;gt; title(titlestr);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % Compare using partial sums of 5, 15 and 50 coeficients&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Np=3;&lt;br&gt;
&amp;gt; tp=[0:(Np*N-1)]';&lt;br&gt;
&amp;gt; tp=Tbegin + tp*Np*Tend/(Np*N-1);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % 5&lt;br&gt;
&amp;gt; Ncoef = 5;&lt;br&gt;
&amp;gt; [a0,a,b]=fourier_series_coef(t,x,Ncoef);&lt;br&gt;
&amp;gt; amin = min([a0; a]); amax = max([a0; a]);&lt;br&gt;
&amp;gt; bmin = min(b); bmax = max(b);&lt;br&gt;
&amp;gt; xg5 = fourier_series_gen(tp,T0,a0,a,b);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % Plot Coefficients&lt;br&gt;
&amp;gt; f = [f0:f0:(Ncoef*f0)]';&lt;br&gt;
&amp;gt; figure&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % a's&lt;br&gt;
&amp;gt; %subplot(2,1,1),stem([0; f],[a0; a],'r','LineWidth',3,'MarkerSize',5), grid&lt;br&gt;
&amp;gt; %xlabel('f (Hz)');&lt;br&gt;
&amp;gt; %ylabel('a_m');&lt;br&gt;
&amp;gt; %title('Cosine Coefficients');&lt;br&gt;
&amp;gt; %axis([-f0 (Ncoef+1)*f0 amin amax]);&lt;br&gt;
&amp;gt; % b's&lt;br&gt;
&amp;gt; %subplot(2,1,2), stem(f,b,'LineWidth',3,'MarkerSize',5), grid&lt;br&gt;
&amp;gt; %xlabel('f (Hz)');&lt;br&gt;
&amp;gt; %ylabel('b_m');&lt;br&gt;
&amp;gt; %title('Sine Coefficients');&lt;br&gt;
&amp;gt; %axis([-f0 (Ncoef+1)*f0 bmin bmax]);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % 15&lt;br&gt;
&amp;gt; Ncoef = 15;&lt;br&gt;
&amp;gt; [a0,a,b]=fourier_series_coef(t,x,Ncoef);&lt;br&gt;
&amp;gt; xg15 = fourier_series_gen(tp,T0,a0,a,b);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % 50&lt;br&gt;
&amp;gt; Ncoef = 50;&lt;br&gt;
&amp;gt; [a0,a,b]=fourier_series_coef(t,x,Ncoef);&lt;br&gt;
&amp;gt; xg50 = fourier_series_gen(tp,T0,a0,a,b);&lt;br&gt;
&amp;gt; figure, plot(tp,xg5,tp,xg15,tp,xg50), grid on&lt;br&gt;
&amp;gt; xlabel('t (sec)');&lt;br&gt;
&amp;gt; ylabel('\it{x''(t)}')&lt;br&gt;
&amp;gt; title('Partial Sum Approximations (5, 15, 50 terms)');&lt;br&gt;
&amp;gt; legend('5','15','50')&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; %plotting Dn&lt;br&gt;
&amp;gt; %n=[-10:10];&lt;br&gt;
&amp;gt; subplot(2,1,1),stem([abs(Dn)],'r','LineWidth',3,'MarkerSize',5), grid&lt;br&gt;
&amp;gt; xlabel('n');&lt;br&gt;
&amp;gt; ylabel('magnitude');&lt;br&gt;
&amp;gt; title('magnitude of Dn');&lt;br&gt;
&amp;gt; axis([-f0 (Ncoef+1)*f0 amin amax]);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; subplot(2,1,2), stem([angle(Dn(1:NNNd))],'LineWidth',3,'MarkerSize',5), grid&lt;br&gt;
&amp;gt; xlabel('n');&lt;br&gt;
&amp;gt; ylabel('argument');&lt;br&gt;
&amp;gt; title('argument of Dn');&lt;br&gt;
&amp;gt; axis([-f0 (Ncoef+1)*f0 bmin bmax]);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Function m-files:&lt;br&gt;
&amp;gt; fourier_series_coef.m&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; function [a0, a, b]=fourier_series_coef(t,x,numterms)&lt;br&gt;
&amp;gt; [nrt nct]=size(t);&lt;br&gt;
&amp;gt; [nrx ncx]=size(x);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % Check for input errors&lt;br&gt;
&amp;gt; if (nrt~=nrx)|(nct~=ncx)&lt;br&gt;
&amp;gt;     error('t and x must be the same size')&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; if min(nrt,nct)~=1&lt;br&gt;
&amp;gt;     error('t and x must be row or column vectors')&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; N = length(t);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; td = diff(t);&lt;br&gt;
&amp;gt; if any(td&amp;lt;0)&lt;br&gt;
&amp;gt;     error('t must be increasing')&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; tdd=diff(td);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; dt = t(2)-t(1);&lt;br&gt;
&amp;gt; T0 = t(N)-t(1);&lt;br&gt;
&amp;gt; f0 = 1/T0;&lt;br&gt;
&amp;gt; w0 = 2*pi*f0;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % a0 = 1/T0 * integ(x(t)) over T0&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; a0 = (1/T0)*trapz(t,x);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; % am = 2/T0 * integ(x(t)*cos(m*w0*t)) over T0&lt;br&gt;
&amp;gt; % bm = 2/T0 * integ(x(t)*sin(m*w0*t)) over T0&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; a = zeros(numterms,1);&lt;br&gt;
&amp;gt; b = zeros(numterms,1);&lt;br&gt;
&amp;gt; for m=1:numterms&lt;br&gt;
&amp;gt;     a(m) = (2/T0)*trapz(t,(x.*cos(m*w0*t)));&lt;br&gt;
&amp;gt;     b(m) = (2/T0)*trapz(t,(x.*sin(m*w0*t)));&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; return&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; fourier_series_gen.m&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; function [x] = fourier_series_gen(t,T0,a0,a,b)&lt;br&gt;
&amp;gt; % generates a waveform using the partial sum of the fourier series&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; [nra nca]=size(a);&lt;br&gt;
&amp;gt; [nrb ncb]=size(b);&lt;br&gt;
&amp;gt; Na = length(a);&lt;br&gt;
&amp;gt; Nb = length(b);&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; x = a0 + zeros(size(t));&lt;br&gt;
&amp;gt; w0=2*pi/T0;&lt;br&gt;
&amp;gt; for m=1:Na&lt;br&gt;
&amp;gt;     x = x + a(m)*cos(m*w0*t);&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; for m=1:Nb&lt;br&gt;
&amp;gt;     x = x + b(m)*sin(m*w0*t);&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; return&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; rect_fcn.m&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; function y = rect_fcn(t)&lt;br&gt;
&amp;gt; % Continuous-time rect function&lt;br&gt;
&amp;gt; % y = 0.0 for t&amp;lt;-0.5 and t&amp;gt;+0.5,&lt;br&gt;
&amp;gt; % y = 1.0 for -0.5&amp;lt; t &amp;lt;+0.5&lt;br&gt;
&amp;gt; % y = 0.5 for |t| = 0.5&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; y = zeros(size(t));&lt;br&gt;
&amp;gt; y((t&amp;gt;-0.5)&amp;(t&amp;lt;+0.5)) = 1.0;&lt;br&gt;
&amp;gt; y((t==-0.5)|(t==+0.5)) = 0.5;&lt;br&gt;
&amp;gt; return </description>
    </item>
    <item>
      <pubDate>Sun, 14 Jun 2009 19:01:02 -0400</pubDate>
      <title>Re: Negative indices in arrays</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/253645#657199</link>
      <author>Chris G</author>
      <description>61 views and only one person has been able to help me?  All I'm looking for is how to get Matlab to plot the negative indices for my array of numbers in Dn...</description>
    </item>
    <item>
      <pubDate>Sun, 14 Jun 2009 22:44:02 -0400</pubDate>
      <title>Re: Negative indices in arrays</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/253645#657231</link>
      <author>Sadik </author>
      <description>Hi Chris,&lt;br&gt;
&lt;br&gt;
Why would you need non-positive indices? I could not go through your code exactly, so I will not be able to be specific about the problem. How about this simple example. Maybe it is clearer if we talk on this new small piece of code:&lt;br&gt;
&lt;br&gt;
n = -10:0.5:10;&lt;br&gt;
Dn = n.^2;&lt;br&gt;
&lt;br&gt;
Now, we have Dn as a function of n. It is clear that length(n) is 41. So now, n(1) is -10, n(2) is -9.5, ..., n(41) = 10. Therefore, if you need the value of Dn at n = -9 for example, you can get it by Dn(3), right? Since n(3) = -9.&lt;br&gt;
&lt;br&gt;
Please let me know if I understood your question correctly. If not, please give an example which is similar to the above one.&lt;br&gt;
&lt;br&gt;
&quot;Chris G&quot; &amp;lt;n4cag@yahoo.com&amp;gt; wrote in message &amp;lt;h13hde$qlc$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; 61 views and only one person has been able to help me?  All I'm looking for is how to get Matlab to plot the negative indices for my array of numbers in Dn...</description>
    </item>
    <item>
      <pubDate>Sun, 14 Jun 2009 23:17:16 -0400</pubDate>
      <title>Re: Negative indices in arrays</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/253645#657233</link>
      <author>Kian </author>
      <description>That's a lot of code for people to read through. You need to realize that some people are willing to help here, but it's your job to make it really easy for them. If you post a 100-line-long code and expect people to decode it and solve your issue then you might not get a response. I'm not being mean, but just trying to help.&lt;br&gt;
&lt;br&gt;
Again, from what I understand, you're trying to plot a vector that goes from -10 to 10, right? Is it something like this:&lt;br&gt;
&lt;br&gt;
x = -10:10;&lt;br&gt;
&lt;br&gt;
And then you're trying to plot it? You never need negative indices. For example, imagine a Cartesian coordinate that goes from -10 to 10 for the x-axis in 1-unit steps:&lt;br&gt;
&lt;br&gt;
-10 -9 -8 -7 ... -1 0 1 2 ... 7 8 9 10&lt;br&gt;
&lt;br&gt;
OK? You don't have to start your indices from the middle. The beginning of the vector will be the most left number. In this case, your vector X will have the following values:&lt;br&gt;
&lt;br&gt;
X = [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];&lt;br&gt;
&lt;br&gt;
X(1) = -10&lt;br&gt;
X(11) = 0&lt;br&gt;
X(21) = 10&lt;br&gt;
&lt;br&gt;
Does this make sense? If not, please try to explain a bit more. Please give an example of what exactly it is that you're trying to do instead of posting the entire code. It's really hard to understand a long code that I haven't written and it's not formatted and color-coded in the .m form.&lt;br&gt;
&lt;br&gt;
Kian</description>
    </item>
    <item>
      <pubDate>Sun, 14 Jun 2009 23:39:02 -0400</pubDate>
      <title>Re: Negative indices in arrays</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/253645#657235</link>
      <author>Kian </author>
      <description>Seems like Sadik and I had the same take on your problem. I hope this is what you're looking for. If not, please explain more using either one of our examples. We talked about the same thing (I believe we posted at the same time).</description>
    </item>
    <item>
      <pubDate>Mon, 15 Jun 2009 01:16:34 -0400</pubDate>
      <title>Re: Negative indices in arrays</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/253645#657247</link>
      <author>Steven Lord</author>
      <description>&lt;br&gt;
&quot;Chris G&quot; &amp;lt;n4cag@yahoo.com&amp;gt; wrote in message &lt;br&gt;
news:h11ecd$mps$1@fred.mathworks.com...&lt;br&gt;
&amp;gt; Since matlab will not allow negative numbers or 0 of indices in arrays, &lt;br&gt;
&amp;gt; how can I manipulate it to produce numbers for those negative indexes? &lt;br&gt;
&amp;gt; I'm needing to calculate and plot on a stem plot an array going from -10 &lt;br&gt;
&amp;gt; to 10 for a specified equation.&lt;br&gt;
&lt;br&gt;
Do you mean you want a plot like this?&lt;br&gt;
&lt;br&gt;
x = -10:1:10;&lt;br&gt;
y = x.^2;&lt;br&gt;
stem(x, y)&lt;br&gt;
&lt;br&gt;
Many plotting functions allow you to specify not only y but also x (and for &lt;br&gt;
3D plotting functions, you can specify all of x, y, and z.)&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Steve Lord&lt;br&gt;
slord@mathworks.com </description>
    </item>
  </channel>
</rss>

