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