About using quad in MATLAB
-
13-07-2021 - |
質問
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');
解決
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.
所属していません StackOverflow