5

I encounter a strange problem with quad function. I was using quad to calculate simple integral, and it worked for 10 to 20 times, then Matlab issues the following error:

Error using quad (line 75)
The integrand function must return an output vector of the same length as the input vector. 
yteor(k) = quad(@(q)(exp(-(q).^2).*q.^2/(k.^2+1)), 0, 1);

Here q and k are scalars. I can not get what is wrong and why it worked several hours ago.

Edit

Here is my code

for k=1:100,
    xteor(k)=step*k;
    yteor(k)=quad(@(q)(exp(-(q).^2).*q.^2/((step.*k+1).^2)),0,1);
end plot(xteor,yteor,'r');
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
  • 2
    The error message seems pretty self-explanative. Could you show your code? (ideally, working and not working one) – FabienAndre Oct 22 '12 at 14:14
  • The whole code is too big so i will send the part which integrate and plot the graph: – Sergei Sokolov Oct 22 '12 at 14:40
  • for k=1:100 xteor(k)=step*k; yteor(k)=quad(@(q)(exp(-(q).^2).*q.^2/((step.*k+1).^2)),0,1); end plot(xteor,yteor,'r'); – Sergei Sokolov Oct 22 '12 at 14:41
  • Matlab thinks that i send him a vector but I send him a scalar. – Sergei Sokolov Oct 22 '12 at 15:04
  • 1
    You can edit your question (grey link under the post), and try to use Markdown formatting. Finally, it is often well seen to provide full working example (here, adding `step`, `xteor` and `yteor`initialisation. – FabienAndre Oct 22 '12 at 15:10
  • the code works fine for me. I would suggest you modify step to another variable name, such as `stepInterval`, as step is also a function name. – Rasman Oct 22 '12 at 19:48

1 Answers1

1

The following snippet works for me on Octave (Matlab GNU clone)

step = 1;
xteor = zeros(100,1);
yteor = zeros(100,1);

for k=1:100,
  xteor(k)=step*k;
  yteor(k)=quad(@(q)(exp(-(q).^2).*q.^2/((step.*k+1).^2)),0,1);
end
plot(xteor,yteor,'r');
pause

My hypothesis is that your error is the consequence of something else happening earlier in your code (maybe related to step not being a scalar?). Instead of focusing on this line where the error arise. Try to search what you have changed just before the error appear.

FabienAndre
  • 4,514
  • 25
  • 38
  • Yep, thank you. I looked through the code and found that somewhere I occasinally put "."))) Anyway thanks a lot for help!!! – Sergei Sokolov Oct 22 '12 at 16:06