I keep getting an error: Attempted to access x(2); index out of bounds because numel(x)=1. in my newtmult code. Please help!

4 views (last 30 days)
This is my code:
function [J,f]=jfreact(x,y,varargin)
df1dx1=2*x;
df1dx2= -1;
df2dx1= -2*x;
df2dx2= -2*y;
J=[df1dx1 df1dx2;df2dx1 df2dx2];
f1=u(x(1),x(2));
f2=v(x(1),x(2));
f=[f1;f2];
function f=u(x,y)
f = x^2-y-1;
function f=v(x,y)
f = 5-y^2-x^2;
i am trying to use newtmult function to solve the system of linear equations with an initial guess of x=y=1.5. any help would be greatly appreciated.
  1 Comment
Walter Roberson
Walter Roberson on 21 Oct 2012
Please show your code that invokes newtmult. Please also clearly show which line the error is occurring on.
newtmult() is not a Mathworks provided function. Are you using a routine out of Numerical Methods In MATLAB, such as is found at http://www.nd.edu/~msen/Teaching/NumMeth/Programs/BookMfiles/newtmult.m ?

Sign in to comment.

Answers (1)

Matt J
Matt J on 21 Oct 2012
Edited: Matt J on 22 Oct 2012
You cannot write jfreact as function of separate arguments x and y.
[J,f]=jfreact(x,y,varargin)
newtmult expects you to bundle all of the unknowns into a single vector and pass that vector as the first input argument:
function [J,f]=jfreact(xy,varargin)
x=xy(1);
y=xy(2);
df1dx1=2*x;
df1dx2= -1;
df2dx1= -2*x;
df2dx2= -2*y;
J=[df1dx1 df1dx2;df2dx1 df2dx2];
f1=u(x(1),x(2));
f2=v(x(1),x(2));
f=[f1;f2];

Community Treasure Hunt

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

Start Hunting!