0

好吧,我尝试使用此代码进行矢量输出。只是一些我需要以矢量形式获得的功能

  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

但输出看起来像这样

>> 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

穿什么?如何解决?谢谢!

4

1 回答 1

1

由于final/wstep是常数(2/0.2 = 10),因此在循环的每次迭代中,数组的相同字段都会被结果填充(即smthOut(10))。

你可以尝试类似的东西

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
于 2013-02-19T18:25:39.203 回答