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
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=
>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.
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
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
________________________________
>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
---- 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