필터 지우기
필터 지우기

Error using quad "Error using quad (line 75) The integrand function must return an output vector of the same length as the input vector."

조회 수: 2 (최근 30일)
I have edited the code according to answers.
Can anyone explain the reason behind the errors and help me correcting the following code:
U = 1;
e = @(q) 2*(1-cos(2*pi*q));
hq = @(q,n0) ((e(q)).^2+2*U*n0*(e(q))).^0.5;
y = @(q,n0) (((e(q))+(U*n0))/hq(q,n0))-1;
a = -0.5;
b = 0.5;
v = @(n0) quad(@(q) y(q,n0),a,b);
cv =@(n0) n0+(0.5*v(n0))-1;
while U < 20
n0 = 0.1;
options = optimset('Display', 'iter');
n = fsolve(cv(n0),n0,0.1,options);
plot(U,n)
hold on
U = U + 1;
end
Errors:
Error using quad (line 75)
The integrand function must return an output vector of the same length as the input vector.
Error in @(n0)quad(@(q)y(q,n0),a,b)
Error in @(n0)n0+(0.5*v(n0))-1
Error in simulv1 (line 12)
n = fsolve(cv(n0),n0,0.1,options);

채택된 답변

Star Strider
Star Strider 2015년 2월 25일
You forgot to fully vectorise those equations. Change them to:
hq = @(q,n0) ((eq(q)).^2+2*U*n0*(eq(q))).^0.5;
y = @(q,n0) (((eq(q))+(U*n0))./hq(q,n0))-1;
and see if that helps.
  댓글 수: 5
A Jenkins
A Jenkins 2015년 2월 25일
I would guess that your y is the wrong size because you have missed Star Strider's second suggestion: that you also need ./ instead of / in the line that calculates y.
Star Strider
Star Strider 2015년 2월 25일
@jayash — Evaluate ‘y(q,n0)’ (and your other functions as well) for representative values of both arguments to see what it does. Always evaluate the functions you give to solver routines first to be certain that they work and give you the results you expect.
@A Jenkins — Thank you. That very well could be the problem.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

dpb
dpb 2015년 2월 25일
_"I am using .^ but..."
But you aren't, everywhere. The line given by the error message is--
hq = @(q,n0) ((eq(q))^2+2*U*n0*(eq(q))).^0.5;
The term (eq(q))^2 doesn't have a 'dot' operator just as the error says.
BTW, do NOT use eq as a variable or function name; that aliases the builtin logical "equal" function == or eq()
>> which eq
built-in (C:\ML_R2012b\toolbox\matlab\ops\@double\eq) % double method
>>

A Jenkins
A Jenkins 2015년 2월 25일
There are two powers in that line and you have only replaced one.
@(q,n0)((eq(q)) ^ 2+2*U*n0*(eq(q))) .^ 0.5

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by