Python程序语言讲解、辅导data留学生编程 调试Matlab程序|调试Matlab程序
- 首页 >> Python编程 CR4 (Submission phase)
Submission phase
Instructions for submission
Using your preferred code editor (e.g. VSCode), in a Python script called "CR4.py", write code to answer the following problem.
Problem
The secant method is a root-finding algorithm used to solve equations of the form . The secant method has similarities from both the
regula falsi and Newton's method. Starting from two initial guesses and , with , the next guess is computed recursively as
, where the iteration function is given by
This can be seen as taking a finite difference approximation to the derivative in Newton's method: we approximate the tangent at by tracing
the secant line between the points and (as is done for regula falsi between the two bounds of the bracketing
interval), and the next guess is the intersection of this secant with the x-axis.
An appropriate choice for the initial guesses and is one for which the root . One iteration counts as one computation of
; this is to say that, for instance, if convergence is achieved with , it is achieved in 1 iteration (not 2).
Your task
Write a function "secant(F, x0, x1, tol, kmax)" which takes 5 input arguments:
a function "F", representing the function whose root we are seeking,
two numbers "x0" and "x1", representing the initial guesses and respectively, which bracket the root ( ),
a positive number "tol", representing the tolerance required for convergence,
a strictly positive integer "kmax", representing the maximum number of iterations allowed,
and returns
the root of found by the secant method, as a floating point number, and
the total number of iterations required to find the root.
If "kmax" iterations have been computed and convergence still has not been achieved, return the last guess instead of the root, and
additionally print a message on the screen to display that convergence has failed; the message should also display the value of
.
You should also consider the case where and handle it appropriately.
Additionally, before computing the result, the function should perform all necessary checks on the type and the value of the input arguments, as
was done in the Week 9 workshop. This means that if any input arguments are given incorrectly so that the algorithm will fail to run (either
producing a runtime error or an incorrect result), the function should display an appropriate error message to help the user correct their function
call, and exit the function immediately.
Submission phase
Current phase
Assessment phase Grading evaluation phase Closed
𝐹(𝑥) = 0
𝑥0 𝑥1 𝑥0 ≠ 𝑥1
𝑥𝑘+1 = 𝐺(𝑥𝑘) 𝐺(𝑥)
𝐺(𝑥) = 𝑥𝑘 − 𝐹(𝑥𝑘) .
𝑥𝑘−𝑥𝑘−1
𝐹(𝑥𝑘)−𝐹(𝑥𝑘−1)
𝐹(𝑥𝑘)
(𝑥𝑘,𝐹(𝑥𝑘)) (𝑥𝑘−1,𝐹(𝑥𝑘−1))
𝑥0 𝑥1 𝑥∗ ∈ [𝑥0 ,𝑥1 ]
𝑥𝑘+1 = 𝐺(𝑥𝑘) 𝑥2
𝐹 𝑥∗
𝑥0 𝑥1 𝑥∗ ∈ [𝑥0 ,𝑥1 ]
𝐹
𝑥𝑘max+1
|𝑒𝑘 +1 | = | − | max 𝑥𝑘max+1 𝑥𝑘max
𝐹(𝑥𝑘) = 𝐹(𝑥𝑘−1)
Setup phase
2021/3/24 CR4 (Submission phase)
https://moodle2.maths.ed.ac.uk/live2018/mod/workshop/view.php?id=3641 2/3
Testing
The tests should verify that
each of the checks you perform on the input arguments is triggered appropriately when invalid inputs are given, and
valid input arguments produce a valid result every time.
As usual, you should use some trivial test functions/values with known results for sanity checking. Make sure that the value returned is correct, as
well as the number of iterations required; make sure that when convergence is not achieved, the appropriate message is displayed.
You should include your test code in the script CR4.py, after the function definition. If you produce any plots, make sure you label them
appropriately and clearly.
Scope
This is for the more experienced programmers amongst you: remember that this is peer-assessed, and that Computing & Numerics is a course
designed for beginner programmers. Please try to stick to what we've seen in the course material so far, even if you feel confident in doing
something more advanced -- keep it simple.
You should not need to -- but if you absolutely want to use something we haven't seen in the course, then please keep it to a minimum, and make
sure you explain clearly what you are doing (and why) in the code comments, so that a student with little or no previous programming
experience can assess your work with confidence. If you don't follow these guidelines, and submit code which requires much more advanced
knowledge/experience with Python to fully understand than we have seen in the course so far, then your assessors will be able to penalise you
(see marking scheme below).
Submission
Upload your file "CR4.py" here when you are finished. Make sure you upload your file before Monday 29th March, 12pm (noon), otherwise you
won't be able to participate to the peer-assessment phase, and you will get a zero for the whole CR4 task!
Marking scheme
The marking scheme which you will use when assessing next week is given in the table below. Each criterion counts equally towards the grade,
and is assessed on a scale of 0 to 3.
Criteria 0 1 2 3
Was the file CR4.py submitted
correctly, and the code written as
instructed?
If you penalise here, indicate what
the problem was in the text box.
The file was in the
wrong format, which
made it difficult or
impossible to open.
The file was in the correct format,
but either didn't have the correct
name, or the code inside did not
follow the instructions (e.g. the
function was named differently, or
performed a completely different
task, or there was no function, or
there were no tests).
Everything was submitted in
the correct format and the
code followed the
specifications, but the code
was much too
complex/advanced compared
to what we have seen in the
course.
Everything was
submitted in the
correct format and
the code followed
the specifications
(regardless of
whether it was
correct or not).
Is the code correct and without
bugs? Does the function "secant()"
produce the correct results for any
appropriate inputs?
If you can find any bugs or issues,
then you can indicate them in the
text box below to help the author fix
them. If you know that there is an
issue but you aren't sure why, then
indicate which test(s) look incorrect,
and give your best guess as to what
could be wrong.
(Use the test code provided in the
script by the student, as well as the
model answer and test code
provided by the instructors, to make
sure the code is fully functional.)
The code does not
work at all, there are a
lot of problems which
would take a long time
to find and fix.
There are a few bugs, so the code
works only for some input values,
but fails for many others (either
giving an error or returning the
wrong result).
There is a small number of
minor bugs or omissions, so
the code perhaps works for
most input values but fails for
a few special cases; or
perhaps the code doesn't work
but only because of a minor
issue which is trivial to fix.
The code works
correctly and
returns the correct
results for all input
values.
2021/3/24 CR4 (Submission phase)
https://moodle2.maths.ed.ac.uk/live2018/mod/workshop/view.php?id=3641 3/3
Was the function tested with an
appropriate range of different inputs,
to check if it worked? (Whether or
not the tests passed is not relevant
here.)
No tests were included
in the script.
Very few tests were performed, not
nearly enough to fully check that
the function worked.
A number of tests were
performed, which covered a
good number of possible
values, but perhaps some
important cases were missed.
A good number of
tests were
performed, which
covered all (or
almost all) of the
possible values
we could use with
this function.
Are the type and value of the input
arguments to "secant()" checked,
with an appropriate error message
displayed when necessary?
There are no checks on
the input arguments.
There are only a very small
number of checks on input
arguments, perhaps not all of them
appropriate or functional.
Most required checks on the
input arguments are
performed, but a few are
missing or not functioning; or
perhaps all checks are
performed, but with
inappropriate or absent error
messages.
All input
arguments are
checked
appropriately for
correct type and
value, with an
informative error
message for
each.
Is the code easy to read, with
consistent spacing, and appropriate
variable names?
If you penalise here, give some
suggestions in the text box for how
to improve readability.
The code is very
difficult to read,
perhaps because of
bad/inconsistent
spacing (e.g. if the
code is very "squished"
without any white
space), and poor
variable name choices.
The code is fairly difficult to read in
places, the variable names are not
generally well chosen, and the
spacing could be improved.
The code is mostly easy to
read, although could still be
slightly improved.
The code is easy
to read, with
consistent code
style, spacing,
and appropriate
variable names.
Is the code well-commented and
documented? Do the code
comments help me understand how
the code works?
Remember that the most important
thing is that the comments are
useful to read and understand the
code.
There are no code
comments at all and
there is no docstring for
the function.
There are a small number of code
comments, or perhaps a docstring,
but they are not particularly
informative, or not sufficient to help
the reader understand the code
easily.
There are some code
comments and perhaps a
docstring, but they are not
always helpful, or they are too
descriptive and don't always
explain the different steps.
There are helpful
code comments,
and perhaps a
docstring, clearly
and succinctly
explaining the
main steps of the
code.
Your submission
You have not submitted your work yet
Add submission
You are logged in as Yiwei Zong (Log out)
www.learn.ed__81855_1
Data retention summary
Get the mobile app
◀ CR3
Jump to...
Week 2 practice questions: Lists, loops, and functions ▶
Submission phase
Instructions for submission
Using your preferred code editor (e.g. VSCode), in a Python script called "CR4.py", write code to answer the following problem.
Problem
The secant method is a root-finding algorithm used to solve equations of the form . The secant method has similarities from both the
regula falsi and Newton's method. Starting from two initial guesses and , with , the next guess is computed recursively as
, where the iteration function is given by
This can be seen as taking a finite difference approximation to the derivative in Newton's method: we approximate the tangent at by tracing
the secant line between the points and (as is done for regula falsi between the two bounds of the bracketing
interval), and the next guess is the intersection of this secant with the x-axis.
An appropriate choice for the initial guesses and is one for which the root . One iteration counts as one computation of
; this is to say that, for instance, if convergence is achieved with , it is achieved in 1 iteration (not 2).
Your task
Write a function "secant(F, x0, x1, tol, kmax)" which takes 5 input arguments:
a function "F", representing the function whose root we are seeking,
two numbers "x0" and "x1", representing the initial guesses and respectively, which bracket the root ( ),
a positive number "tol", representing the tolerance required for convergence,
a strictly positive integer "kmax", representing the maximum number of iterations allowed,
and returns
the root of found by the secant method, as a floating point number, and
the total number of iterations required to find the root.
If "kmax" iterations have been computed and convergence still has not been achieved, return the last guess instead of the root, and
additionally print a message on the screen to display that convergence has failed; the message should also display the value of
.
You should also consider the case where and handle it appropriately.
Additionally, before computing the result, the function should perform all necessary checks on the type and the value of the input arguments, as
was done in the Week 9 workshop. This means that if any input arguments are given incorrectly so that the algorithm will fail to run (either
producing a runtime error or an incorrect result), the function should display an appropriate error message to help the user correct their function
call, and exit the function immediately.
Submission phase
Current phase
Assessment phase Grading evaluation phase Closed
𝐹(𝑥) = 0
𝑥0 𝑥1 𝑥0 ≠ 𝑥1
𝑥𝑘+1 = 𝐺(𝑥𝑘) 𝐺(𝑥)
𝐺(𝑥) = 𝑥𝑘 − 𝐹(𝑥𝑘) .
𝑥𝑘−𝑥𝑘−1
𝐹(𝑥𝑘)−𝐹(𝑥𝑘−1)
𝐹(𝑥𝑘)
(𝑥𝑘,𝐹(𝑥𝑘)) (𝑥𝑘−1,𝐹(𝑥𝑘−1))
𝑥0 𝑥1 𝑥∗ ∈ [𝑥0 ,𝑥1 ]
𝑥𝑘+1 = 𝐺(𝑥𝑘) 𝑥2
𝐹 𝑥∗
𝑥0 𝑥1 𝑥∗ ∈ [𝑥0 ,𝑥1 ]
𝐹
𝑥𝑘max+1
|𝑒𝑘 +1 | = | − | max 𝑥𝑘max+1 𝑥𝑘max
𝐹(𝑥𝑘) = 𝐹(𝑥𝑘−1)
Setup phase
2021/3/24 CR4 (Submission phase)
https://moodle2.maths.ed.ac.uk/live2018/mod/workshop/view.php?id=3641 2/3
Testing
The tests should verify that
each of the checks you perform on the input arguments is triggered appropriately when invalid inputs are given, and
valid input arguments produce a valid result every time.
As usual, you should use some trivial test functions/values with known results for sanity checking. Make sure that the value returned is correct, as
well as the number of iterations required; make sure that when convergence is not achieved, the appropriate message is displayed.
You should include your test code in the script CR4.py, after the function definition. If you produce any plots, make sure you label them
appropriately and clearly.
Scope
This is for the more experienced programmers amongst you: remember that this is peer-assessed, and that Computing & Numerics is a course
designed for beginner programmers. Please try to stick to what we've seen in the course material so far, even if you feel confident in doing
something more advanced -- keep it simple.
You should not need to -- but if you absolutely want to use something we haven't seen in the course, then please keep it to a minimum, and make
sure you explain clearly what you are doing (and why) in the code comments, so that a student with little or no previous programming
experience can assess your work with confidence. If you don't follow these guidelines, and submit code which requires much more advanced
knowledge/experience with Python to fully understand than we have seen in the course so far, then your assessors will be able to penalise you
(see marking scheme below).
Submission
Upload your file "CR4.py" here when you are finished. Make sure you upload your file before Monday 29th March, 12pm (noon), otherwise you
won't be able to participate to the peer-assessment phase, and you will get a zero for the whole CR4 task!
Marking scheme
The marking scheme which you will use when assessing next week is given in the table below. Each criterion counts equally towards the grade,
and is assessed on a scale of 0 to 3.
Criteria 0 1 2 3
Was the file CR4.py submitted
correctly, and the code written as
instructed?
If you penalise here, indicate what
the problem was in the text box.
The file was in the
wrong format, which
made it difficult or
impossible to open.
The file was in the correct format,
but either didn't have the correct
name, or the code inside did not
follow the instructions (e.g. the
function was named differently, or
performed a completely different
task, or there was no function, or
there were no tests).
Everything was submitted in
the correct format and the
code followed the
specifications, but the code
was much too
complex/advanced compared
to what we have seen in the
course.
Everything was
submitted in the
correct format and
the code followed
the specifications
(regardless of
whether it was
correct or not).
Is the code correct and without
bugs? Does the function "secant()"
produce the correct results for any
appropriate inputs?
If you can find any bugs or issues,
then you can indicate them in the
text box below to help the author fix
them. If you know that there is an
issue but you aren't sure why, then
indicate which test(s) look incorrect,
and give your best guess as to what
could be wrong.
(Use the test code provided in the
script by the student, as well as the
model answer and test code
provided by the instructors, to make
sure the code is fully functional.)
The code does not
work at all, there are a
lot of problems which
would take a long time
to find and fix.
There are a few bugs, so the code
works only for some input values,
but fails for many others (either
giving an error or returning the
wrong result).
There is a small number of
minor bugs or omissions, so
the code perhaps works for
most input values but fails for
a few special cases; or
perhaps the code doesn't work
but only because of a minor
issue which is trivial to fix.
The code works
correctly and
returns the correct
results for all input
values.
2021/3/24 CR4 (Submission phase)
https://moodle2.maths.ed.ac.uk/live2018/mod/workshop/view.php?id=3641 3/3
Was the function tested with an
appropriate range of different inputs,
to check if it worked? (Whether or
not the tests passed is not relevant
here.)
No tests were included
in the script.
Very few tests were performed, not
nearly enough to fully check that
the function worked.
A number of tests were
performed, which covered a
good number of possible
values, but perhaps some
important cases were missed.
A good number of
tests were
performed, which
covered all (or
almost all) of the
possible values
we could use with
this function.
Are the type and value of the input
arguments to "secant()" checked,
with an appropriate error message
displayed when necessary?
There are no checks on
the input arguments.
There are only a very small
number of checks on input
arguments, perhaps not all of them
appropriate or functional.
Most required checks on the
input arguments are
performed, but a few are
missing or not functioning; or
perhaps all checks are
performed, but with
inappropriate or absent error
messages.
All input
arguments are
checked
appropriately for
correct type and
value, with an
informative error
message for
each.
Is the code easy to read, with
consistent spacing, and appropriate
variable names?
If you penalise here, give some
suggestions in the text box for how
to improve readability.
The code is very
difficult to read,
perhaps because of
bad/inconsistent
spacing (e.g. if the
code is very "squished"
without any white
space), and poor
variable name choices.
The code is fairly difficult to read in
places, the variable names are not
generally well chosen, and the
spacing could be improved.
The code is mostly easy to
read, although could still be
slightly improved.
The code is easy
to read, with
consistent code
style, spacing,
and appropriate
variable names.
Is the code well-commented and
documented? Do the code
comments help me understand how
the code works?
Remember that the most important
thing is that the comments are
useful to read and understand the
code.
There are no code
comments at all and
there is no docstring for
the function.
There are a small number of code
comments, or perhaps a docstring,
but they are not particularly
informative, or not sufficient to help
the reader understand the code
easily.
There are some code
comments and perhaps a
docstring, but they are not
always helpful, or they are too
descriptive and don't always
explain the different steps.
There are helpful
code comments,
and perhaps a
docstring, clearly
and succinctly
explaining the
main steps of the
code.
Your submission
You have not submitted your work yet
Add submission
You are logged in as Yiwei Zong (Log out)
www.learn.ed__81855_1
Data retention summary
Get the mobile app
◀ CR3
Jump to...
Week 2 practice questions: Lists, loops, and functions ▶