0

I have a function F=c+dz, which has particular boundary values, say F(z=1)=2 and F(z=-1)=1, Then I can determine the coefficients: c=1.5 and d=0.5. However, the following does not work!

ClearAll; 
F[c_, d_, z_] = c + d*z;
Print[F[c, d, 1]]; Print[F[c, d, -1]];
Solve[{F[c, d, 1] == 2, F[c, d, -1] == 1}, {c, d}];
Print["c=", c, " d=", d];

Where's the mistake?

PS. There's a question of similar title here, which hasn't received an answer (and looks complicated too).

hbaromega
  • 183
  • 1
  • 7

2 Answers2

4

You need to understand how to define a function and how replacement rules work. This is a nice start point.

f[c_, d_, z_] := c + d z
s = Solve[{f[c, d, 1] == 2, f[c, d, -1] == 1}, {c, d}]

(* {{c -> 3/2, d -> 1/2}} *)

Print["c=", c /. s[[1]], " d=", d /. s[[1]]]

(* c=3/2 d=1/2 *)
Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
  • What if I just use s = Solve[{f[c, d, 1] == 2, f[c, d, -1] == 1}] in the second line (omitting {c,d} part)? Seems that works too. Anyway still c and d don't take their values (What is Print[c,d];?). How does s become an array? – hbaromega Jan 04 '15 at 16:55
  • @hbaromega Solve[] doesn't return "values" but "replacement rules". Please read the docs and this – Dr. belisarius Jan 05 '15 at 05:14
  • Thanks. I have read this. Still it's not clear what's the point of mentioning c and d, if Solve cannot return them! Anyway, granting that, could you explain me how it stores in an array s? Say, what would s[[2]] mean and when can I use that? – hbaromega Jan 05 '15 at 07:31
  • @hbaromega http://mathematica.stackexchange.com/a/18706/193 – Dr. belisarius Jan 05 '15 at 14:28
2

In addition to @belisarius' solution, you might want to look at StringForm

Clear[f]

f[c_, d_, z_] = c + d z; (* either Set or SetDelayed works *)

s = Solve[{f[c, d, 1] == 2, f[c, d, -1] == 1}, {c, d}];

StringForm["c = `1`, d = `2`", c, d] /. s[[1]]

enter image description here

Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198