Thread Subject: Problem with "for"

Subject: Problem with "for"

From: Estelle

Date: 2 Jan, 2008 09:39:53

Message: 1 of 5

I do not konw why but I have a problem with one of my "for" in my code.
Please help me!!! LOL

This is my code and my problem is in the 3rd "for" with "k=(N/2+2):N"
because the caculation of wc(k) give twice the number of wc I want. In fact
I obtain N values of wc while I need N/2 values and between 1 and N/2 all
the values are zero. I do not understand why. Yesterday when I did few
changes, I obtaines 3 times N values.
Thank you for your help

%%% vérifier longueur point%%%
n=1;
while 2^n<Zu.npts;
    n=n+1;
end;
corr=2^n;
plus=corr-Zu.npts;
ajout=zeros(plus,1);
new=vertcat(Zu.trace,ajout);
N=length(new);

% Transformée de fourier discrète du signal : a(t)-> A(w):
A=fft(new);
% Aa=A(1:N/2);
% Ab=A(N/2+1:N);

%Damping de 5% :
epsi=0.05;

% %Complex :
% i=sqrt(-1);

%%% Matrice vide %%%
Sd=[];

%%%pas en fréquence %%%%
pas=1/(N*Zu.delta);

%%% fréquence propre en Hz %%%
delta=10^(1/30);

for m=1:91;
    wo=0.1*(delta)^m;

    for j=1:N/2+1;
        w(j)=2*pi*pas*(j-1);
        %Fonction de transfert H en fonction de w, wo et epsi :
        H(j)=1/(w(j)^2-(2*i*(wo)*(epsi)*w(j))-wo^2);
        Xa(j)=A(j,1)*H(j);
    end


    for k=(N/2+2):N;
        wc(k)=-2*pi*pas*(N+1-k);
        %Fonction de transfert H en fonction de w, wo et epsi :
        Hc(k)=1/(wc(k)^2-(2*i*(wo)*(epsi)*wc(k))-wo^2);
        Xb(k)=A(k,1)*Hc(k);
    end


    X=vertcat(Xa,Xb);

     x=ifft(X);
     ma=max(x);
     Sd=[Sd;[ma wo]];
     S=vertcat(Sd);
end
    semilogx(S(:,2),S(:,1));
    xlabel ('frequency (Hz)','FontSize',10); ylabel
('displacement','FontSize',10);
    title('Displacement response spectra','FontSize',14);





Subject: Problem with

From: Huy

Date: 2 Jan, 2008 11:05:37

Message: 2 of 5

"Estelle" <tahoser45@hotmail.com> wrote in message
<477b5bff$0$21142$7a628cd7@news.club-internet.fr>...
> I do not konw why but I have a problem with one of my "for"
in my code.
> Please help me!!! LOL
>
> This is my code and my problem is in the 3rd "for" with
"k=(N/2+2):N"
> because the caculation of wc(k) give twice the number of wc I
want. In fact
> I obtain N values of wc while I need N/2 values and between
1 and N/2 all
> the values are zero. I do not understand why. Yesterday when
I did few
> changes, I obtaines 3 times N values.
> Thank you for your help
>
> %%% vérifier longueur point%%%
> n=1;
> while 2^n<Zu.npts;
> n=n+1;
> end;
> corr=2^n;
> plus=corr-Zu.npts;
> ajout=zeros(plus,1);
> new=vertcat(Zu.trace,ajout);
> N=length(new);
>
> % Transformée de fourier discrète du signal : a(t)-> A(w):
> A=fft(new);
> % Aa=A(1:N/2);
> % Ab=A(N/2+1:N);
>
> %Damping de 5% :
> epsi=0.05;
>
> % %Complex :
> % i=sqrt(-1);
>
> %%% Matrice vide %%%
> Sd=[];
>
> %%%pas en fréquence %%%%
> pas=1/(N*Zu.delta);
>
> %%% fréquence propre en Hz %%%
> delta=10^(1/30);
>
> for m=1:91;
> wo=0.1*(delta)^m;
>
> for j=1:N/2+1;
> w(j)=2*pi*pas*(j-1);
> %Fonction de transfert H en fonction de w, wo et epsi
 :
