1
$\begingroup$

Im trying to write a very simple code that will solve an equation.

total = 800;
side1 = x;
side2 = total - 2 x;
f[x_] := side1*side2
f[x]
f'[x]
Solve[f'[x]==0]

But what seems to be happening is that f[x] is the equation i want it to be, but it thinks the derivative is zero, which is not the case and im not sure how im messing this up. Thank you for any advice.

$\endgroup$
3
$\begingroup$
total = 800;
side1 = x;
side2 = total - 2 x;
f[x_] = side1*side2;
Solve[f'[x] == 0, x]
(* {{x -> 200}} *)

or

total = 800;
side1[x_] = x;
side2[x_] = total - 2 x;
f[x_] := side1[x]*side2[x];
Solve[f'[x] == 0, x]
(* {{x -> 200}} *)
$\endgroup$
1
$\begingroup$

This issue dives into the difference between Set (=) and SetDelayed (:=), and a somewhat unclear issue with taking the derivatives of Unevaluated symbols which do not explicitly depend on the variable of differentiation.

Using SetDelayed results in apparently identical behavior to differentiating the unevaluated symbols directly:

g[x_] := side1 * side2;
{g'[x], D[Unevaluated[side1 * side2], x]}

{0, 0}

Differentiating with D provides the correct result, however, because it evaluates g before taking the derivative:

D[g[x], x]

800 - 4 x

Using Set performs the evaluation beforehand, and thus works properly with the ' notation:

f[x_] = side1 * side2;
f'[x]

800 - 4 x

$\endgroup$
0
$\begingroup$

You should have define the Sides functions. But for F function it's ok to be defined f[x] or without defined since they already depend on sides functions already defined.

With defining f

total = 800;
side1[x_] := x;
side2[x_] := total - 2 x;
f[x_] := side1[x]*side2[x];
f'[x]

Solve[f'[x] == 0, x]
(*{{x -> 200}}*)

FindRoot[f'[x] == 0, {x, 0}]
(*{x -> 200.}*)

OR without defining f

total = 800;
side1[x_] := x;
side2[x_] := total - 2 x;
f = side1[x]*side2[x];
D[f, x]

Solve[D[f, x] == 0, x]
(*{{x -> 200}}*)

FindRoot[D[f, x], {x, 0}]
(*{x -> 200.}*)
$\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.