4
$\begingroup$

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?

$\endgroup$
9
  • $\begingroup$ What do you mean by "without rewriting the function"? What is going to happen with this $u0?$ $\endgroup$
    – Igor Rivin
    Sep 18, 2014 at 23:31
  • $\begingroup$ @IgorRivin Because I use the default value for u() and now I want it can be changed. $\endgroup$
    – Viet Tran
    Sep 18, 2014 at 23:42
  • $\begingroup$ 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. $\endgroup$
    – Mr.Wizard
    Sep 19, 2014 at 15:26
  • $\begingroup$ @Mr.Wizard I have posted the code, I hope you have a look. $\endgroup$
    – Viet Tran
    Sep 20, 2014 at 3:06
  • 2
    $\begingroup$ 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[α, β, λ]]. $\endgroup$
    – m_goldberg
    Sep 20, 2014 at 4:01

3 Answers 3

2
$\begingroup$

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.

$\endgroup$
2
$\begingroup$

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[α, β, λ]]
$\endgroup$
1
$\begingroup$

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]

$\endgroup$
1
  • $\begingroup$ You might use u0_: True and them If[TrueQ[u0], . . .]. $\endgroup$
    – Mr.Wizard
    Sep 21, 2014 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.