quad function error

조회 수: 3 (최근 30일)
James
James 2011년 12월 5일
Ok when I run the program below using the regular 'quad' function I get the following error: "The integrand function must return an output vector of the same length as the input vector." Now if I use the 'quadv' function I get a matrix of values. Does anyone know how I can get a single value for the 'lift_co'
function [induced_angle, lift_co,induce_drag_co] = Task1_2_372(v,d,b,s)
y = -b/2:.1:b/2;
D = .05;
lift_dist = d*v*sqrt(1-((2*y)/b).^2); %lift distribution in this case elliptical
circ_dist = lift_dist./(d*v);
c2 = 2/(v*s); %calculate constants
F1 = @(y)(c2.*circ_dist);
lift_co = quadv(F1,-b/2,b/2); %lift coefficient
display(lift_co)
figure(4)
plot(y,lift_co)

답변 (1개)

Walter Roberson
Walter Roberson 2011년 12월 5일
You define "D" but use "d".
You use "b" and "v" in multiple places but never define them. You won't get anywhere unless "b" and "v" have definite values. You use "s" in one place but never define it; your code will fail if it does not have a definite value.
Your anonymous function F1 has a parameter "y" that you never use.
You define a specific value (vector) for "y", and you use that specific vector in calculating lift_dist . If we presume that the assignment to "D" is really an assignment to "d", and if we assume that v and b have specific values, the result would be a specific value (vector) for lift_dist. Then on the next line, that vector together with d and v would define a specific value (vector) for circ_dist . If we then further assume that "s" has a specific value, the c2 line would assign a specific value to c2. If we then look at the F1 anonymous function, since you do not use the dummy parameter "y" in the expression and all elements after that have specific values, your F1 function is calculating a specific value (vector).
As you have noticed, quad does not like a vector output, which is what your F1 calculates. You have to ask yourself what it means to do a numeric integration of an unchanging value vector over a variable range that plays no role in the calculation.
I would suggest that you have significantly misunderstood how far back an anonymous function can reach. An anonymous function definition looks at the right hand side, grabs and "locks in" the current value (as of the time of execution) of all of the variables mentioned except for the ones listed as dummy parameters in the @() part, and builds code in which everything is constant except for the dummy variables.
So... you probably want something like this:
d = 0.05;
lift_dist = @(y) d*v*sqrt(1-((2*y)/b).^2); %lift distribution in this case elliptical
circ_dist = @(y) lift_dist(y) ./ (d*v);
c2 = 2/(v*s); %calculate constants
F1 = @(y)(c2.*circ_dist(y));
lift_co = quad(F1,-b/2,b/2); %lift coefficient
display(lift_co)
Notice that at no point in this code is y ever given a specific value: y is only a dummy parameter. Note also that nothing is plotted: a plot is not useful as lift_co will be a single numeric value (scalar).
If you are trying to integrate at very specific points such as -b/2:.1:b/2, rather than using adaptive quadrature, and if you want to display a plot that shows the cumulative integral from -b/2 to each point, then rather than using the quad*() family, you should be using cumtrapz()
  댓글 수: 4
Walter Roberson
Walter Roberson 2011년 12월 5일
In my earlier write-up, ignore the first two paragraphs, and in the third, replace my clause
If we presume that the assignment to "D" is really an assignment to "d",
with
If we presume that "d" has a specific value,
Everything else I wrote still applies.
Walter Roberson
Walter Roberson 2011년 12월 5일
Oh, and in my "you probably want" code, change the "d =" to "D ="

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

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by