Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1996
*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 1996

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

Search the Archive

Re: Problem with If[] and %

  • To: mathgroup at smc.vnet.net
  • Subject: [mg5436] Re: Problem with If[] and %
  • From: von_Aschen at unidui.uni-duisburg.de (Harald von Aschen)
  • Date: Sat, 7 Dec 1996 00:26:11 -0500
  • Organization: Gerhard-Mercator-Universitaet GHS Duisburg Germany
  • Sender: owner-wri-mathgroup at wolfram.com

Hans Steffani <hans.steffani at e-technik.tu-chemnitz.de> wrote:

Hi Hans,

> If[ i^2+v^2>15, 15, i^2+v^2]
> Plot3D[ %,{i,-5,5},{v,-5,5}]
> 
> does what I want. But i^2+v^2 is only an example for an
> other more complex function which is derived in the steps
> before.
> 
> Therefore I test
> 
> i^2+v^2
> If[ % >15,15,%]
> Plot3D[ %,{i,-5,5},{v,-5,5}]
> 
> Which only leads to several errors.

Look on the results you get:

In[1]:=  If[ i^2+v^2>15, 15, i^2+v^2]

Out[1]=      2    2            2    2
         If[i  + v  > 15, 15, i  + v ]

In[2]:=  i^2+v^2
Out[2]=   2    2
         i  + v

In[3]:=  If[ % >15,15,%]
Out[3]=  If[% > 15, 15, %]

You see here the problem. You don't get the result you want to have.


My solution would be to use a replacement rule:


In[3]:=  i^2+v^2
Out[3]=   2    2
         i  + v

In[4]:=  If[ q >15,15, q] /. q -> %
Out[4]=      2    2            2    2
         If[i  + v  > 15, 15, i  + v ]

In[5]:= Plot3D[ %,{i,-5,5},{v,-5,5}]

You should use insteat of q any not used symbol (or the replacement
would be wrong). The q is then replaced in your expression by the
preceding result.

Kind regards, Harald
-- 
Harald von Aschen
eMail: von_aschen at uni-duisburg.de


  • Prev by Date: How can I use different symbols to reperesent my 2D data?
  • Next by Date: Re: select a range of elements from a nested list
  • Previous by thread: Re: Problem with If[] and %
  • Next by thread: Integration of exp(-az)*sigma(t-to)*dz ??