0
$\begingroup$

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).

$\endgroup$
4
$\begingroup$

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 *)
$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – hbaromega Jan 4 '15 at 16:55
  • $\begingroup$ @hbaromega Solve[] doesn't return "values" but "replacement rules". Please read the docs and this $\endgroup$ – Dr. belisarius Jan 5 '15 at 5:14
  • $\begingroup$ 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? $\endgroup$ – hbaromega Jan 5 '15 at 7:31
  • $\begingroup$ @hbaromega mathematica.stackexchange.com/a/18706/193 $\endgroup$ – Dr. belisarius Jan 5 '15 at 14:28
2
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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