Help me with Matlab code on Newton-Raphson method to solve a system of nonlinear equations.

8 次查看(过去 30 天)
I want to approximate the zeros of the following nonlinear system, using N-R method:
f(x,y)=x+y^9/3+x^{243}/9+y^{2187}/27=0;
g(x,y)=y+x^{27}/3+y^{243}/9+x^{6561}/27=0.
I have tried with the following Matlab code. But I am getting error. Can you please help me out ?
close all; clc,clear
% Newton Raphson solution of two nonlinear algebraic equations
xy = [-1 -1]; % initial guesses
iter=0;
maxiter=100;
xy_N = 0.5;
TOL = 0.05;
error1 = [1, 1];
f = zeros(iter, 2);
error = zeros(iter, 2);
% begin iteration
while xy_N>TOL
iter=iter+1;
x = xy(1);
y = xy(2);
% calculate the functions
f(iter,:) = [x+y^9/3+x^{243}/9+y^{2187}/27; y+x^{27}/3+y^{243}/9+x^{6561}/27];
% calculate the Jacobian
J= [1+27*x^{242}, 3*y^8+81*y^{2186}; 9*x^{26}+24*x^{6560}, 1+27*y^{242}];
xy_new = xy - ((J)\f(iter,:)')';
error1=abs(xy_new-xy);
error(iter,:)=error1;
% Calculate norm:
xy_N = norm(f(iter,:));
XYN(iter) = xy_N;
xy=xy_new;
%iter=iter+1;
if (iter > maxiter)
fprintf('Did not converge! \n', maxiter);
break
end
end
if iter<maxiter
f(iter+1:end,:)=[];
end
% Print results
fprintf('Number of iterations is: %f \n ', iter)
fprintf('Final values of [x, y] = %f %f \n', xy(end, :));
fprintf('Final values of EQN: %f %f \n', f(end, :));
fprintf('Final values of ERROR: %f %f \n', error(end, :));

回答(1 个)

Ahmad
Ahmad 2022-4-30
why you use { } ?
this is for cell type
  8 个评论

请先登录,再进行评论。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by