// Numbas version: exam_results_page_options {"name": "Julien's copy of Interactive Newton-Raphson method", "extensions": ["jsxgraph"], "custom_part_types": [], "resources": [], "navigation": {"allowregen": true, "showfrontpage": false, "preventleave": false}, "question_groups": [{"pickingStrategy": "all-ordered", "questions": [{"variable_groups": [], "preamble": {"js": "", "css": ""}, "rulesets": {"std": ["all", "!collectNumbers", "!noLeadingMinus"]}, "variables": {"tol": {"name": "tol", "definition": "0", "group": "Ungrouped variables", "description": "", "templateType": "anything"}, "a": {"name": "a", "definition": "random(8..15)", "group": "Ungrouped variables", "description": "", "templateType": "anything"}, "ans1": {"name": "ans1", "definition": "precround(results,4)", "group": "Ungrouped variables", "description": "", "templateType": "anything"}, "ans": {"name": "ans", "definition": "precround(tans,4)", "group": "Ungrouped variables", "description": "", "templateType": "anything"}, "b": {"name": "b", "definition": "random((a+1)..9)", "group": "Ungrouped variables", "description": "", "templateType": "anything"}, "results": {"name": "results", "definition": "nr(m,b,a,2,10,[])", "group": "Ungrouped variables", "description": "", "templateType": "anything"}, "m": {"name": "m", "definition": "random(1.5..2#0.1)", "group": "Ungrouped variables", "description": "", "templateType": "anything"}, "tans": {"name": "tans", "definition": "((2*m-1)*exp(2*m)+a)/(m*exp(2*m)+b)", "group": "Ungrouped variables", "description": "", "templateType": "anything"}, "maxy": {"name": "maxy", "definition": "ceil(e^(2*m)+2*b-a)+5", "group": "Ungrouped variables", "description": "", "templateType": "anything"}}, "variablesTest": {"condition": "", "maxRuns": 100}, "statement": "

Consider the following equation.

\n

\$\\simplify[std]{e^({m}x)+{b}x-{a}=0}\$

\n

Find the approximate solution in the range $0 \\le x \\le 1$ by using the Newton-Raphson method.

\n

The following diagram demonstrates the method.

\n

$x_0$ is the starting value, you can slide it along the x-axis to see the effect of changing it.

\n

\n

{test(m,b,a,maxy)}

\n

\n

\n

", "extensions": ["jsxgraph"], "advice": "

a)

\n

