0

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
1
  • When flag is 0, you are not assigning anything to error, so the function doesn't know what to give as that output. And you have to change the name of that variable, error is a saved word and can cause unpredicted behavior. Commented Sep 6, 2017 at 6:22

1 Answer 1

0

Your problem is that you specify an output of:

[x error niter flag ]

but if you go into the else statement, you are not defining, x nor error, thus it cannot output these variables. Note that not setting x does technically not yield and error as you get it as input, but then the output x is equal the input x.

Personally I would prefer a version which casts a warning:

niter=index;
x=z;
error=err;
if flag==0
    warning(strcat('You have reached the maximum iterations of , int2str(niter),' which is larger than ',int2str(maxiter)));
end

instead of

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;
    x=z;
    error=err;
    sprintf(maxindex,niter,maxiter);
end
Sign up to request clarification or add additional context in comments.

5 Comments

x is not a problem, he gets it as an input.
Well, error-wise you are right, but I suppose it is still not the x-value that he wants to return, thus I mentioned it.
thank you all. Yes, I am aware of the error issue, and the changes would be great, but I unfortunately have been given this task and that output is required. Thank you as now I at least do not get that error. i changed the last part to this but do not get the string anymore
the code i have now is if flag==1 niter=index; x=z; error=err; else x='N/A'; error='N/A'; maxindex='you have reached the maximum iterations of %d which is larger than %d.'; niter=index; maxiter=maxiter; sprintf(maxindex,niter,maxiter); end
I cannot get the code to be formatted correct for this comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.