> H(j)=1/(w(j)^2-(2*i*(wo)*(epsi)*w(j))-wo^2);
> Xa(j)=A(j,1)*H(j);
> end
>
>
> for k=(N/2+2):N;
> wc(k)=-2*pi*pas*(N+1-k);
> %Fonction de transfert H en fonction de w, wo et epsi
 :
> Hc(k)=1/(wc(k)^2-(2*i*(wo)*(epsi)*wc(k))-wo^2);
> Xb(k)=A(k,1)*Hc(k);
> end
>
>
> X=vertcat(Xa,Xb);
>
> x=ifft(X);
> ma=max(x);
> Sd=[Sd;[ma wo]];
> S=vertcat(Sd);
> end
> semilogx(S(:,2),S(:,1));
> xlabel ('frequency (Hz)','FontSize',10); ylabel
> ('displacement','FontSize',10);
> title('Displacement response spectra','FontSize',14);
>
>
>
>
>

This example could illustrate your error:

a(3) = 1

The result will be

a = [0 0 1]

Because k gets values in the range [N/2+2),N], wc becomes a
vector with N elements.

Moreover, the soure code needs to be vectorized and avoid using
loop. Here is just an example which could replace the 2nd and
3rd loops

    w = 2*pi*pas*[0:N/2 -N/2+1:-1];
    H = 1./(w.^2-2*i*wo*epsi*w-wo^2);
    X = A.* H';

Anh Huy Phan
RIKEN - BSI









Subject: Problem with

From: Estelle

Date: 2 Jan, 2008 13:08:19

Message: 3 of 5

Hi,
Thanks for your help.
I just do not understand why you said that wc has N values. Because the
values are included into N/2 and N. I know that it could be evident for you
but not for me and I really want to understand the problem.
I try your code as well but it does not work.

I am sorry if I am a bit insistent but I really want to understand all
steps!

Thank you very much
Best regards
Estelle


"Huy " <phananhhuy@mathworks.com> a écrit dans le message de news:
flfr61$mtu$1@fred.mathworks.com...
> "Estelle" <tahoser45@hotmail.com> wrote in message
> <477b5bff$0$21142$7a628cd7@news.club-internet.fr>...
>> I do not konw why but I have a problem with one of my "for"
> in my code.
>> Please help me!!! LOL
>>
>> This is my code and my problem is in the 3rd "for" with
> "k=(N/2+2):N"
>> because the caculation of wc(k) give twice the number of wc I
> want. In fact
>> I obtain N values of wc while I need N/2 values and between
> 1 and N/2 all
>> the values are zero. I do not understand why. Yesterday when
> I did few
>> changes, I obtaines 3 times N values.
>> Thank you for your help
>>
>> %%% vérifier longueur point%%%
>> n=1;
>> while 2^n<Zu.npts;
>> n=n+1;
>> end;
>> corr=2^n;
>> plus=corr-Zu.npts;
>> ajout=zeros(plus,1);
>> new=vertcat(Zu.trace,ajout);
>> N=length(new);
>>
>> % Transformée de fourier discrète du signal : a(t)-> A(w):
>> A=fft(new);
>> % Aa=A(1:N/2);
>> % Ab=A(N/2+1:N);
>>
>> %Damping de 5% :
>> epsi=0.05;
>>
>> % %Complex :
>> % i=sqrt(-1);
>>
>> %%% Matrice vide %%%
>> Sd=[];
>>
>> %%%pas en fréquence %%%%
>> pas=1/(N*Zu.delta);
>>
>> %%% fréquence propre en Hz %%%
>> delta=10^(1/30);
>>
>> for m=1:91;
>> wo=0.1*(delta)^m;
>>
>> for j=1:N/2+1;
>> w(j)=2*pi*pas*(j-1);
>> %Fonction de transfert H en fonction de w, wo et epsi
> :
>> H(j)=1/(w(j)^2-(2*i*(wo)*(epsi)*w(j))-wo^2);
>> Xa(j)=A(j,1)*H(j);
>> end
>>
>>
>> for k=(N/2+2):N;
>> wc(k)=-2*pi*pas*(N+1-k);
>> %Fonction de transfert H en fonction de w, wo et epsi
> :
>> Hc(k)=1/(wc(k)^2-(2*i*(wo)*(epsi)*wc(k))-wo^2);
>> Xb(k)=A(k,1)*Hc(k);
>> end
>>
>>
>> X=vertcat(Xa,Xb);
>>
>> x=ifft(X);
>> ma=max(x);
>> Sd=[Sd;[ma wo]];
>> S=vertcat(Sd);
>> end
>> semilogx(S(:,2),S(:,1));
>> xlabel ('frequency (Hz)','FontSize',10); ylabel
>> ('displacement','FontSize',10);
>> title('Displacement response spectra','FontSize',14);
>>
>>
>>
>>
>>
>
> This example could illustrate your error:
>
> a(3) = 1
>
> The result will be
>
> a = [0 0 1]
>
> Because k gets values in the range [N/2+2),N], wc becomes a
> vector with N elements.
>
> Moreover, the soure code needs to be vectorized and avoid using
> loop. Here is just an example which could replace the 2nd and
> 3rd loops
>
> w = 2*pi*pas*[0:N/2 -N/2+1:-1];
> H = 1./(w.^2-2*i*wo*epsi*w-wo^2);
> X = A.* H';
>
> Anh Huy Phan
> RIKEN - BSI
>
>
>
>
>
>
>
>
>


