Re: SetDelayed::write ... [x_] is protected
- To: mathgroup at smc.vnet.net
- Subject: [mg97907] Re: SetDelayed::write ... [x_] is protected
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Wed, 25 Mar 2009 05:44:32 -0500 (EST)
On 3/24/09 at 5:30 AM, Serych at panska.cz (Serych Jakub) wrote: >Dear M users, I have solution of equations which is dependend on one >parameter lets say: sol={{i1->220/x}, {i2 -> 100+x}} and I need to >define function based on this solution with the x as argument lets >say: myfun[x_]:=50*220/x + 2 (100+x); >I'm trying to do it like this: >myfun[x_]:=50 * i1 + 2 * i2 /. sol >But it prints the error message: "SetDelayed::write ... [x_] is >protected". >Does anybody know what's the problem and how to define such >function? I don't get an error when using your code above. That is: In[1]:= sol = {{i1 -> 220/x}, {i2 -> 100 + x}}; myfun[x_] := 50*i1 + 2*i2 /. sol myfun[y] Out[3]= {2 i2+11000/x,50 i1+2 (x+100)} However, I think you probably didn't want a list as a result. So, I think this is more likely to be what you wanted: In[4]:= Clear[myfun] myfun[x_] := 50*i1 + 2*i2 /. Flatten[sol] myfun[y] Out[6]= 2 (x+100)+11000/x I note using global variables in a function definition isn't good programming practice. It is far to easy to assign values to those variables which will cause the function to return something other than what was intended. And when this occurs, it is often difficult to find the problem.