I am getting subscripted assignment dimension mismatch: on the line err(:,i)=abs(X-Xold);

1 view (last 30 days)
% to solve the system of non-linear eqaution using NR
%%initial conditions:
X0 = [100.0;30.0;40.0;2000.0;2000.0;500.0];
Max_Iter = 50;
tolX = 1e-6;
%%using for loop iteration:
X = X0;
Xold = X0;
err = zeros(1,50);
for i = 1:Max_Iter
[f,j] = MEE_NR(X);
X = X - inv(j)*f;
err(:,i)= abs(X-Xold);
Xold = X;
if (err(:,i)<tolX)
break;
end
end
[EDITED, Jan, Code formatted]

Answers (1)

Geoff Hayes
Geoff Hayes on 13 Feb 2016
Shyam - err has been initialized as a 1x50 array, X and Xold are 6x1 arrays and so the absolute of their difference is a 6x1 array too. Your line of code
err(:,i)= abs(X-Xold);
is trying to assign a 6x1 array to the ith element of err. So an array is being assigned to a scalar. What is the intent here? Do you wish to sum these errors and assign them to the ith element of err as
err(i) = sum(abs(X-Xold));
or do you wish to do something else?
(As an aside it is good practice to avoid using i and j as indexing variables as MATLAB uses them to represent the imaginary number.)

Community Treasure Hunt

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

Start Hunting!