I need to write a function MySolve

4 次查看(过去 30 天)
sarah
sarah 2011-3-11
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
  1 个评论
Andrew Newell
Andrew Newell 2011-3-11
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.

请先登录,再进行评论。

回答(3 个)

Matt Tearle
Matt Tearle 2011-3-11
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.)
  3 个评论
Andrew Newell
Andrew Newell 2011-3-11
@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
Matt Tearle 2011-3-11
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.

请先登录,再进行评论。


Sean de Wolski
Sean de Wolski 2011-3-11
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

Joe Howes
Joe Howes 2012-5-10
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
  2 个评论
Walter Roberson
Walter Roberson 2012-5-10
Do you want discussion here or in your Question on this topic, http://www.mathworks.com/matlabcentral/answers/38031-mysolve-help
Geoff
Geoff 2012-5-10
Haha, yeah just gave a bunch of suggestions on that. Hopefully not too much =)
Just direct the whole class to this site. Your tutor would be so impressed.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Systems of Nonlinear Equations 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by