MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply TodayTo resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Asked by sarah on 11 Mar 2011

I need to write a function MySolve that will solve the nonlinear system of equations in the form

0=f(x)

where I have f:R^n -> R^n is an arbitrary function with n-dimensions input and n-dimensional output. The function should implement the newton iteration.

I know the first line of my function looks like:

function [x,converged]=MySolve(f,xo,tol,maxit)

But I am having difficulty with the rest

Answer by Matt Tearle on 11 Mar 2011

function [x,converged]=MySolve(f,xo,tol,maxit) opts = optimset('MaxIter',maxit,'TolFun',tol); [x,~,converged] = fsolve(f,xo,opts);

(Unless, of course, this is a homework problem. In which case you might not be allowed to do that.)

Andrew Newell on 11 Mar 2011

@Matt, I'm afraid I'll have to dock you some marks for using the the trust-region dogleg algorithm instead of a Newton method.

Matt Tearle on 11 Mar 2011

It's a fair cop. I was too lazy to look up which method fsolve used by default. From memory, I thought it was Levenburg-Marquardt. Which would count as a Newton method. Maybe it's fzero that uses Lev-Marq.

Answer by Sean de Wolski on 11 Mar 2011

This should help a little

while the_error > tol && iter <= maxit iter = iter+1; Do a whole bunch of other stuff that you have to write end

Answer by Joe Howes on 10 May 2012

i am also having the same problem as this so far i have

function [x,converged]=MySolve(f,xold,tol,maxit)

%maxit maximum number of iterations to be tried.

x=xold;

h=1e-10;

% run a loop from 1 to maxit

for k=0:maxit

%Need to call in MyJacobian

J=Myjacobian(f,x,h);

% Newton iteration

x= xold-(J\f(xold));

if(max(abs(x-xold)))<tol && (max(abs(x-xold)))<tol converged=1;

% Newtons iteration has converged

else converged=0;

%Newtons iteration hasn't converged

end

xold=x;

end

end

with my jacobian function looking like this

function df=Myjacobian(f,x,h)

% f: function to be differentiated %x: point where jacobian is taken % h: parameter for finite differences % outputs df: m*n matrix, jacobian of f in x.

n=length(x);

% defines number of rows

fx=f(x); m=length(fx);

% defines number of colums

df=zeros(n,m);

% matrix of zeros

for i=1:n;

% runs a loop that takes two values x1 and x2 and places it % into my empty matrix df the process then produces 2 matircies of % df1 and df2

x(i)=x(i)+h; x1= f(x); x(i)=x(i)-(2*h); x2= f(x); df(:,i)=(x1-x2)/(2*h); x(i)=x(i)+h;

end

end

Walter Roberson on 10 May 2012

Do you want discussion here or in your Question on this topic, http://www.mathworks.com/matlabcentral/answers/38031-mysolve-help

## 1 Comment

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/3007#comment_6276

No one is going to do your homework for you. Try solving this problem yourself first, then show us your code and tell us what problems you're having.