0
sol1[t_?NumericQ] := FindRoot[{a + b - t == 0, a - b - 1 == 0}, 
   {{a, 0.80, 0.95}, {b, 0.95, 2}}];

a0[t_] := sol1[t][[1, 2]]; 
b0[t_] := sol1[t][[2, 2]];
f[t_] := 5 + a0[t] - b0[t]^2;

Print[sol1[1][[1, 2]]];

max0 := NMaximize[f[t], t];
Print[max0];

max1 := FindMaximum[f[t], {t, 2}];
Print[max1];

Plot[f[t], {t, 0, 5}]
MarcoB
  • 67,153
  • 18
  • 91
  • 189

2 Answers2

1

By defining sol1[t_?NumericQ] := you will cause a symbolic argument to be held:

sol1[foo]    (* out:   sol1[foo]   *)

You can therefore not extract parts from it that do not exist. You should therefore make functions that call sol1 also hold symbolic arguments:

ClearAll[a0, b0]

a0[t_?NumericQ] := sol1[t][[1, 2]];
b0[t_?NumericQ] := sol1[t][[2, 2]];

Now symbolic arguments do not evaluate, but numeric ones do:

a0[foo]
a0[3.7]
a0[foo]

2.35

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
1

Consider the following rewrite of your code.

  1. Find a generic solution to your system of linear equations:

    solution = Solve[{a + b - t == 0, a - b - 1 == 0}, {a, b}]
    (* Out: {{a -> (1 + t)/2, b -> 1/2 (-1 + t)}} *)
    
  2. Define a function involving those solutions; no need to define it with SetDelayed (:=) in this case. Simple Set (=) will do fine here.

    f[t_] = 5 + a - b^2 /. First@solution
    (* Out: 5 - 1/4 (-1 + t)^2 + (1 + t)/2 *)
    
  3. Obtain the value of $t$ for which the function $f$ achieves its maximum value. The calculation can be done symbolically:

    maximum = ArgMax[f[t], t]
    (* Out: 2 *)
    
  4. Plot the function f and the position of the maximum to check:

    Plot[f[t], {t, 0, 5}, 
      Epilog -> {PointSize[0.02], Red, Point[{maximum, f[maximum]}]}
    ]
    

Mathematica graphics

MarcoB
  • 67,153
  • 18
  • 91
  • 189
  • Thanks for the help. This example represents a much bigger (non-linear) program, and was only meant to show the problem (getting the error messages). Please focus solely on this issue, and not on rectifying the code which is irrelevant per-se! – Benjamin Bental Feb 23 '16 at 07:00