I am trying to complete the code below which is an iteration code. Yes I finally got it to work, but when i put the stipulations in for the flag, found and the bottom.
I now get "Output argument "error" (and maybe others) not assigned during call to "jacobi"."
I know it has to do with the else statement when flag=0 instead of 1 because everything was working fine until I tried to put the else coding in and now i get this error. Any help would be very much appreciated.
when i step into the code it works all the way with correct answers and matrix values. When i am in the final if statment for if flag=1 it will skip down to the else section, print out everything that i want it to print out and then when the green arrow goes to the final end statment with the function and i click next, it gives off the error above.
how can it work all the way until the final end statement. I have to be missing something. I am new to this so forgive me if this is something easy.
function [x error niter flag ] =jacobi(A,x,b,maxiter, tol)
if isrow(x)==1
x=x';
end
if isrow(b)==1
b=b';
end
if n ~= m
disp('The matrix has to be square for this function, please enter a
matrix that is sqaure');
end
index=1;
Dinv= inv(diag(diag(A)));
D=diag(diag(A));
flag=0;
y=x;
while index <= maxiter
z = Dinv*((D-A)*y+b);
if norm(z-y)<tol
flag=1;
err=abs(norm(z-y));
break
end
y=z;
index=index+1;
end
if flag==1
niter=index;
x=z;
error=err;
else
maxindex='you have reached the maximum iterations of %d which is larger
than %d.';
niter=index;
maxiter=maxiter;
sprintf(maxindex,niter,maxiter);
end
end
flagis 0, you are not assigning anything toerror, so the function doesn't know what to give as that output. And you have to change the name of that variable,erroris a saved word and can cause unpredicted behavior.