-
-
Notifications
You must be signed in to change notification settings - Fork 359
Forward euler implementation in Common Lisp #607
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
7b5c628
131ae54
9f0a2be
d1fcdca
0455d34
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
;;;; Forward euler implementation in Common Lisp | ||
|
||
(defun solve-euler (timestep n) | ||
"Returns a function where y'(t) = -3t and y(0) = 0 using the forward euler method" | ||
(loop | ||
with result = (make-array n :initial-element 1) | ||
for i from 1 upto (1- n) do | ||
(setf (svref result i) (- (svref result (1- i)) (* 3 (svref result (1- i)) timestep))) | ||
finally (return result))) | ||
|
||
(defun approximatep (result threshold timestep) | ||
"Checks the result from the solve-euler function" | ||
(loop | ||
with approximatep = t | ||
with solution = 0 | ||
for i from 0 upto (1- (length result)) do | ||
(setf solution (exp (* (- 3) i timestep))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar indentation question here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey @berquist, what is wrong with the indentation exactly? How many spaces should There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I should learn to read. I disagree that it shouldn't be indented, because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like you're getting indentation within the In addition a very loose style guide (https://lisp-lang.org/style-guide/) plus reading somewhere else that it doesn't really matter that much, these are nitpicks that can be ignored. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just pressed tab on the code and found that it unindented the code. I apparently made the decision half a year ago that this would be easier to read. I don't really care that much now and it should still be fine, so I'll put the code back. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, leave it as is. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well now I've changed it. This is stupid, just merge it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
(when (> (- (svref result i) solution) threshold) | ||
(setf approximatep nil) | ||
(format t "~d ~d~%" (svref result i) solution)) | ||
finally (return approximatep))) | ||
|
||
(defvar timestep 0.01) | ||
(defvar n 100) ;number of steps | ||
(defvar threshold 0.01) | ||
|
||
(defvar result (solve-euler timestep n)) | ||
(defvar approximatep (approximatep result threshold timestep)) | ||
(format t "~:[Value(s) not in threshold~;All values within threshold~]~%" approximatep) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this should be indented.