Message Boards Message Boards


How to write a time efficient user-defined function with local variables?

Posted 2 years ago
3 Replies
1 Total Likes

I want to write a 'Gauss' function in my main program like the way its written in Matlab. In the following example I want to pass the variables ('aar' & 'es' are matrices and other are scalars) "aar, es, x1value, x2value, x3value, x4value, y1value, y2value, y3value, y4value" like given below. I want the output to store in detjacobs & Invdetjacobs. Kindly suggest me the correct way.

[detjacobs, Invdetjacobs] = Gauss[aar, es, x1value, x2value, x3value, x4value, y1value, y2value, y3value, y4value]

r = aar[[i]]; s = es[[j]];

shape1 = ((1. - r) (1. - s))/4.; shape2 = ((1. + r) (1. - s))/4.; shape3 = ((1. + r) (1. + s))/4.; shape4 = ((1. - r) (1. + s))/4.; dhdr1 = 1./4. (-1. + s); dhdr2 = (1. - s)/4.; dhdr3 = 1./4. (1. + s); dhdr4 = -(1. + s)/4.; dhds1 = 1./4. (-1. + r); dhds2 = 1./4. (-1. - r); dhds3 = (1. + r)/4.; dhds4 = (1. - r)/4.;

 detjacobs = {{dhdr1 x1value + dhdr2 x2value + dhdr3 x3value + 
  dhdr4 x4value, 
 dhdr1 y1value + dhdr2 y2value + dhdr3 y3value + 
  dhdr4 y4value}, {dhds1 x1value + dhds2 x2value + dhds3 x3value +
   dhds4 x4value, 
 dhds1 y1value + dhds2 y2value + dhds3 y3value + dhds4 y4value}};

Invdetjacobs = 1/detjacobs;


3 Replies

Take a look at the documentation for Module and Block for defining local variables. If evaluation speed is important, see Hope this helps, Ian

Anonymous User
Anonymous User
Posted 2 years ago

Mathematica has at least a few ways to do it.

I assume you already looked in Help and found your answers. And I'm unsure what your Guass function is (what book what equation) (there must be many guass equations).

I do know in the old Mathematica book it could be hard to find the right section in Help (if one looked up Function one got a lesson on that keyword). However in the new Help there are tutorial related guides and suggested other readings: I assume you've already found your answer.

Value assignment:

{detjacobs, Invdetjacobs} = Gauss[aar_Integer, es_, x1value_, x2value_, x3value_, x4value_, y1value_, y2value_, y3value_, y4value_] := Block[ {i=1,j=1}, Do[ Do[ r = aar[[i]]; s = es[[j]],imax],jmax] ]

where the last expr evaluated is the return value (Return[value] can be used). I used Block because


does not include any facility for "local variables" which you may be used to (it actually DOES but that's a lesson on Context[]).

Delayed assignment (value is evaluated each time variable as a value appears)

{detjacobs, Invdetjacobs} := Gauss ...

Now note aar_Integer - it says this function is ignored if Guass is called with something other than. There can be many Guass each matching certain variable types or even certain patterned expressions.

POSTED BY: Anonymous User

Thanks Mr. John. This time I uploaded the notebook file. Kindly look into it. I have so many local variables. Will "Block" and" Return" commands consume more time? Actually, I am trying to write a time-efficient program.So, I thought of using this function in between to minimize time. In Matlab, there is room to define local variables while maintaining time efficiency. How to doit in mathematica?

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract