symbolic newton method for nonlinear equations.
Show older comments
I have written this code to get a function handle matrix of a system of equations and a guess vector yet when I run it it returns obsolete answers.surprisingly,when I try to debug it,the first iteration goes as planned yet it fails afterwards.
I would be happy if you told me what is wrong with my code and how to fix it.
thank you in advance.
ps:my vwersion of matlab is 2013b
function [x,varargout] = newton_solve(f,x0,varargin)
k = 1000;
epsilon = 10^-3;
i = 0;
if ~isempty(varargin)
for i = 1:length(varargin) - 1
switch varargin{i}
case('k')
k = varargin(i + 1);
case('epsilon')
epsilon = varargin(i + 1);
end
end
end
xlast = x0';
sol = x0;
varsmat = sym('x',[1,length(x0)]);
vars = {};
xlastin = {};
for i = 1:length(varsmat)
vars{i} = varsmat(i);
xlastin{i} = x0(i);
end
j = jacobian(f(vars{:}));
while true
nj=doubl(subs(j,vars,mat2cell(xlast')));
in = inv(nj);
i = i + 1;
temp = transpose(f(xlastin{:}));
debug_check = in * temp;
sol = xlast - debug_check;
xlast = sol;
if i>k || norm(sol) < epsilon
break
end
end
x = sol;
1 Comment
nj=double(subs(j,vars,mat2cell(xlast'))) % typo
It seems you have a typo error in while loop.
Answers (0)
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!