DanilGholtsman
DanilGholtsman

Reputation: 2374

matlab vector output

Well I try to make vector output with this code. Just some function which results i need to get in vector form

  function [smthOut] = myErf(start, wstep, final) 

     for k = start:wstep:final 
        error_func = 2/sqrt(pi) * erfTaylor(k);
        smthOut(final/wstep) = error_func;
        disp(['ON x=', num2str(k), ' RES IS f= ',  num2str(error_func)]);
     end;

  end

but output is looks like this

>> s = myErf(0, 0.2, 2)
ON x=0 RES IS f= 0
ON x=0.2 RES IS f= -0.0029732
ON x=0.4 RES IS f= -0.022959
ON x=0.6 RES IS f= -0.073171
ON x=0.8 RES IS f= -0.1606
ON x=1 RES IS f= -0.28568
ON x=1.2 RES IS f= -0.44374
ON x=1.4 RES IS f= -0.62745
ON x=1.6 RES IS f= -0.82906
ON x=1.8 RES IS f= -1.042
ON x=2 RES IS f= -1.2614

s =

  Columns 1 through 8

         0         0         0         0         0         0         0         0

  Columns 9 through 10

         0   -1.2614

What's worng? How to fix it? Thanks!

Upvotes: 0

Views: 588

Answers (1)

H.Muster
H.Muster

Reputation: 9317

Since final/wstep is constant (2/0.2 = 10), in each iteration of the loop the same field of the array gets filled with results (i.e., smthOut(10)).

You can try something like

function [smthOut] = myErf(start, wstep, final) 

   cnt = 1;
   for k = start:wstep:final 
      error_func = 2/sqrt(pi) * erfTaylor(k);
      smthOut(cnt) = error_func;
      disp(['ON x=', num2str(k), ' RES IS f= ',  num2str(error_func)]);
      cnt = cnt + 1;
   end;

end

Upvotes: 1

Related Questions