Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2003
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Problem with If statement in a Do or a While loop

  • To: mathgroup at smc.vnet.net
  • Subject: [mg39495] Re: Problem with If statement in a Do or a While loop
  • From: Omega Consulting <info at omegaconsultinggroup.com>
  • Date: Wed, 19 Feb 2003 04:43:17 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

Replace

If[ (   (y > (c10 - (c10*tol) ) || y < (c10 + (c10*tol)) ) && t10 == 0 ) ,
   ( t10 = \[Theta]; Print["t10 found"]; Continue[] ) ,
   (Print["t10 not found"]; Continue[])
]

with

If[ (   (y > (c10 - (c10*tol) ) || y < (c10 + (c10*tol)) ) && t10 == 0 ) ,
   ( t10 = \[Theta]; Print["t10 found"]; Continue[] ) ,
   (Print["t10 not found"]; Continue[]),
   Print[((y>(c10-(c10*tol)) || y<(c10+(c10*tol))) && t10==0)]; Abort[]
]

In[5]:= n=1; Fig[n]=Diffusion2[1,0.5+n/10,n/4,130,1,0.1,0.9]

{0.001}>{{0.036}}||{0.001}<{{0.044}}

Out[5]=$Aborted

The test doesn't evaluate to True or False because < and > can't compare lists.