Recall that the Newton-Raphson method is defined by:
\$x_{n+1}=x_n-\\frac{g(x_n)}{g'(x_n)}\$
where we would like to find the root of the equation $g(x)=0$

\n

In this question we have:
\$\\simplify[std]{g(x) = Exp({m} * x) + {b} * x + { -a}} \\Rightarrow \\simplify[std]{g'(x) = {m}*Exp({m} * x) + {b}}\$
Substituting these expressions into the formula we have:
\$x_{n+1} =\\simplify[std]{ x_n -((Exp({m} * x_n) + {b} * x_n + { -a}) / ({m} * Exp({m} * x_n) + {b}))}\$

\n

which can be rearranged to give:
\$x_{n + 1} = \\simplify[std]{(({m} * x_n -1) * Exp({m} * x_n) + {a}) / ({m} * Exp({m} * x_n) + {b})}\$

\n

(In your answers you would input $x$ rather than $x_n$.)

\n

In the following let $\\displaystyle f(x)=\\simplify[std]{ (({m} * x -1) * Exp({m} * x ) + {a}) / ({m} * Exp({m} * x ) + {b})}$

\n

b)

\n

If $x_0=2$ then $x_1$ is simply given by:
\$\\simplify[std]{x_1 = (({2*m} -1) * Exp({2*m}) + {a}) / ({m} * Exp({2*m}) + {b})}\$

\n

which to 4 decimal places is: $\\;\\;x_1= \\var{ans}$

\n

We find on running the iteration that the first six values are:

\n

\\\begin{align}x_1&=f(x_0)=f(2)&=\\var{results}\\\\x_2&=f(x_1)=f(\\var{results})&=\\var{results}\\\\x_3&=f(x_2)=f(\\var{results})&=\\var{results}\\\\x_4&=f(x_3)=f(\\var{results})&=\\var{results}\\\\x_5&=f(x_4)=f(\\var{results})&=\\var{results}\\\\x_6&=f(x_5)=f(\\var{results})&=\\var{results}\\end{align}\

\n

So the solution to the equation to four decimal places for $0 \\le x \\le 1$ is $x=\\var{precround(ans1,4)}$

\n

Here we see the graph of $\\simplify{e^({m}*x)+{b}*x-{a}}$ and the first four successive approximations to the root:

\n

{test(m,b,a,maxy)}

\n

\n

Note that you can slide the first approximation $x_0$ along the x-axis to see the effect of changing the starting value.

", "tags": [], "contributors": [{"name": "Bill Foster", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/6/"}, {"name": "Julien Ugon", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/3575/"}], "ungrouped_variables": ["a", "maxy", "ans1", "m", "results", "b", "tol", "ans", "tans"], "metadata": {"licence": "Creative Commons Attribution 4.0 International", "description": "

Write down the Newton-Raphson formula for finding a numerical solution to the equation $e^{mx}+bx-a=0$. If $x_0=1$ find $x_1$.

\n

Included in the Advice of this question are:

\n

6 iterations of the method.

\n

Graph of the NR process using jsxgraph. Also user interaction allowing change of starting value and its effect on the process.

"}, "name": "Julien's copy of Interactive Newton-Raphson method", "functions": {"fun": {"definition": "((m*c-1)*e^(m*c)+a)/(m*e^(m*c)+b)", "parameters": [["m", "number"], ["b", "number"], ["a", "number"], ["c", "number"]], "language": "jme", "type": "number"}, "nr": {"definition": "if(n=5,l+[fun(m,b,a,c)]+[fun(m,b,a,fun(m,b,a,c))],nr(m,b,a,fun(m,b,a,c),n-1,l+[c]))", "parameters": [["m", "number"], ["b", "number"], ["a", "number"], ["c", "number"], ["n", "number"], ["l", "list"]], "language": "jme", "type": "list"}, "funfornr": {"definition": "var div = Numbas.extensions.jsxgraph.makeBoard('600px','200px',{boundingBox:[0,10,1,-8],grid:false});\n var board = div.board;\n var m = Numbas.jme.unwrapValue(scope.variables.m);\n var be = Numbas.jme.unwrapValue(scope.variables.b);\n var al = Numbas.jme.unwrapValue(scope.variables.a);\n var ans = Numbas.jme.unwrapValue(scope.variables.ans1)\n var a = board.create('point',[ans,0],'$A$');\n \n var graph = board.create('functiongraph',function(x){return Math.exp(m*x)+be*x-al});\n \n return div;\n ", "parameters": [], "language": "javascript", "type": "html"}, "test": {"definition": "var div = Numbas.extensions.jsxgraph.makeBoard('600px','400px', {boundingbox:[0,maxy,3,-30], axis:false});\n var brd=div.board;\n // Initial function term\n var term = function(x) { return Math.exp(m*x)+b*x-a; };\n var graph = function(x) { return term(x); };\n // Recursion depth\n var steps = 4;\n // Start value\n var s = 2;\n \n //for (i = 0; i < steps; i++) {\n //document.write('x' + i + ' = ');\n //}\n \n var i;\n var ax = brd.create('axis', [[0,0], [1,0]], {strokeColor: 'black'});\n var ay = brd.create('axis', [[0,0], [0,1]], {strokeColor: 'black'});\n \n var g = brd.create('functiongraph', [function(x){return graph(x);}],{strokeWidth: 2, dash:0});\n var x = brd.create('glider',[s,0,ax], {name: 'x_{0}', strokeColor: 'magenta', fillColor: 'yellow'});\n \n newton(x, steps, brd);\t\n \n //function xval() {\n //for (i = 0; i < steps; i++)\n //document.getElementById('xv' + i).innerHTML = (brd.select('x_{' + i + '}').X()).toFixed(14);\n //}\n \n //brd.addHook(xval);\n \n function newton(p, i, board) {\t\n board.suspendUpdate();\t\n if(i>0) {\n var f = board.create('glider',[function(){return p.X();}, function(){return graph(p.X())},g], {name: '', style: 3, strokeColor: 'green', fillColor: 'yellow'});\n var l = board.create('line', [p,f],{strokeWidth: 0.5, dash: 1, straightFirst: false, straightLast: false, strokeColor: 'black'});\n var t = board.create('tangent',[f],{strokeWidth: 0.5, strokeColor: '#0080c0', dash: 0});\n var x = board.create('intersection',[ax,t,0],{name: 'x_{'+(steps-i+1) + '}', style: 4, strokeColor: 'magenta', fillColor: 'yellow'});\n newton(x,--i, board);\n }\n board.unsuspendUpdate(); \n \n \n }\t\n return div;", "parameters": [["m", "number"], ["b", "number"], ["a", "number"], ["maxy", "number"]], "language": "javascript", "type": "html"}}, "parts": [{"customMarkingAlgorithm": "", "variableReplacementStrategy": "originalfirst", "variableReplacements": [], "customName": "", "stepsPenalty": 0, "steps": [{"checkingType": "absdiff", "prompt": "

Recall that the Newton-Raphson method is defined by:
\$x_{n+1}=x_n-\\frac{g(x_n)}{g'(x_n)}\$
where we would like to find the root of the equation $g(x)=0$

", "checkingAccuracy": 0.001, "answer": "", "valuegenerators": [], "customName": "", "scripts": {}, "checkVariableNames": false, "vsetRangePoints": 5, "failureRate": 1, "customMarkingAlgorithm": "", "variableReplacementStrategy": "originalfirst", "variableReplacements": [], "showCorrectAnswer": true, "showFeedbackIcon": true, "extendBaseMarkingAlgorithm": true, "unitTests": [], "marks": 0, "type": "jme", "vsetRange": [0, 1], "showPreview": true, "useCustomName": false}], "sortAnswers": false, "gaps": [{"checkingType": "absdiff", "customName": "", "checkingAccuracy": 0.0001, "answer": "(((({m} * x) -1) * Exp(({m} * x))) + {a})", "valuegenerators": [{"name": "x", "value": ""}], "scripts": {}, "checkVariableNames": false, "vsetRangePoints": 5, "failureRate": 1, "customMarkingAlgorithm": "", "variableReplacementStrategy": "originalfirst", "variableReplacements": [], "answerSimplification": "std", "showCorrectAnswer": true, "showFeedbackIcon": true, "extendBaseMarkingAlgorithm": true, "unitTests": [], "marks": 1, "type": "jme", "vsetRange": [1, 1.5], "showPreview": true, "useCustomName": false}, {"checkingType": "absdiff", "customName": "", "checkingAccuracy": 0.001, "answer": "(({m} * Exp(({m} * x))) + {b})", "valuegenerators": [{"name": "x", "value": ""}], "scripts": {}, "checkVariableNames": false, "vsetRangePoints": 5, "failureRate": 1, "customMarkingAlgorithm": "", "variableReplacementStrategy": "originalfirst", "variableReplacements": [], "answerSimplification": "std", "showCorrectAnswer": true, "showFeedbackIcon": true, "extendBaseMarkingAlgorithm": true, "unitTests": [], "marks": 1, "type": "jme", "vsetRange": [0, 1], "showPreview": true, "useCustomName": false}], "showCorrectAnswer": true, "showFeedbackIcon": true, "extendBaseMarkingAlgorithm": true, "prompt": "\n

This equation has a root in the range $0 \\lt x \\lt 1$.

\n

Using the Newton-Raphson formula, if $x_n$ is the $n$th estimate for this root, show that the next estimate can be written in the form \$x_{n+1}= \\frac{p(x_n)}{g'(x_n)}\$
Enter $p(x_n)$ and $g'(x_n)$ in the boxes below.

\n

Please note that if you enter a function of the form $xe^{ax}$, then you must input it as $x*e^{ax}$.

\n

$p(x_n)=\\;\\;$[] In your answer use $x$ instead of $x_n$.

\n

$g'(x_n)=\\;\\;$[] In your answer use $x$ instead of $x_n$.

\n

If you have forgotten the Newton-Raphson formula you can click on Steps to see it. You will not lose any marks in doing so.

\n ", "unitTests": [], "type": "gapfill", "scripts": {}, "useCustomName": false, "marks": 0}, {"customMarkingAlgorithm": "", "variableReplacementStrategy": "originalfirst", "variableReplacements": [], "prompt": "

If $x_0=2\\;\\;\\;$what is $x_1$ correct to $4$ decimal places?

\n

$x_1=\\;\\;$ []

\n

Enter your answer to 4 decimal places.

", "sortAnswers": false, "gaps": [{"customMarkingAlgorithm": "", "variableReplacementStrategy": "originalfirst", "variableReplacements": [], "customName": "", "maxValue": "ans+tol", "showCorrectAnswer": true, "allowFractions": false, "correctAnswerFraction": false, "showFractionHint": true, "minValue": "ans-tol", "showFeedbackIcon": true, "mustBeReduced": false, "extendBaseMarkingAlgorithm": true, "mustBeReducedPC": 0, "unitTests": [], "type": "numberentry", "scripts": {}, "notationStyles": ["plain", "en", "si-en"], "correctAnswerStyle": "plain", "useCustomName": false, "marks": 1}], "showCorrectAnswer": true, "showFeedbackIcon": true, "extendBaseMarkingAlgorithm": true, "customName": "", "unitTests": [], "type": "gapfill", "scripts": {}, "useCustomName": false, "marks": 0}]}]}], "contributors": [{"name": "Bill Foster", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/6/"}, {"name": "Julien Ugon", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/3575/"}]}