Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SetDelayed::write ... [x_] is protected

952 views
Skip to first unread message

Serych Jakub

unread,
Mar 24, 2009, 6:30:23 AM3/24/09
to
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?

Thanks in advance

Jakub

Sjoerd C. de Vries

unread,
Mar 25, 2009, 6:42:42 AM3/25/09
to
I don't get that error. Anyway, I woulden't use SetDelayed (:=) in
this case, but Set (=) as the replacement in the former case is not
performed until after the function is called with an actual value.

Cheers -- Sjoerd

On Mar 24, 12:30 pm, "Serych Jakub" <Ser...@panska.cz> 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=

Bill Rowe

unread,
Mar 25, 2009, 6:43:15 AM3/25/09
to
On 3/24/09 at 5:30 AM, Ser...@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.


Bob Hanlon

unread,
Mar 25, 2009, 6:45:10 AM3/25/09
to
sol = {{i1 -> 220/x}, {i2 -> 100 + x}};

myfun[x_] = 50*i1 + 2*i2 /. Flatten[sol]

2*(x + 100) + 11000/x

Bob Hanlon


On Tue, Mar 24, 2009 at 7:46 AM , 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?
>

> Thanks in advance
>
> Jakub

Richard Hofler

unread,
Mar 25, 2009, 6:45:43 AM3/25/09
to
Jakub,

You'll probably get several better solutions than this one. However,. . .

In[25]:= sol={{i1->220/x},{i2->100+x}};
(* [[..]] is the Part function *)
myfun[x_]:=50*sol[[1,2]]+2*sol[[2,2]]

(* If want to see what Flatten does, evaluate this line without the ";" *)
sol = Flatten@sol <mailto:Flatten@sol> ;
myfun[x]

Out[28]= 11000/x + 2(100+x)

Richard Hofler

________________________________

Bill Rowe

unread,
Mar 26, 2009, 6:22:49 AM3/26/09
to
On 3/25/09 at 5:42 AM, Ser...@panska.cz (Serych Jakub) wrote:

>Thanks for the Flatten, now it seem that it works (without any error
>messages), but in fact it doesn't work.

>sol = {{i1 -> 220/x}, {i2 -> 100 + x}};
>myfun[x_] := 50*i1 + 2*i2 /. Flatten[sol]

>(no errors here) but:

>In: myfun[1]

>Out: 11000/x + 2 (100 + x)

>and not 11202 which would be result for argument 1.

>So it cannot recognize (or connect together with the pattern x_) the
>= variable x in the result from sol.

What you have done is use SetDelayed. That means by design
evaluation of the

50*i1 + 2*i2 /. Flatten[sol]

part on the right hand side isn't done until myfun is called.
Consequently, nothing matches the pattern x_ and you get the
result you posted. What you need for this application is to
replace SetDelayed with Set which causes evaluation of the right
hand side to occur immediately. That is:

In[1]:= sol = {{i1 -> 220/x}, {i2 -> 100 + x}};
myfun[x_] = 50*i1 + 2*i2 /. Flatten[sol];

In[3]:= myfun[1]

Out[3]= 11202

Does what you want


Bob Hanlon

unread,
Mar 26, 2009, 6:24:25 AM3/26/09
to
Use the expression that I sent, i.e., use Set ( = ) rather than SetDelayed ( := ).


Bob Hanlon

---- Serych Jakub <Ser...@panska.cz> wrote:

=============


Thanks for the Flatten, now it seem that it works (without any error
messages), but in fact it doesn't work.

sol = {{i1 -> 220/x}, {i2 -> 100 + x}};
myfun[x_] := 50*i1 + 2*i2 /. Flatten[sol]
(no errors here) but:

In: myfun[1]

Out: 11000/x + 2 (100 + x)

and not 11202 which would be result for argument 1.

So it cannot recognize (or connect together with the pattern x_) the =
variable
x in the result from sol.

What to do with it?

Thanks

Jakub


> -----Original Message-----
> From: Bob Hanlon [mailto:han...@cox.net]
> Sent: Tuesday, March 24, 2009 12:46 PM
> To: Serych Jakub
> Cc: math...@smc.vnet.net
> Subject: RE: is protected
>
> sol = {{i1 -> 220/x}, {i2 -> 100 + x}};
>
> myfun[x_] = 50*i1 + 2*i2 /. Flatten[sol]
>

> 2*(x + 100) + 11000/x
>
>
>
> Bob Hanlon
>
>
> On Tue, Mar 24, 2009 at 7:46 AM , 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?
> >
> > Thanks in advance
> >
> > Jakub
>


--

Bob Hanlon


0 new messages