File tree Expand file tree Collapse file tree 2 files changed +22
-3
lines changed
lab-instructions/modules/latex/example Expand file tree Collapse file tree 2 files changed +22
-3
lines changed Original file line number Diff line number Diff line change
1
+ //> using scala 3.4.2
2
+
3
+ @ main def run (x0 : Double , eps : Double ) = println(NewtonRaphson .solve(x0, eps))
4
+
5
+ object NewtonRaphson :
6
+ def f (x : Double ) = math.exp(- x) - math.sin(x)
7
+
8
+ def fprim (x : Double ) = - math.exp(- x) - math.cos(x)
9
+
10
+ def solve (start : Double , eps : Double ) =
11
+ var x0 = start
12
+ var x1 = start
13
+ while {
14
+ x0 = x1
15
+ x1 = x0 - f(x0) / fprim(x0)
16
+ math.abs(x1 - x0) > math.abs(x1) * eps
17
+ } do ()
18
+ x1
Original file line number Diff line number Diff line change 18
18
\usepackage {graphicx }
19
19
20
20
21
-
22
21
\title {Newton-Raphsons metod}
23
22
\author {Per Holm}
24
23
\date {21 maj 2001} % OBS! Vet inte när Per skrev dokumentet, jag hittade på ett datum.
@@ -121,7 +120,8 @@ \subsection{Konvergens}
121
120
122
121
123
122
124
- \section {Newton-Raphsons metod i Java }
123
+ % \section{Newton-Raphsons metod i Java}
124
+ \section {Newton-Raphsons metod i Scala }
125
125
126
126
Det är inte helt enkelt att implementera Newton-Raphsons metod i ett
127
127
program, åtminstone inte om man kräver att metoden ska fungera i
@@ -130,6 +130,7 @@ \section{Newton-Raphsons metod i Java}
130
130
undantagsfall som finns eller fel som kan inträffa, till exempel
131
131
att derivatan \texttt {fprim(x0) } blir nära noll (se avsnitt~\ref {sec:konv }).
132
132
133
- \VerbatimInput {NewtonRaphson.java}
133
+ % \VerbatimInput{NewtonRaphson.java}
134
+ \VerbatimInput {NewtonRaphson.scala}
134
135
135
136
\end {document } % Slut på dokumentet
You can’t perform that action at this time.
0 commit comments