I need to write a function MySolve
2 ビュー (過去 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!