Help with " Attempted to access X(2,1); index out of bounds because numel(X)=1."
6 views (last 30 days)
Show older comments
Hi, so I just copy the code here... Script:
clc
clear
x1 = sym('x1','real');
x2 = sym('x2','real');
T = [x1 - x2-2;x1^2+x2-2];
J= jmat(T)
x1=1;
x2=1;
%x3=1;
%x4=1;
X = [x1; x2];
[m,n]=size(X)
eps=1e-6;
Y = [];
disp('iteracija x1 x2 F(x1) F(x2) Norma')
fprintf('%5i %11.6f %11.6f\n',0 , X(1,1), X(2,1))
i=1;
while norm(F(T,X(i)))>=eps
X(i+1)=X(i)-DF(J,X(i)^(-1))*F(T,X(i));
norma(i)=norm(F(T,X(:,end)));
fprintf('%5i %11.6f %11.6f %11.6f %11.6f %11.6f \n',i, X(1,end), X(2,end), Y(1,end),Y(2,end), norma(i))
end
function files: jmat.m
function [ R ] = jmat( T )
x1 = sym('x1','real');
x2 = sym('x2','real');
v = [x1, x2];
R= jacobian(T,v);
end
DF.m
function [ R ] = DF( J, X )
x1 = sym('x1','real');
x2 = sym('x2','real');
%x3 = sym('x3','real');
%x4 = sym('x4','real');
R=J;
x1=X(1,1);
x2=X(2,1);
R=eval(R)
end
F.m
function [ Y ] = F( T, X)
x1 = sym('x1','real');
x2 = sym('x1','real');
%x3 = sym('x3','real');
%x4 = sym('x4','real');
Y=T;
x1=X(1,1);
x2=X(2,1);
Y=eval(Y)
end
And the error is:
??? Attempted to access X(2,1); index out of bounds because numel(X)=1.
Error in ==> F at 11 x2=X(2,1);
Error in ==> lygciusistemos at 20 while norm(F(T,X(i)))>=eps
And how the hell X became (1,1) in function, when it was (2,1) in script?
I would really appreciate your help :)
And if there's something you can't understand, tell me :)
0 Comments
Accepted Answer
Star Strider
on 7 Oct 2012
Edited: Star Strider
on 7 Oct 2012
‘And how the hell X became (1,1) in function, when it was (2,1) in script?’
I believe that problem is simply a mistyped statement.
In F.m, you defined x1 and x2 to be the same:
F.m
function [ Y ] = F( T, X)
x1 = sym('x1','real');
x2 = sym('x1','real');
Also, X is a column-major matrix, so you should refer to its column-vector elements as:
X(:,i)
and your updates to it should be:
X(:,i+1)
(NOTE that it is best that you not use i or j as variables because MATLAB uses them for its imaginary operators. This can cause confusion if you have complex numbers.)
0 Comments
More Answers (1)
Matt J
on 7 Oct 2012
Edited: Matt J
on 7 Oct 2012
Because in this expression, you passed X(i), a scalar, to F()
while norm(F(T,X(i)))
2 Comments
Matt J
on 7 Oct 2012
How would I know that better than you? Is F() supposed to be able to process a scalar input X(i) or not? If so, what is the line
x2=X(2,1);
doing there in F.m ?
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!