Group Abstract Group Abstract

Message Boards Message Boards

0
|
6.7K Views
|
5 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Functional programming issues

Posted 10 years ago

Hi,

Just started the look at some more complete Mathematica programming and got stuck with implementing functions. Could anyone show me how to execute few computation in a single function?

  PTpcM[GasSGHC_,Co2Mol_,H2sMol_,N2Mol_,TpcmCorr_,PpcmCorr_,Oil_:True]:=
(*If checking if Oil optional is True then does PpcHC computation for True and then False*)
If[Oil=True, PpcHC=677+15*GasSGHC-37.5*GasSGHC^2; TpcHC=168+325*GasSGHC-12.5*GasSGHC^2,  (*Both PpcHC and TpcHC should be computed here if Oil= True*)
   PpcHC=706+51.7*GasSGHC-11.1*GasSGHC^2; 
   TpcHC=187+330*GasSGHC-71.5*GasSGHC^2;] (*Computed if Oil=False*)

(*Below part needs to be computed in the function*)
   TpcM=(1-Co2Mol-H2sMol-N2Mol)*TpcHC+227.3*N2Mol+547.6*Co2 + 672.4*H2s;
   PpcM= (1-Co2Mol-H2sMol-N2Mol)*PpcHC+493*N2Mol+1071*Co2+1306*H2s;
   e = 120 * ((Co2Mol+ H2sMol) ^ 0.9 - (Co2Mol+ H2sMol) ^ 1.6) + 15 * (H2sMol^ 0.5 - H2sMol ^ 4);
   TpcmCorr = TpcM - e;
   PpcmCorr = PpcM * (TpcM - e) / (TpcM + H2s * (1 - H2s) * e);

(*Function stops here*)

Isn't this possible, what am I doing wrong? So far, I didn't find any example showing Application/programs with long functions, but rather short one, why is that?

Thank you for the help and direction.

Regards

Hugh

POSTED BY: hugh trifol
5 Replies
Posted 10 years ago

Hugh,

Thanks for the nice words!

It seems to me that most recent books, tutorials, and online discussions focus on the syntax of programming, and not much on the semantics of programming nor on best practices to use during the actual programming process. More attention was paid to these subjects in the past by the likes of Harlan Mills and Edsger Dijkstra (to name just the two that come to mind immediately, there are many others). Oh, there's also "Structure and Interpretation of Computer Programs", a textbook that is freely available online.

The working backward technique I demonstrated is an example of "stepwise refinement", which might turn up something interesting if you do a search.

Be prepared to spend many hours with the papers and books you will find if you follow these threads...

Regards, Eric

POSTED BY: Eric Rimbey
Posted 10 years ago

Eric,

thank you for taking the time to describe in so much details. It works great. Is there any tutorial or book that explains such topics? Did not find any, only short functions but nothing like you propose.

Regards

Hugh

POSTED BY: hugh trifol
Posted 10 years ago

I would suggest you work backwards. To return two results, you need to combine them into a single structure. I'll assume a List works:

PTpcM[] := {TpcmCorr, PpcmCorr}

You could define functions for TpcmCorr and PpcmCorr, but I'll just do a direct substitution, and I'll explode out the pieces onto separate lines to make things easier to follow:

PTpcM[] :=
 {
  TpcM - e,
  PpcM*(TpcM - e)/(TpcM + H2s*(1 - H2s)*e)
  }

Now, I do further replacements at the next level. The syntax highlighting tells me which variables have been defined and which I need to still define, which is why this work backward technique is nice. You could use either Module or With, and I'll choose With.

PTpcM[] :=
 With[
  {
   e = 120*((Co2Mol + H2sMol)^0.9 - (Co2Mol + H2sMol)^1.6) + 
     15*(H2sMol^0.5 - H2sMol^4),
   TpcM = (1 - Co2Mol - H2sMol - N2Mol)*TpcHC + 227.3*N2Mol + 
     547.6*Co2 + 672.4*H2s,
   PpcM = (1 - Co2Mol - H2sMol - N2Mol)*PpcHC + 493*N2Mol + 
     1071*Co2 + 1306*H2s
   },
  {
   TpcM - e,
   PpcM*(TpcM - e)/(TpcM + H2s*(1 - H2s)*e)
   }
  ]

I don't see a definition for H2s or Co2, so I'll just leave those for now. You'll have to figure out where they come from eventually. I see some of the other variables as inputs to your original declaration, so I'll add those along with another With for the non-input variables:

PTpcM[Co2Mol_, H2sMol_, N2Mol_] :=
 With[
  {
   TpcHC = 
    If[Oil, 168 + 325*GasSGHC - 12.5*GasSGHC^2, 
     187 + 330*GasSGHC - 71.5*GasSGHC^2],
   PpcHC = 
    If[Oil, 677 + 15*GasSGHC - 37.5*GasSGHC^2, 
     706 + 51.7*GasSGHC - 11.1*GasSGHC^2]
   },
  With[
   {
    e = 120*((Co2Mol + H2sMol)^0.9 - (Co2Mol + H2sMol)^1.6) + 
      15*(H2sMol^0.5 - H2sMol^4),
    TpcM = (1 - Co2Mol - H2sMol - N2Mol)*TpcHC + 227.3*N2Mol + 
      547.6*Co2 + 672.4*H2s,
    PpcM = (1 - Co2Mol - H2sMol - N2Mol)*PpcHC + 493*N2Mol + 
      1071*Co2 + 1306*H2s
    },
   {
    TpcM - e,
    PpcM*(TpcM - e)/(TpcM + H2s*(1 - H2s)*e)
    }
   ]
  ]

And I would continue on in this fashion, adding input variables and With wrappers, until I had a complete function.

POSTED BY: Eric Rimbey
Posted 10 years ago

Nasser,

thank you for the explanations, I ll follow them. The function is supposed to return the results of those two expressions

TpcmCorr = TpcM - e
PpcmCorr = PpcM * (TpcM - e) / (TpcM + H2s * (1 - H2s) * e)

How can I get those two results?

Regards

Hugues

POSTED BY: hugh trifol

You have many problems. May be you should practice on simple functions first or go over tutorials.

First you need to make a Module. Syntax is

        foo[p_]:=Module[{localStuff}, code;code;code]

You can't write

       foo[p_]:=code1;code2;

and expect code1 and code2 to be in the same function foo since Mathematica will stop at code1 in the this example.

Also you have many local variables that are not defined local. These should be local to the module. You are also assigning values to input parameters inside the function. This does not work in Mathematica by default. Input parameters are read-only by default. You are also using H2s and Co2 in the RHS of some assignments, but these are not defined anywhere and not passed in.

Look at the coloring of the symbol in the notebook. The coloring helps you identify which symbol is local, which is input parameter and which is already defined somewhere else in the notebook (outside the function). Do Edit->Preferences->Appearance to see the syntax coloring used.

What is the function supposed to return? Final expression in a function is its return value. But do not put a ";" at the end.

hth

POSTED BY: Nasser M. Abbasi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard