I need to write a function MySolve
조회 수: 4 (최근 30일)
이전 댓글 표시
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
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
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
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
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
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
댓글 수: 0
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
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
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 Center 및 File Exchange에서 Systems of Nonlinear Equations에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!