Azzera filtri
Azzera filtri

I need to write a function MySolve

4 visualizzazioni (ultimi 30 giorni)
sarah
sarah il 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
  1 Commento
Andrew Newell
Andrew Newell il 11 Mar 2011
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.

Accedi per commentare.

Risposte (3)

Matt Tearle
Matt Tearle il 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.)
  3 Commenti
Andrew Newell
Andrew Newell il 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
Matt Tearle il 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.

Accedi per commentare.


Sean de Wolski
Sean de Wolski il 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

Joe Howes
Joe Howes il 10 Mag 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
  2 Commenti
Walter Roberson
Walter Roberson il 10 Mag 2012
Do you want discussion here or in your Question on this topic, http://www.mathworks.com/matlabcentral/answers/38031-mysolve-help
Geoff
Geoff il 10 Mag 2012
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.

Accedi per commentare.

Categorie

Scopri di più su Systems of Nonlinear Equations in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by