At 03:33 AM 2/17/2003, A.Reinstaller at MERIT.unimaas.nl wrote:
>Hi,
>
>thanks and sorry for providing incomplete information. I have checked your
>example on the file and it must indeed be the case that the statement cannot
>be checked, BUT simplifying the condition in the IF statement afterwards
>does not help. A simple y>c10, where y is the result of the interpolating
>function and c10 is another value extracted from there does not "fire", even
>though I have checked with a Print[y],Print[c10] in the loop whether the
>values are correctly assigned which is the case.
>
>I'm just puzzled... I don't get it where I have it wrong - on such a trivial
>issue. The problem suggests that the parameter values correctly assigned
>beforehand, are not taken over into the If evaluation, but I can't believe
>that that's the case.
>
>
>
>\!\(\(Diffusion2[\[Omega]_, q1_, cvet_, \ Tmax_, \ rate_, \ low_, \ high_]
>:=
>     \[IndentingNewLine]Module[{D, C, q2, x1, x2, y1, y2, w1, w2, u1, u2, px,
>
>         py, cy, e, \[Alpha], \ S, \ t10, t90, \ c10, c90, y, tol, \
>         \[CapitalDelta]t, stept, \ \[Theta]},
>       \[IndentingNewLine]\[IndentingNewLine]D = {{\(-1\), 1}, {1, \(-1\)}};
>       \[IndentingNewLine]C\  = {{1, \(-1\)}, {\(-1\), 1}};
>       \[IndentingNewLine]\n\n\t\t\n\t\tw1 = {\@\(1 - px\), \@\(1 - py[t]\)};
>
>       \[IndentingNewLine]w2 = {\@\(2 - px\), \@\(2 - py[t]\)}; \n\n\t\t
>       \[IndentingNewLine]q2 = 1 - q1;
>       \[IndentingNewLine]\[Alpha] = 2  \[Omega] - 1;
>       \[IndentingNewLine]\[IndentingNewLine]\n\t\t
>       \[IndentingNewLine]u1 =
>         q1*\[Alpha]*C . {x1[t], y1[t]} + q2*C . {x2[t], y2[t]} + w1;
>       \[IndentingNewLine]u2 =
>         q1*D . {x1[t], y1[t]} + q2*\[Alpha]*C . {x2[t], y2[t]} + w2;
>       \[IndentingNewLine]\[IndentingNewLine]veblen07 =
>         \[IndentingNewLine]{
>           \[IndentingNewLine]\(x1'\)[t] \[Equal]
>             x1[t]*{1 - x1[t], \(-y1[t]\)} . u1,
>           \[IndentingNewLine]\(y1'\)[t] \[Equal]
>             y1[t]*{\(-x1[t]\), 1 - y1[t]} . u1,
>           \[IndentingNewLine]\[IndentingNewLine]\(x2'\)[t] \[Equal]
>             x2[t]*{1 - x2[t], \(-y2[t]\)} . u2,
>           \[IndentingNewLine]\(y2'\)[t] \[Equal]
>             y2[t]*{\(-x2[t]\), 1 - y2[t]} . u2,
>           \[IndentingNewLine]\[IndentingNewLine]\(py'\)[t] \[Equal]
>             \(-rate\)*\((py[t] - cy)\)*
>               \((y1[t] + y2[t])\)\[IndentingNewLine]};
>       \[IndentingNewLine]\[IndentingNewLine] (*\ Tmax = 25; \ *)
>         \[IndentingNewLine]px = 0.5; cy = 0.9; \[IndentingNewLine]e = 0.001;
>
>       \[IndentingNewLine]\[IndentingNewLine]IvaluesIncome = {
>           \[IndentingNewLine]x1[0] \[Equal] 1 - e, y1[0] \[Equal] e,
>           \[IndentingNewLine]x2[0] \[Equal] 1 - e, y2[0] \[Equal] e, \
>           \[IndentingNewLine]py[0] \[Equal] 1};
>       \[IndentingNewLine]\[IndentingNewLine]solutionIncome =
>         NDSolve[Join[veblen07, IvaluesIncome], {x1, x2, y1, y2, py}, {t, 0,
>             Tmax}]; \[IndentingNewLine]\n\t\t
>       S = {\(\((q1*y1[t] + q2*y2[t])\) /. solutionIncome\) /. t -> Tmax}; \n
>       \t\ty = {\(\((q1*y1[t] + q2*y2[t])\) /. solutionIncome\) /. t -> 0};
>\n
>       \t\tc10 = S*low; \ c90 = S*high; \n\t\tt10 = 0; t90 = 0; \n\t\t
>       \[CapitalDelta]t = 0; \n\t\ttol = 0.1; \n\t\n\t\ \ stept =
>Tmax*0.0001;
>       \ \n\t\n\t\n\ \ \t
>       Do[\ \n\t\t\t{
>           y = \(\((q1*y1[t] + q2*y2[t])\) /. solutionIncome\) /.
>               t -> \[Theta]; \ \ \n\t\t\t\t\n\t\t\t\t
>           If[\ \((\ \ \
>               \((y > \((c10 - \((c10*tol)\)\ )\) || \
>                     y < \((c10 + \((c10*tol)\))\)\ )\)\  && \ t10\  == 0\ \
>)
>               \)\ , \((\ t10 = \[Theta]; \ Print["\<t10 found\>"]; \
>               Continue[]\ )\)\ , \
>             \((Print["\<t10 not found\>"]; \ Continue[])\)\ \ \ ];
>\n\t\t\t\t
>           \n\t\t\t\t
>           If[\ \((\ \ \
>               \((\ y > \((c90 - \((c90*tol)\))\)\  || \
>                     y < \((c90 + \((c90*tol)\))\)\ )\)\  && \
>                 t90\  == 0\ \ \ )\)\ ,
>             \((\ \ t90 = \[Theta]; \ Print["\<t90 found\>"]; \
>Continue[])\),
>             \ \((\ Print["\<t90 not found\>"]; \ Continue[])\)\ \ ]; \n
>           \t\t\t\t\n\t\t\t\t
>           If[\ \((t10 > 0)\)\  && \ \((t90 > 0)\),
>             \((\[CapitalDelta]t = t90 - t10; \ Continue[])\)\ ]; },
>\n\t\t\t\n
>         \t\t\t{\[Theta], 0, Tmax, stept}]; \t\n\t\t\n\t\t\n\t\t
>       \[IndentingNewLine]Plot[{
>             Evaluate[\((q1*y1[t] + q2*y2[t])\) /. solutionIncome]}, {t, 0,
>             Tmax}, \ Frame \[Rule] True,
>           PlotLabel \[Rule] "\<Diffusion path \>", PlotRange \[Rule] All,
>           PlotStyle \[Rule] {style1[cvet]}]\n\t\t\t\t\n\t\t
>         Print["\<q2 \>", \ \ q2, "\<  omega = \>"\ , \ \[Omega]]; \n\t\t
>       Print["\<Saturationlevel = \>", \ S]; \ \n\t\t
>       Print["\<Time elapsed to reach t10 = \>", \ t10]; \n\t\t
>       Print["\<Time elapsed to reach t90 = \>", \ t90]; \n\t\t
>       Print["\<Diffusion speed \[CapitalDelta]t = \>", \ \[CapitalDelta]t];
>\t
>       \n\n\t\t\n\t\t\n\[IndentingNewLine]]\n\n\n\t\t\)\)
>
>         Do[{Fig[n]=Diffusion2[1,0.5+n/10,n/4,130,1,0.1,0.9]},{n,1,4,1}];
>
>
>
>-----Original Message-----
>From: Andrzej Kozlowski [mailto:akoz at mimuw.edu.pl]
To: mathgroup at smc.vnet.net
>To: mathgroup at smc.vnet.net
>Subject: [mg39495]  Problem with If statement in a 
>Do or a
>While loop
>
>
>
>Because you do not provide the values needed to run your program it is
>impossible to answer your question without very detailed examination of
>your code. Perhaps somebody will have the time to do that. However, let
>me point out that If may produce no output when Mathematica can't
>verify whether a statement is true or untrue for example:
>
>
>If[x>2,Print[3],Print[4]]
>
>If[x>2,Print[3],Print[4]]
>
>Since Mathematica does not know the value of x it just returns the
>input. In such cases you cna use If with an extra argument:
>
>
>If[x>2,Print[3],Print[4],Print[5]]
>
>
>5
>
>This may or may not apply to your case.
>
>Andrzej Kozlowski
>Yokohama, Japan
>http://www.mimuw.edu.pl/~akoz/
>http://platon.c.u-tokyo.ac.jp/andrzej/
>
>
>On Saturday, February 15, 2003, at 03:42 PM,
>A.Reinstaller at MERIT.unimaas.nl wrote:
>
> > There is a problem I continue to have, perhaps one knows what's wrong.
> >  I
> > try to check the values generated by an interpolating function. I do
> > so by
> > using a Do loop in which I ask whether the function has reached some
> > value
> > within a small intervall and if so to write the time tag of the first
> > hit
> > out. Now, when running the program it does not step into the IF
> > evaluations.
> > I have added an else branch to be sure that even if the condition was
> > not
> > true something would be written out. I have also checked whether the
> > values
> > of the interpolating function are correctly extracted as well as
> > whether
> > they were really lying in the intervall asked for in the IF statement.
> > In
> > top of that I varied the stepsize to a minimal and increased the
> > margins of
> > the intervall. I used also a While instead of a Do loop. But in any
> > case
> > neither the THEN nor the ELSE branch "fired", telling me that the IF
> > statement is not checked. WHY????
> >
> >> Is there some restriction for the use of IF clauses in loops in
> >> Mathematica? Otherwise I could not explain why nothing happens. I
> >> checked
> >> in the manual but nothing was mentioned.
> >>
> >> Thanks for your forthcoming help. Andreas
> >>
> >> Here is the code:
> >>
> >> solutionIncome=
> >> NDSolve[Join[veblen07,IvaluesIncome],{x1,x2,y1,y2,py},{t,0,Tmax}];
> >>
> >>      S={(q1*y1[t]+q2*y2[t])/.solutionIncome/.t->Tmax};
> >>      c10=S*0.01; c90=S*0.9;
> >>      t10=0;t90=0; \[CapitalDelta]t=0;
> >>      tol=0.1; stept=Tmax*0.0001;
> >>
> >>      Do[ {
> >>              y=(q1*y1[t]+q2*y2[t])/.solutionIncome/.t->\[Theta];
> >>              If[ (   (y>(c10-(c10*tol) )|| y<(c10+(c10*tol)) ) && t10 ==0
> >> ) ,( t10=\[Theta]; Print["t10 found"]; Continue[] ) , (Print["t10 not
> >> found"]; Continue[])   ];
> >>              If[ (   ( y>(c90-(c90*tol)) || y<(c90+(c90*tol)) ) && t90
> >> ==0   ) ,(t90=\[Theta]; Print["t90 found"]; Continue[]), (Print["t90
> >> not
> >> found"]; Continue[])  ];
> >>              If[ (t10>0) && (t90>0),(\[CapitalDelta]t=t90-t10;
> >> Continue[]) ];
> >>              },
> >>                  {\[Theta],0,Tmax,stept}];
> >>
> >>
> >>
> >
> >
> >

--------------------------------------------------------------
Omega Consulting
"The final answer to your Mathematica needs"
http://omegaconsultinggroup.com



  • Prev by Date: Re: Simplify a module
  • Next by Date: RE: Simplify a module
  • Previous by thread: RE: Re: Problem with If statement in a Do or a While loop
  • Next by thread: Plotting problem