Message Boards Message Boards


Replacement rule function question/help

Posted 10 years ago
8 Replies
3 Total Likes
I'm building a model for breakthrough/adsorption.  I'm not well versed in the mathematica programming and learn best by reverse-engineering an existing code.  I learned most of my stuff from this demonstration.

My code has three major parts.  Everything uses a position and time dimension [z,t].

1)  An interation loop that makes a table (mytable) where the elements of the table are InterpolatingFunctions calculated and stored during the loop.

2)  The Adsorption isotherm equation.  This is set up as a "set delay" function, I think its called.  This function q[z_,t_] = K*c[z,t] is called up by part 3.

3)  The NDSolve section.  The main function is solving:
D[c[z,t],t] = mass balance + D[q[z,t],t].

I'm attempting to incorporate the interpolation functions, where the table part is chosen based on a function of z.  Therefore part 2 becomes
q[z_,t_] = K*c[z,t] * mytable[[ F(z) ]][c[z,t]]

When I run this program I get an error where it says that F(z) is not a integer/part specification.  When I added this part to the code, I'd assumed that the value of z would be "brought in" by the delay mechanism in the same way that q is brought into the NDSolve.  It seems instead that z is not a number "yet" and causes the error.

So, my question is:  Is it possible to do this in the way I have it set up?  Perhaps I have a syntax error or something.  OR, if it is not possible with this rough lookup table method, how would I be able to incorporate a FindRoot function within NDSolve where the zeroes depend on the value of c[z,t]?  ALSO, is there a good demonstration code which has similar functionality?

I can't provide my code right now, but I may be able to provide a trimmed down and readable version if needed.

Thank you
POSTED BY: Greg Cmarik
8 Replies
Hi Greg,
I hope you find the answer to your question. I am on the same path, and I am kind of beginner with Mathematica. If you successfully run your program do you mind guide me on how to get it worked.
Thank you.
POSTED BY: Timur Islamoglu
About " func [ z ] and zpart [ z ] is turning into func and zpart", the editor sometimes does things to special characters.

To enter code in a post, start a new line, click the "Mathematica Code" spikey, and type or paste in the code.
POSTED BY: Bruce Miller
I think the additional Part::pspec message comes when NDSolve evaluates its first argument, where z is a symbol.
To prevent that, clear any previous definitions of q and define it as q[z_?NumericQ, t_] := ...
POSTED BY: Ilian Gachevski
Posted 10 years ago

q[z_,t_] = stuff

differ from

q[z_,t_] := stuff

I thought the underscore did the same thing as :=, but I guess my reading of the documentation mislead me.

It does change my results, so that seems to have overcome my error.  Thanks! 

Of course, now I have new bugs to work out.
POSTED BY: Greg Cmarik
Try using SetDelayed (:=) instead of Set (=).
In[1]:= t = Range[5]; f[z_] := t[[Round[z]]]

In[2]:= f[1.8]

Out[2]= 2
POSTED BY: Ilian Gachevski
Posted 10 years ago
Thank you.  It doesn't seem to work as I still get the error as copied below.

Part::pspec: "Part specification 1+Round[1/5\ (55-27\ z)] is neither an integer nor a list of integers.  This is the same as before.

The way I changed my code is that I now have,

mytable[]=stuff;  "This builds a table of InterpolatingFunction <> which will later be used based on c[z,t] values";

zpart[z_]=Round[ func ]+1; "This produces an index from 1 to whatever as whole numbers";
q[z_,t_] = ( K*c[z,t] ) * ( mytable[[ zpart ]][c[z,t]] ); "This calculates uptake which is taken as a derivative in the NDSolve section below";

D[c[z,t],t] = mass balance + D[q[z,t],t];

Thank you for your help

This is strange, the code I have up there is not publishing as I typed it.  func [ z ] and zpart [ z ] is turning into func and zpart within the double brackets.  I assume mathematica may be doing something similar, but I don't know what it is doing behind the scenes.

Question for you.  Should the zPart[z_] be inside the NDSolve section, or outside as it is now?
POSTED BY: Greg Cmarik
 F(z)  is interpreted as F*z, a multiplication.  If you meant function F with an argument z, that is F, with square brackets.

POSTED BY: Bruce Miller
Posted 10 years ago
Thanks, but that was more for show, not so much as it is in my code.  Actually, it is mytable[[ 1 + Round[ (z - zo) / L ] ]] where zo and L are constants.  It is simply meant to calculate an index for the "part assignment".  The problem is as the code passes that section of code q[z_,t_] = K*c[z,t] * mytable[[ 1 + Round[ (z - zo) / L ]][c[z,t]]  I get an error that the function which uses z is not an integer and can't be used in the "part assignment".
POSTED BY: Greg Cmarik
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract