MATLAB: Non-Linear Dynamical Systems – Iteration Help? (Beginer)

dyanmical systemsiterationjacobianMATLABnonlinear

Hi,
I am a total novice MATLAB user and I feel as if I am banging my head against a desk with this problem. I am aware that it is VERY simple, but as I mentioned I am very new to MATLAB, any help at all would be much appreciated.
I am studying a non-linear dynamical systems course and I'm required to write MATLAB files that will solve a non-linear system of equations of the form: f(x)=0
I am also told that the function should implement the newton iteration:
x(k+1) = x(k) - ( [df/dx(x(k))]^-1 ( f( x(k) ) )
The expression [df/dx(x(k))] is the Jacobian of f in the previous point x(k) and the initial guess x0 is provided by the user of mysolve (MySolve.m is the name we are told to call our file) I have written a program that finds the Jacobian of any matrix:
function df=MyJacobian(f,x,h)
n = length(x);
xplus = x;
xminus = x;
for i=1:n
xplus(i)=xplus(i)+h;
xminus(i)=xminus(i)-h;
df(:,i)=(f(xplus)-f(xminus))/(2.*h);
xplus(i)=x(i);
xminus(i)=x(i);
end
I know this works.
Finally we are told that the iteration should stop if both the correction (e(k+1)=x(k+1)-x(k)) and the residual (r=f(x(k))) are less than some specified tolerance 'tol' (for example 1e-10). After maxit iterations without convergence one should exit anyway, and set converged=0 (for example, maxit could be 10).
I am sorry for such a long question but once again would appreciate any help what so ever,

Best Answer

I don't want to just do your homework for you, so here are a few hints that will help with the Matlab syntax. First, make a loop for k=1:maxit, so if you exceed the maximum number of iterations you just exit the loop. Inside the loop, update x and use an IF condition to test for convergence:
converged = correction < tol && residual < tol;
if converged
xsolution = current_value_of_x;
return
end
If converged is true, it is set to 1 and you finish.
Related Question