Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
creating for loop.

Subject: creating for loop.

From: prabhakaran m

Date: 12 May, 2012 03:04:55

Message: 1 of 5

i am trying to do code the below formula in a loop

y = a0 + an*cos(n'*x) ...
       + bn*sin(n'*x); ...

in the formula the .... denotes the summation till n terms. here size of n is (35,1), x is (73,1),an and bn is(35,1) so y produces 73 values. when i do it in loops for me its little complicated

  l=cos(n'*x);
  k=sin(n'*x);
y=[];
 for n =1:35
     y(i)=0;
    for i = 1:73
y(i) =y(i)+(an(n)*l(n,i))+( bn(n)*k(n,i));
    end
    y(i)=y(i)+a0;
end
   
but i am getting error in this loop, what may be the problem

   

Subject: creating for loop.

From: Roger Stafford

Date: 12 May, 2012 04:28:25

Message: 2 of 5

"prabhakaran m" <prabha.gahon@gmail.com> wrote in message <jokk0n$3i1$1@newscl01ah.mathworks.com>...
> i am trying to do code the below formula in a loop
> y = a0 + an*cos(n'*x) ...
> + bn*sin(n'*x); ...
> ...........
> but i am getting error in this loop, what may be the problem
- - - - - - - - - -
  If an and bn are 1 by 35 row vectors and x is a 1 by 73 row vector, you can computer your y as:

 t = (1:35).'*x;
 y = a0 + an*cos(t) + bn*sin(t);

producing the 1 by 73 row vector y without using any for-loops.

  As for your given code, you seem to have your inner and outer for-loops reversed. The summations for calculating the y(i)'s should each take place for the 35 values of n. As you have it, on the first trip through the outer loop with n = 1, y(i) = 0 is given before i is even defined and matlab won't like that. Even if that were ignored, the inner loop will then try to call on y(2), y(3), etc., and those will also not have been defined. Also you have used 'n' in two different ways, as a vector and as an index for a for-loop. That is a no-no! If you prefer using for-loops, you will have to rewrite this code so that it doesn't encounter these problems.

Roger Stafford

Subject: creating for loop.

From: prabhakaran m

Date: 12 May, 2012 06:06:12

Message: 3 of 5

"Roger Stafford" wrote in message <jokot9$lsd$1@newscl01ah.mathworks.com>...
> "prabhakaran m" <prabha.gahon@gmail.com> wrote in message <jokk0n$3i1$1@newscl01ah.mathworks.com>...
> > i am trying to do code the below formula in a loop
> > y = a0 + an*cos(n'*x) ...
> > + bn*sin(n'*x); ...
> > ...........
> > but i am getting error in this loop, what may be the problem
> - - - - - - - - - -
> If an and bn are 1 by 35 row vectors and x is a 1 by 73 row vector, you can computer your y as:
>
> t = (1:35).'*x;
> y = a0 + an*cos(t) + bn*sin(t);
>
> producing the 1 by 73 row vector y without using any for-loops.
>
> As for your given code, you seem to have your inner and outer for-loops reversed. The summations for calculating the y(i)'s should each take place for the 35 values of n. As you have it, on the first trip through the outer loop with n = 1, y(i) = 0 is given before i is even defined and matlab won't like that. Even if that were ignored, the inner loop will then try to call on y(2), y(3), etc., and those will also not have been defined. Also you have used 'n' in two different ways, as a vector and as an index for a for-loop. That is a no-no! If you prefer using for-loops, you will have to rewrite this code so that it doesn't encounter these problems.
>
> Roger Stafford

Yes Sir, i found that simple way instead of going for loops. as u stated.
i have unnecessarily complicated myself when i have the simple way. thank you for your reply.

Subject: creating for loop.

From: steveh44

Date: 12 May, 2012 04:12:03

Message: 4 of 5

On May 11, 10:04 pm, "prabhakaran m" <prabha.ga...@gmail.com> wrote:
> i am trying to do code the below formula in a loop
>
> y = a0 + an*cos(n'*x) ...
>        + bn*sin(n'*x); ...
>
> in the formula the .... denotes the summation till n terms. here size of n is (35,1), x is (73,1),an and bn is(35,1) so y produces 73 values. when i do it in loops for me its little complicated
>
>   l=cos(n'*x);
>   k=sin(n'*x);
> y=[];
>  for n =1:35
>      y(i)=0;
>     for i = 1:73
> y(i) =y(i)+(an(n)*l(n,i))+( bn(n)*k(n,i));
>     end
>     y(i)=y(i)+a0;
> end
>
> but i am getting error in this loop, what may be the problem

 i think you are using i for loop which is imaginary number in matlab,
that is why you got errors.

you should use ii not i so not to use imaginary number but real
numbers

steve

Subject: creating for loop.

From: Roger Stafford

Date: 14 May, 2012 23:37:07

Message: 5 of 5

steveh44 <steve_h44@yahoo.com> wrote in message <a0b354c1-2b4d-44e1-ae53-719ff514e773@k41g2000yqf.googlegroups.com>...
> i think you are using i for loop which is imaginary number in matlab,
> that is why you got errors.
- - - - - - - - -
  Steve, it is incorrect to say "that is why you got errors." Prabhakaran's use of 'i' in his for loops will not produce an error message because of that. Any possible errors from this will only arise if he subsequently expects 'i' to then refer to the square root of -1 in the same program sequence. That would no longer work since he would have overwritten its original definition.

  However, it is a good policy to avoid the use of 'i' (and 'j' too) as anything other than the square root of -1 for that very reason.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

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.

Contact us