Newton's method for 2x2 case:
$$J(x_n)s_n= f(x_n)$$ then update $$x_{n+1} = x_n - s_n$$ where $J$ is the Jacobian matrix formed from $f$. And $x_n$ is a 2-D vector.
Write a MATLAB function to implement Newton's method for a system of 2 nonlinear equations.
It should have the calling sequence [roots, count, resids, history] = newton(func, x0, tolerance)
The first input argument is a function handle to a function that returns the value of the function $f$ and the Jacobian matrix $J$ at the position $x$
e.g. function [f, J] = arbitraryfunc(x)
...
end
The function arbitraryfunc needs to be written by the user, including the expression for f and J. (i.e. the program should allow the user to input any functions). There is no need for J to be found symbolically from f.
My question are: 1. I am not sure how to fill in the blank ... ? 2. How to return the Jacobian of the function (inputted by the user)?
A detailed explanation is very much appreciated. Thanks.
So you want to write a subroutine that returns the jacobian matrix at point $x\in \mathbb{R}^2$ of a function handle
@fin matlab. Also you wanna do this numerically, not symbolically.Here is a simple finite difference subroutine that does the job for
fbeing a function handle:The input
fis a function handle,xis any point. The outputJis the jacobian matrix atx. Example: $f = (x^2+y^2, x^2-y^2)$:For the
Symbolic Toolboxapproach, please refer to my answer here in this question: Solving a system with Newton's method in matlab? . A more object-oriented approach would be like you mentioned, allowing user to input any type of function: inline function, symbolic, function handle, etc. Converting symbolic function to function handle can be done bymatlabFunctionin MATLAB. And inline functions arestring, hence easy to be converted to a symbolic function.