Group Abstract Group Abstract

Message Boards Message Boards

0
|
9.6K Views
|
10 Replies
|
7 Total Likes
View groups...
Share
Share this post:

[Solved] Functions with multiple variables and lines of calculations

I am almost a new user of Mathematica, my question is that how can I have something like a function or exactly a function that for example gets two inputs (a) and (b), then calculates a+b and a*b. In fact something that gets multiple inputs and does multiple calculations or tasks or in other words function with multiple variables and multiple tasks or lines of calculations.

POSTED BY: Davood Rafiei
10 Replies

The problem is solved.

POSTED BY: Davood Rafiei
Anonymous User
Anonymous User
Posted 5 years ago

You are not forced to use a function in the Wolfram Language like you are in other languages. You only "need a function" if you will call this as a function repeatedly. While loops and older "software programming styles" are something you'll learn you rarely need or should use.

You really need to read the Documentation (Help) on functions. It can't be explained quickly.

Your basic strategy for "accepting multiple inputs" is multiple function definitions of the same function (which WL allows) (and is very typically done). Now for function return you can return a List[] and you'll very typical to see things in lists going in and out of WL functions.

fun[x_]:={x,x}
fun[x_,y_]:={x+y,x*y}
fun[xi_List]:={Plus@@xi, Times@@xi}
...

You now have three function of the same name without any templating, three ways to invoke, and the last one may have wide output, you'd use Dimensions[fun[list]]] to know what size the output is.

POSTED BY: Anonymous User

Thanks for your explanation,

POSTED BY: Davood Rafiei
Posted 5 years ago

Davood:

I do not have time to rewrite and test your example code, but here is a simpler example:

a = 1;
b = 2;
c = 30;

While[
 a < c,
 a = a * b;
 ]
{a, c}

Can be turned into the following function:

f[a_Integer, b_Integer, c_Integer] := Module[
   {
    retVal
    },
   retVal = a;
   While[
    retVal < c,
    retVal = retVal * b;
    ];
   {retVal, c}
   ];

And then called as follows:

f[1, 2, 30]

Which returns:

{32, 30}

Note that I had to declare a privately scoped variable (retVal) for Module because parameters to functions are immutable. You do not always have to do this, but it is helpful to know how.

POSTED BY: Mike Besso

Thank you very much for your help and consideration.

POSTED BY: Davood Rafiei

In my Program I have these initial values : Pr, Mr, Er,r then I want to calculate the lines below :

While[Pr > 0, 

 f1 = -(1.47*Mr*
      Er*(1 + Pr/Er)*(1 + 4*\[Pi]*r^3*Pr*.08969/Mr))/(r^2*(1 - 
       2*Mr*1.47/r));
 g1 = 4*\[Pi]*r^2*Er*.08969;
 k1 = h*f1;
 m1 = h*g1;

 f2 = -(1.47*Mr*
      Er*(1 + (Pr + k1/2)/Er)*(1 + 
        4*\[Pi]*(r + h/2)^3*(Pr + k1/2)*.08969/Mr))/((r + h/2)^2*(1 - 
       2*Mr*1.47/(r + h/2)));
 g2 = 4*\[Pi]*(r + h/2)^2*(Er + m1/2)*.08969;
 k2 = h*f2;
 m2 = h*g2;

 f3 = -(1.47*Mr*
      Er*(1 + (Pr + k2/2)/Er)*(1 + 
        4*\[Pi]*(r + h/2)^3*(Pr + k2/2)*.08969/Mr))/((r + h/2)^2*(1 - 
       2*Mr*1.47/(r + h/2)));
 g3 = 4*\[Pi]*(r + h/2)^2*(Er + m2/2)*.08969;
 k3 = h*f3;
 m3 = h*g3;

 f4 = -(1.47*Mr*
      Er*(1 + (Pr + k3)/Er)*(1 + 
        4*\[Pi]*(r + h)^3*(Pr + k3)*.08969/Mr))/((r + h)^2*(1 - 
       2*Mr*1.47/(r + h)));
 g4 = 4*\[Pi]*(r + h)^2*(Er + m3)*.08969;
 k4 = h*f4;
 m4 = h*g4;
 Pr = Pr + 1/6*(k1 + 2*k2 + 2*k3 + k4);
 Mr = Mr + 1/6*(m1 + 2*m2 + 2*m3 + m4);
 Er = fromPressureToEnergy[Pr];
 r = r + h;
 ]

Then the result are the last Mr and r when the loop is finished.

Could you please help me that how I can write a function for this code and calculations?

POSTED BY: Davood Rafiei
Posted 5 years ago

Davood,

With the documentation and Mike's examples please try to do this yourself rather than asking someone else to do it for you.

A Chinese proverb attributed to Confucius: 耳到眼到手到心到, which roughly translates to "If I hear then I forget, if I see then I remember, if I do then I learn".

If you get stuck, post your attempt and we can help you resolve the issue.

POSTED BY: Rohit Namjoshi

Thank you very much for your attention and consideration. Very nice and important proverb especially for someone who is a beginner in something.

POSTED BY: Davood Rafiei
Posted 5 years ago

Davood:

To use your example, you can do something like:

In[14]:=
   f[a_, b_] := {a + b, a * b};
   f[2, 3]

Out[15]= {5, 6}

But this is the Wolfram Language, so you can do this at least a dozen different ways. Check out the link Rohit provided above.

POSTED BY: Mike Besso
Posted 5 years ago

Hi Davood,

Take a look at this tech note.

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