Subject: Problem with

From: Huy

Date: 2 Jan, 2008 14:04:24

Message: 4 of 5

Assume that N = 8, you type this line in the command window:

  wc(N/2+2) = 1
  
-> wc =

     0 0 0 0 0 1

You get the vector wc with 8/2+2 = 6 elements

And then, type this line

  wc(N) = 1

-> wc =
  
     0 0 0 0 0 1 0 1

wc has 8 elements.

And the modified source code is as below

...
for m=1:91;
    wo=0.1*(delta)^m;
    
    %%%
    w = 2*pi*pas*[0:N/2 -N/2+1:-1];
    H = 1./(w.^2-2*i*wo*epsi*w-wo^2);
    X = A.* H';
    %%%

    x=ifft(X);
    ma=max(x);
    Sd=[Sd;[ma wo]];
    S=vertcat(Sd);
end
...

By the ways, there is another way without using for loop.

Anh Huy Phan
RIKEN - BSI


"Estelle" <tahoser45@hotmail.com> wrote in message
<477b8cdd$0$21146$7a628cd7@news.club-internet.fr>...
> Hi,
> Thanks for your help.
> I just do not understand why you said that wc has N values.
Because the
> values are included into N/2 and N. I know that it could be
evident for you
> but not for me and I really want to understand the problem.
> I try your code as well but it does not work.
>
> I am sorry if I am a bit insistent but I really want to
understand all
> steps!
>
> Thank you very much
> Best regards
> Estelle
>
>
> "Huy " <phananhhuy@mathworks.com> a écrit dans le message de
news:
> flfr61$mtu$1@fred.mathworks.com...
> > "Estelle" <tahoser45@hotmail.com> wrote in message
> > <477b5bff$0$21142$7a628cd7@news.club-internet.fr>...
> >> I do not konw why but I have a problem with one of my "for"
> > in my code.
> >> Please help me!!! LOL
> >>
> >> This is my code and my problem is in the 3rd "for" with
> > "k=(N/2+2):N"
> >> because the caculation of wc(k) give twice the number of
wc I
> > want. In fact
> >> I obtain N values of wc while I need N/2 values and between
> > 1 and N/2 all
> >> the values are zero. I do not understand why. Yesterday
when
> > I did few
> >> changes, I obtaines 3 times N values.
> >> Thank you for your help
> >>
> >> %%% vérifier longueur point%%%
> >> n=1;
> >> while 2^n<Zu.npts;
> >> n=n+1;
> >> end;
> >> corr=2^n;
> >> plus=corr-Zu.npts;
> >> ajout=zeros(plus,1);
> >> new=vertcat(Zu.trace,ajout);
> >> N=length(new);
> >>
> >> % Transformée de fourier discrète du signal : a(t)-> A(w):
> >> A=fft(new);
> >> % Aa=A(1:N/2);
> >> % Ab=A(N/2+1:N);
> >>
> >> %Damping de 5% :
> >> epsi=0.05;
> >>
> >> % %Complex :
> >> % i=sqrt(-1);
> >>
> >> %%% Matrice vide %%%
> >> Sd=[];
> >>
> >> %%%pas en fréquence %%%%
> >> pas=1/(N*Zu.delta);
> >>
> >> %%% fréquence propre en Hz %%%
> >> delta=10^(1/30);
> >>
> >> for m=1:91;
> >> wo=0.1*(delta)^m;
> >>
> >> for j=1:N/2+1;
> >> w(j)=2*pi*pas*(j-1);
> >> %Fonction de transfert H en fonction de w, wo et
epsi
> > :
> >> H(j)=1/(w(j)^2-(2*i*(wo)*(epsi)*w(j))-wo^2);
> >> Xa(j)=A(j,1)*H(j);
> >> end
> >>
> >>
> >> for k=(N/2+2):N;
> >> wc(k)=-2*pi*pas*(N+1-k);
> >> %Fonction de transfert H en fonction de w, wo et
epsi
> > :
> >> Hc(k)=1/(wc(k)^2-(2*i*(wo)*(epsi)*wc(k))-wo^2);
> >> Xb(k)=A(k,1)*Hc(k);
> >> end
> >>
> >>
> >> X=vertcat(Xa,Xb);
> >>
> >> x=ifft(X);
> >> ma=max(x);
> >> Sd=[Sd;[ma wo]];
> >> S=vertcat(Sd);
> >> end
> >> semilogx(S(:,2),S(:,1));
> >> xlabel ('frequency (Hz)','FontSize',10); ylabel
> >> ('displacement','FontSize',10);
> >> title('Displacement response spectra','FontSize',14);
> >>
> >>
> >>
> >>
> >>
> >
> > This example could illustrate your error:
> >
> > a(3) = 1
> >
> > The result will be
> >
> > a = [0 0 1]
> >
> > Because k gets values in the range [N/2+2),N], wc becomes
a
> > vector with N elements.
> >
> > Moreover, the soure code needs to be vectorized and avoid
using
> > loop. Here is just an example which could replace the 2nd
and
> > 3rd loops
> >
> > w = 2*pi*pas*[0:N/2 -N/2+1:-1];
> > H = 1./(w.^2-2*i*wo*epsi*w-wo^2);
> > X = A.* H';
> >
> > Anh Huy Phan
> > RIKEN - BSI
> >
> >
> >
> >
> >
> >
> >
> >
> >
>
>

Subject: Problem with

From: Estelle

Date: 2 Jan, 2008 16:06:59

Message: 5 of 5

OK thank you!
"Huy " <phananhhuy@mathworks.com> a écrit dans le message de news:
flg5l8$917$1@fred.mathworks.com...
> Assume that N = 8, you type this line in the command window:
>
> wc(N/2+2) = 1
>
> -> wc =
>
> 0 0 0 0 0 1
>
> You get the vector wc with 8/2+2 = 6 elements
>
> And then, type this line
>
> wc(N) = 1
>
> -> wc =
>
> 0 0 0 0 0 1 0 1
>
> wc has 8 elements.
>
> And the modified source code is as below
>
> ...
> for m=1:91;
> wo=0.1*(delta)^m;
>
> %%%
> w = 2*pi*pas*[0:N/2 -N/2+1:-1];
> H = 1./(w.^2-2*i*wo*epsi*w-wo^2);
> X = A.* H';
> %%%
>
> x=ifft(X);
> ma=max(x);
> Sd=[Sd;[ma wo]];
> S=vertcat(Sd);
> end
> ...
>
> By the ways, there is another way without using for loop.
>
> Anh Huy Phan
> RIKEN - BSI
>
>
> "Estelle" <tahoser45@hotmail.com> wrote in message
> <477b8cdd$0$21146$7a628cd7@news.club-internet.fr>...
>> Hi,
>> Thanks for your help.
>> I just do not understand why you said that wc has N values.
> Because the
>> values are included into N/2 and N. I know that it could be
> evident for you
>> but not for me and I really want to understand the problem.
>> I try your code as well but it does not work.
>>
>> I am sorry if I am a bit insistent but I really want to
> understand all
>> steps!
>>
>> Thank you very much
>> Best regards
>> Estelle
>>
>>
>> "Huy " <phananhhuy@mathworks.com> a écrit dans le message de
> news:
>> flfr61$mtu$1@fred.mathworks.com...
>> > "Estelle" <tahoser45@hotmail.com> wrote in message
>> > <477b5bff$0$21142$7a628cd7@news.club-internet.fr>...
>> >> I do not konw why but I have a problem with one of my "for"
>> > in my code.
>> >> Please help me!!! LOL
>> >>
>> >> This is my code and my problem is in the 3rd "for" with
>> > "k=(N/2+2):N"
>> >> because the caculation of wc(k) give twice the number of
> wc I
>> > want. In fact
>> >> I obtain N values of wc while I need N/2 values and between
>> > 1 and N/2 all
>> >> the values are zero. I do not understand why. Yesterday
> when
>> > I did few
>> >> changes, I obtaines 3 times N values.
>> >> Thank you for your help
>> >>
>> >> %%% vérifier longueur point%%%
>> >> n=1;
>> >> while 2^n<Zu.npts;
>> >> n=n+1;
>> >> end;
>> >> corr=2^n;
>> >> plus=corr-Zu.npts;
>> >> ajout=zeros(plus,1);
>> >> new=vertcat(Zu.trace,ajout);
>> >> N=length(new);
>> >>
>> >> % Transformée de fourier discrète du signal : a(t)-> A(w):
>> >> A=fft(new);
>> >> % Aa=A(1:N/2);
>> >> % Ab=A(N/2+1:N);
>> >>
>> >> %Damping de 5% :
>> >> epsi=0.05;
>> >>
>> >> % %Complex :
>> >> % i=sqrt(-1);
>> >>
>> >> %%% Matrice vide %%%
>> >> Sd=[];
>> >>
>> >> %%%pas en fréquence %%%%
>> >> pas=1/(N*Zu.delta);
>> >>
>> >> %%% fréquence propre en Hz %%%
>> >> delta=10^(1/30);
>> >>
>> >> for m=1:91;
>> >> wo=0.1*(delta)^m;
>> >>
>> >> for j=1:N/2+1;
>> >> w(j)=2*pi*pas*(j-1);
>> >> %Fonction de transfert H en fonction de w, wo et
> epsi
>> > :
>> >> H(j)=1/(w(j)^2-(2*i*(wo)*(epsi)*w(j))-wo^2);
>> >> Xa(j)=A(j,1)*H(j);
>> >> end
>> >>
>> >>
>> >> for k=(N/2+2):N;
>> >> wc(k)=-2*pi*pas*(N+1-k);
>> >> %Fonction de transfert H en fonction de w, wo et
> epsi
>> > :
>> >> Hc(k)=1/(wc(k)^2-(2*i*(wo)*(epsi)*wc(k))-wo^2);
>> >> Xb(k)=A(k,1)*Hc(k);
>> >> end
>> >>
>> >>
>> >> X=vertcat(Xa,Xb);
>> >>
>> >> x=ifft(X);
>> >> ma=max(x);
>> >> Sd=[Sd;[ma wo]];
>> >> S=vertcat(Sd);
>> >> end
>> >> semilogx(S(:,2),S(:,1));
>> >> xlabel ('frequency (Hz)','FontSize',10); ylabel
>> >> ('displacement','FontSize',10);
>> >> title('Displacement response spectra','FontSize',14);
>> >>
>> >>
>> >>
>> >>
>> >>
>> >
>> > This example could illustrate your error:
>> >
>> > a(3) = 1
>> >
>> > The result will be
>> >
>> > a = [0 0 1]
>> >
>> > Because k gets values in the range [N/2+2),N], wc becomes
> a
>> > vector with N elements.
>> >
>> > Moreover, the soure code needs to be vectorized and avoid
> using
>> > loop. Here is just an example which could replace the 2nd
> and
>> > 3rd loops
>> >
>> > w = 2*pi*pas*[0:N/2 -N/2+1:-1];
>> > H = 1./(w.^2-2*i*wo*epsi*w-wo^2);
>> > X = A.* H';
>> >
>> > Anh Huy Phan
>> > RIKEN - BSI
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>>
>>
>


Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com