# Defining a function that that evaluates a function to obtain a value not supplied as an optional argument

I have a function like this

CentralDifferentMethod[α_, β_, α1_, α2_, λ_, v0_, dt_, t_]

Now, I want to add a new parameter

u0 = u[α_, β_, λ_]

as an optional without rewriting the function. Even I have tried

CentralDifferentMethod[α_, β_, α1_, α2_, λ_, u0:_u[α_, β_, λ_], v0_, dt_, t_]

but it does not work. Any suggestions ?

### Edit

To clarify my question, I post a part of my code:

CentralDifferentMethod[α_, β_, α1_, α2_, λ_, v0_, dt_, t_]:=
Module[{sMatrix, mMatrix, dMatrix, disp, velo, accel, result, index, reff,
effStiffnessMatrix},
sMatrix = lumpingMatrix[stiffnessMatrix[α]];
mMatrix = lumpingMatrix[massMatrix[β]];
dMatrix = lumpingMatrix[dampMatrix[α1, α2]];
disp[0] = u[α, β, λ]; (*initial displacement*)
velo[0] = v0;
accel[0] = -Inverse[mMatrix].(dMatrix.velo[0] + sMatrix.disp[0]);
effStiffnessMatrix = 1/dt^2 mMatrix + 1/(2 dt) dMatrix;
For[index = 0, index < Floor[t/dt], index++,
reff =
sMatrix.disp[index] + 1/(2 dt) dMatrix.disp[index - 1] +
mMatrix.(2 disp[index] - disp[index - 1])*1/dt^2;
disp[index + 1] = reff/Diagonal[effStiffnessMatrix];
result = Insert[result, disp[index + 1]];
result]

In most cases, the default initial displacement, disp[0], is known to be u[α, β, λ]. However, I sometimes want to use another initial condition, disp[0] = u0, where U0 is an arbitrary vector given to CentralDifferentMethod as an argument.

Any suggestions?

• What do you mean by "without rewriting the function"? What is going to happen with this $u0?$ – Igor Rivin Sep 18 '14 at 23:31
• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – Dr. belisarius Sep 18 '14 at 23:34
• @IgorRivin Because I use the default value for u() and now I want it can be changed. – Viet Tran Sep 18 '14 at 23:42
• It is not at all clear what you are trying to do here. I am going to close this question until it is clarified, which I surely hope it will be as it will probably interest me. The expression u[α_, β_, λ_] is not a function. You need to include examples of the input and output of this function. As it stands you only have pseudo-code for the left-hand-side. – Mr.Wizard Sep 19 '14 at 15:26
• Since your question is closed, I will have to write my answer as comment, which will make it hard to read. Sorry about that. The easiest way to accomplish what you ask for requires a some rewriting, but not much. First change your current definition to CentralDifferentMethod[α_, β_, α1_, α2_, λ_, v0_, dt_, t_, uo_] := Module{...}, ... disp[0] = u0; ...] and then add a 2nd definition CentralDifferentMethod[α_, β_, α1_, α2_, λ_, v0_, dt_, t_] := CentralDifferentMethod[α, β, α1, α2, λ, v0, dt, t, u[α, β, λ]]. – m_goldberg Sep 20 '14 at 4:01

I'm not sure I understand the question, so will try this to see if it's in the right direction. Consider the following collection of definitions for a function f[ ]

f[x_] := x^2;
f[x_, y_] := f[x] + y^3;
f[x_, y_, z_] := f[x, y] + f[x, z] + Sqrt[z];
f[x_, y_, z_, s_] := x y z s;

Every time you add a new parameter, you need only define how you want it to act with that new parameter. As you can see from the above, the functions can (though need not) have any relationship with each other.

The easiest way to accomplish what you ask for requires a some rewriting, but not much. First change your current definition to

CentralDifferentMethod[α_, β_, α1_, α2_, λ_, v0_, dt_, t_, u0_] :=
Module[{sMatrix, mMatrix, dMatrix, disp, velo, accel, index, reff, effStiffnessMatrix},
sMatrix = lumpingMatrix[stiffnessMatrix[α]];
mMatrix = lumpingMatrix[massMatrix[β]];
dMatrix = lumpingMatrix[dampMatrix[α1, α2]];
disp[0] = u0;
velo[0] = v0;
accel[0] = -Inverse[mMatrix].(dMatrix.velo[0] + sMatrix.disp[0]);
effStiffnessMatrix = 1/dt^2 mMatrix + 1/(2 dt) dMatrix;
For[index = 0, index < Floor[t/dt], index++,
reff = sMatrix.disp[index] + 1/(2 dt) dMatrix.disp[index - 1] +
mMatrix.(2 disp[index] - disp[index - 1])*1/dt^2;
disp[index + 1] = reff/Diagonal[effStiffnessMatrix];
Insert[result, disp[index + 1]]]

and then add a second definition

CentralDifferentMethod[α_, β_, α1_, α2_, λ_, v0_, dt_, t_] :=
CentralDifferentMethod[α, β, α1, α2, λ, v0, dt, t, u[α, β, λ]]

I think it will be difficult to avoid some change to the function.

CentralDifferentMethod[α_, β_, α1_, α2_, λ_, v0_, dt_, t_, u0_: False] := Module[{ ...},
If[SameQ[u0, False],
disp[0] = u[α, β, λ];(*initial displacement*)
disp[0] = u0];
...

Note it is usual to place default values at the end of the input parameters, otherwise it can be confusing to tell which values are which when they are omitted, e.g.

j[v_: 0, w_, x_: 1, y_, z_: 2] := jp[v, w, x, y, z]
j[a, b]

jp[0, a, 1, b, 2]

j[a, b, c]

jp[a, b, 1, c, 2]

• You might use u0_: True and them If[TrueQ[u0], . . .]. – Mr.Wizard Sep 21 '14 at 10:48