0
$\begingroup$

Whenever I use functions like FindRoot or NDSolve, it sends x through the function and deals with the result. That would be fine if I was sending a simple math function through, but I have something more like a short program. Is there any way to make it solve it by putting specific values through the function and looking at the results?

$\endgroup$
9
  • 3
    $\begingroup$ You can give a program as a function to FindRoot. Just be sure to restric the arguments to numerical values, e.g. programF[x_?NumberQ] := ...; FindRoot[programF[x]==0,{x,1}]. $\endgroup$ Nov 8, 2012 at 22:41
  • $\begingroup$ What do I do about passing arrays? $\endgroup$
    – DanielLC
    Nov 9, 2012 at 0:56
  • $\begingroup$ That's a bit vague. Could you maybe talk about your actual problem, so we can be more helpful? $\endgroup$ Nov 9, 2012 at 2:17
  • $\begingroup$ To pass array try programF[x_?(ArrayQ[#, _, NumericQ] &)] := ...; $\endgroup$ Nov 9, 2012 at 2:18
  • 1
    $\begingroup$ @Plato, like x_ /; ArrayQ[x, _, MatchQ[#, 0 | 1] &] then? $\endgroup$ Nov 9, 2012 at 8:33

1 Answer 1

2
$\begingroup$

I will give you a simple but realistic example. Imagine you are given two $d$-dimensional vectors $X$ and $B$. Now you are asked to find a matrix say $A$ such that $AX=B.$ How can we use Mathematica to solve this problem?

Prepare the two vectors X and B

d = 6;
SeedRandom[123];
X = RandomInteger[{1, 100}, d];
B = RandomReal[{200, 400}, d];

Now is time to define a function that takes a numerical $d\times d$-dimensional array $A$ as input and computes the norm $|A X-B|.$

Obj[A_?(ArrayQ[#, _, NumericQ] &)] := (A. X - B) // Norm;

At this point one would try to minimize the above function in order to get the matrix $A$ such that $AX=B$ holds approximately. We use the FindMinimum function with a derivative free method option.

FindMinimum[Obj[A], {A, RandomReal[{1, 25}, {d, d}]},
Method -> "PrincipalAxis", AccuracyGoal -> 12, PrecisionGoal -> 60,
MaxIterations -> 1000] // Short

{1.39237*10^-13,{A->{{-66.9631,20.7114,14.3336,11.7857,9.99711,14.8347},{-<<19>>,<<4>>,<<19>>},<<3>>,{<<1>>}}}}

Similar things can be done for functions like FindRoot or NDSolve. Hope this helps.

BR

$\endgroup$
2
  • $\begingroup$ This works, but when I tried to modify it to use a real number (replacing (ArrayQ[#, _, NumericQ] &) with NumberQ) or even a one dimensional array (I just changed the program so it uses one dimensional arrays and set the initial point to one) it started sending variables through again. Also, taking out the ?(ArrayQ[#, _, NumericQ] &) part also resulted in that. Am I modifying it wrong? I need to run these sorts of functions with a real number and a one dimensional array. $\endgroup$
    – DanielLC
    Nov 12, 2012 at 23:44
  • $\begingroup$ Never mind. It seems to work fine now. $\endgroup$
    – DanielLC
    Nov 14, 2012 at 23:17

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.