Thread Subject: looping problem

Subject: looping problem

From: Sunipa Som

Date: 4 Jul, 2011 13:00:27

Message: 1 of 5

Hallo,
I have to calculate the value of q1(below). Condition is if q1>50 then q1=50.
Now I am looping in this way
del=3*10^(3);
beta=0.25;
M=10;
e0=601.04;
for j= 1:50
q1=floor((sqrt(M)*((beta/2)-sqrt(((sqrt(j/M)-(beta/2)).^2)+(del/(e0))))).^2);
if q1>50
    q1=50;
else
q1=floor((sqrt(M)*((beta/2)-sqrt(((sqrt(j/M)-(beta/2)).^2)+(del/(e0))))).^2);
end
end

But then it is giving 9 values of q1 which are upto 50 but I need 50 values of q1.
How can i di this?
Thank you
Som

Subject: looping problem

From: Nasser M. Abbasi

Date: 4 Jul, 2011 13:42:01

Message: 2 of 5

On 7/4/2011 6:00 AM, Sunipa Som wrote:
> Hallo,
> I have to calculate the value of q1(below). Condition is if q1>50 then q1=50.
> Now I am looping in this way

> del=3*10^(3);
> beta=0.25;
> M=10;
> e0=601.04;
> for j= 1:50
> q1=floor((sqrt(M)*((beta/2)-sqrt(((sqrt(j/M)-(beta/2)).^2)+(del/(e0))))).^2);
> if q1>50
> q1=50;
> else
> q1=floor((sqrt(M)*((beta/2)-sqrt(((sqrt(j/M)-(beta/2)).^2)+(del/(e0))))).^2);
> end
> end
>
> But then it is giving 9 values of q1 which are upto 50 but I need 50 values of q1.
> How can i di this?
> Thank you
> Som

I do not understand the question.

The above loop will genrate ONE q1.

You are over-writing q1 at each loop step which do not make sense any way.

What 9 values of q1 are you talking about?


--Nasser

Subject: looping problem

From: Torsten

Date: 4 Jul, 2011 13:42:23

Message: 3 of 5

On 4 Jul., 15:00, "Sunipa Som" <sunipa_...@yahoo.com> wrote:
> Hallo,
> I have to calculate the value of q1(below). Condition is if q1>50 then q1=50.
> Now I am looping in this  way
> del=3*10^(3);
> beta=0.25;
> M=10;
> e0=601.04;
> for j= 1:50  
> q1=floor((sqrt(M)*((beta/2)-sqrt(((sqrt(j/M)-(beta/2)).^2)+(del/(e0))))).^2­);
> if q1>50
>     q1=50;
> else
> q1=floor((sqrt(M)*((beta/2)-sqrt(((sqrt(j/M)-(beta/2)).^2)+(del/(e0))))).^2­);
> end
> end
>
> But then it is  giving 9 values of q1 which are upto 50 but I need 50 values of q1.
> How can i di this?
> Thank you
> Som

j=[1:1:50];
q1=min(50,floor((sqrt(M)*((beta/2)-sqrt(((sqrt(j/M)-(beta/2)).^2)+(del/
(e0))))).^2­));

Best wishes
Torsten.

Subject: looping problem

From: dpb

Date: 4 Jul, 2011 13:51:17

Message: 4 of 5

On 7/4/2011 8:00 AM, Sunipa Som wrote:
> Hallo,
> I have to calculate the value of q1(below). Condition is if q1>50 then
> q1=50.
> Now I am looping in this way
> del=3*10^(3);
> beta=0.25;
> M=10;
> e0=601.04;
> for j= 1:50
> q1=floor((sqrt(M)*((beta/2)-sqrt(((sqrt(j/M)-(beta/2)).^2)+(del/(e0))))).^2);
> if q1>50
> q1=50;
> else
> q1=floor((sqrt(M)*((beta/2)-sqrt(((sqrt(j/M)-(beta/2)).^2)+(del/(e0))))).^2);
> end
> end
>
> But then it is giving 9 values of q1 which are upto 50 but I need 50
> values of q1. How can i di this?
> Thank you
> Som

As you've written it, you're overwriting the one value of q1 each loop
anyway...and unless my eyes fail me the second definition is the same as
the first. Use Matlab vector facilities

 >> e0=601.04;
 >> M=10;
 >> sqrM=sqrt(M);
 >> beta=0.25;
 >> del=3000;
 >> j=1:10;
 >>
q1=floor((sqrM*((beta/2)-sqrt(((sqrt(j./M)-(beta/2)).^2)+(del/(e0))))).^2);
 >> q1
q1 =
     44 45 46 46 47 48 49 50 50 51
 >> >> q1=min(q1,50)
q1 =
     44 45 46 46 47 48 49 50 50 50
 >

I shortened the j vector to 10 for brevity; same principle holds.

BTW, as a stylistic comment, don't use "j" as another variable in
Matlab--it and "i" are defined as the sqrt(-1) and overwriting those
will come to bite at some point when dealing w/ complex values. Better
to build the habit early in acquaintance w/ Matlab.

--

Subject: looping problem

From: Sunipa Som

Date: 4 Jul, 2011 21:05:10

Message: 5 of 5

dpb <none@non.net> wrote in message <iusggi$mfh$1@speranza.aioe.org>...
> On 7/4/2011 8:00 AM, Sunipa Som wrote:
> > Hallo,
> > I have to calculate the value of q1(below). Condition is if q1>50 then
> > q1=50.
> > Now I am looping in this way
> > del=3*10^(3);
> > beta=0.25;
> > M=10;
> > e0=601.04;
> > for j= 1:50
> > q1=floor((sqrt(M)*((beta/2)-sqrt(((sqrt(j/M)-(beta/2)).^2)+(del/(e0))))).^2);
> > if q1>50
> > q1=50;
> > else
> > q1=floor((sqrt(M)*((beta/2)-sqrt(((sqrt(j/M)-(beta/2)).^2)+(del/(e0))))).^2);
> > end
> > end
> >
> > But then it is giving 9 values of q1 which are upto 50 but I need 50
> > values of q1. How can i di this?
> > Thank you
> > Som
>
> As you've written it, you're overwriting the one value of q1 each loop
> anyway...and unless my eyes fail me the second definition is the same as
> the first. Use Matlab vector facilities
>
> >> e0=601.04;
> >> M=10;
> >> sqrM=sqrt(M);
> >> beta=0.25;
> >> del=3000;
> >> j=1:10;
> >>
> q1=floor((sqrM*((beta/2)-sqrt(((sqrt(j./M)-(beta/2)).^2)+(del/(e0))))).^2);
> >> q1
> q1 =
> 44 45 46 46 47 48 49 50 50 51
> >> >> q1=min(q1,50)
> q1 =
> 44 45 46 46 47 48 49 50 50 50
> >
>
> I shortened the j vector to 10 for brevity; same principle holds.
>
> BTW, as a stylistic comment, don't use "j" as another variable in
> Matlab--it and "i" are defined as the sqrt(-1) and overwriting those
> will come to bite at some point when dealing w/ complex values. Better
> to build the habit early in acquaintance w/ Matlab.
>
> --

Thank you for your help. Now I got it.

With Regards,
Som

Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
looping problem Sunipa Som 4 Jul, 2011 09:04:13
rssFeed for this Thread

Contact us at files@mathworks.com