1

Which code should I use it for? FindRoot? or Plot? I am actually new to this program... and I would appreciate if you could tell how to mention the precision. Thanks!

Solve the nonlinear equation $\sin(x+0.5)=0.2x-2$ by Newton's method (precision e=0.00001)

AlexR
  • 24,905
Samuel
  • 21
  • Have a look at this (and look in the Options section). – Peter Phipps Jan 24 '14 at 13:11
  • 1
    This question appears to be off-topic because it is about using Mathematica, rather than any mathematical issue. The forum http://mathematica.stackexchange.com/ is of course available for such questions. – hardmath Jan 24 '14 at 14:01

2 Answers2

1

Here is a code I wrote in Mathematica to perform Newton's Method. Merely define your function that you are using before actually running this code (you must call it f[x_]:= function of $x$ you want).

newtonmethod[error_, initial_, maxiteration_, errorpower_] := 
Module[{},
g[x_] := D[f[x], x];
h[t_] := t - f[t]/g[t];
guess = initial;
tol = error;
errorset = {};
ratios = {};
Do[p = h[t] /. t -> guess;
tol = Abs[p - guess];
AppendTo[errorset, tol];
Print["n = ", n, ", x= ", N[ p], ", error =", N[ tol]];
guess = p; 
If[tol <= error \[Or] Chop[g[t] /. t -> guess] == 0, 
Goto["errorcalculation"]], {n, 1, maxiteration}];
Label["errorcalculation"];
Do[AppendTo[ratios, errorset[[i + 1]]/errorset[[i]]^errorpower], {i,
 1, Length[errorset] - 1}];
Print["Here are the error ratios \n"];
TableForm[N[ratios]]
]

Here, the inputs are error, the maximum error you want, initial, the starting point in $x$, max iteration, how many times at most you want this to run, and error power, for you that will be $1$ its calculating the ratios of errors to a certain power to check for something much deeper than you need (it helps check the rate of convergence really). This should work just fine. At the very least, it gives you a starting point for the code.

1
 FindRoot[2 - 0.2*x + Sin[0.5 + x], {x, 4.4},  Method -> {"Newton", "StepControl" -> None}, WorkingPrecision -> 10]
Amzoti
  • 56,093