Group Abstract Group Abstract

Message Boards Message Boards

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

Posted 6 years ago

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]


Do[
Do[
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;

,{j,1,4}];

,{i,1,4}];
Attachments:
3 Replies

Take a look at the documentation for Module and Block for defining local variables. If evaluation speed is important, see https://blog.wolfram.com/2011/12/07/10-tips-for-writing-fast-mathematica-code/. Hope this helps, Ian

POSTED BY: Ian Williams
Anonymous User
Anonymous User
Posted 6 years ago
POSTED BY: Anonymous User
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard