Message Boards Message Boards

0
|
2943 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Make a function that can find the 3 rod of a number?

Posted 6 years ago

Here you can see my code, as it is for now. Must admit, I'm kinda lost why it doesn't work. I think there is something about the syntax I'm not understanding.

Best regards,

kobikrod2[x0_] := Module[{xny, xgl},
  xs = If[Positive[x0], xs = N[x0], Print["Tallet er ikke positivt"]]
     xgl = xs // 2 ;
  xny++ 1 = 1/3 (2*xgl + (xs/xgl^2));

  While[xny != xgl, xgl = xny; 
   xny = xgl + 1 = 1/3 (2*xgl + (xs/xgl^2))];
  xny]
POSTED BY: Martin Friis
2 Replies

The stopping criterion presented in that answer (Abs[func@#] > precision &) is a dangerous! If the function is "flat" near a zero, you could stop when the magnitude of the current approximation is close to 0 yet the approximation is far from 0. It is much better to base the stopping criterion primarily on how far apart the x-values are from each other (e.g., compare the current and preceding approximations). Of course it doesn't hurt to add also a condition about the y-value of the function; but usually it's better, then, to use some kind of relative error rather than absolute error.

POSTED BY: Murray Eisenberg
Posted 6 years ago

There are several problems with the code. e.g. xs // 2. In Mathematica, // is the postfix form of function application so xs // 2 evaluates to 2[xs]. Take a look at this and this to get more familiar with Mathematica.

From the code it looks like you are trying to implement cube root finding using the Newton-Raphson method. Here is one way to do that.

Need to find the value of x for which the function f is zero.

f[x_, n_] := x^3 - n

Generic Newton-Raphson method

newtonRaphson[func_, guess_, precision_: 0.0001] := 
 NestWhile[# - func[#]/func'[#] &, guess, Abs[func@#] > precision &]

Cube root of 3

newtonRaphson[Curry[f][3], 1.0] (* 1.44225 *)

Cube root of 10

newtonRaphson[Curry[f][10], 1.0] (* 2.15443 *)
POSTED BY: Rohit Namjoshi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract