Replacing inline function in Newton Raphson in two dimensions with anonymous function

1 view (last 30 days)
Hello i'm trying to write a general code which solves systems of two non-linear equations simultaneously in two dimensions. For this I need to use the Newton-Raphson method with the Jacobian matrix etc. I have successfully achieved this in one dimension without the Jacobian. Basically my goal would be to write a program which asks the user to input each equation manually and an initial guess for each variable and then automatically calculates the jacobian and hence the answer. My code is shown below but for some reason it only seems to ever solve one iteration no matter how many I specify. I believe that anonymous functions would be a better approach than inline functions but I'm confused as to how I should go about this. Thanks
function [f] = NewtonMulti(x1,x2)
syms x1 x2 %symbolic variables are x1 and x2
f1 = input('input f1: '); %input function f1 in terms of x1 and x2
f2 = input('input f2: '); %input function f2 in terms of x1 and x2
F = [f1;f2]; %creates F as a vector of both functions
G = inline(F); % G is our inline object of vector F
x10 = input('enter initial value of x1: '); %input initial guess for x1
x20 = input('enter initial value of x2: '); %input initial guess for x2
J(1,1) = diff(f1,x1); % element(1,1) of J is the first order partial derivative of f1 in terms of x1
J(1,2) = diff(f1,x2); % element(1,2) of J is the first order partial derivative of f1 in terms of x2
J(2,1) = diff(f2,x1); % element(2,1) of J is the first order partial derivative of f2 in terms of x1
J(2,2) = diff(f2,x2); % element(2,2) of J is the first order partial derivative of f2 in terms of x2
JJ = inline(J); %Let JJ be the inline object of J, this is our jacobian matrix for use in the newton algorithm
x1 = x10; %initial value of x1 will be x10
x2 = x20; %initial value of x2 will be x20
n = input('Input number of iterations: ');%input number of iterations
for u=0:n %from 0 to n iterations
x=[x10;x20]; % let y initially equal x
xnew=x-1*inv(JJ(x1,x2))*G(x1,x2); %formula for newtons method in two dimension
x=x+xnew;
if x == xnew; %if xnew = x old,
break %stop iterating (if xnew = xold)
end
end
answer = xnew;% our solution are the values obtained after the iterations have ceased due to break
disp('The final answer is: '), disp(answer)
disp('The number of iterations it took is: '), disp(u)
%display number of iterations it took to reach convergence

Answers (1)

Rafael Hernandez-Walls
Rafael Hernandez-Walls on 11 Feb 2018
I think you have problems with this lines:
f1 = input('input f1: '); %input function f1 in terms of x1 and x2
f2 = input('input f2: '); %input function f2 in terms of x1 and x2
if you substitute with the following lines
f1 = input('input f1: ','s'); %input function f1 in terms of x1 and x2
f2 = input('input f2: ','s'); %input function f2 in terms of x1 and x2
Rafael

Categories

Find more on Function Creation 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!