intrgartion_problem..

1 次查看(过去 30 天)
george veropoulos
Hi im trying to run a code i call in some point the folllow lines
y_theory=@(t)((1./(P.*Zl)).*f_xi(t./(Zl*P)))./(g_norm);
x=0:1e-6:V2L;
yi=@(x)integral(y_theory,0,x,'ArrayValued',true)
plot(x,yi(x),'r','LineWidth',2)
xlabel('|V_{L}|(V)')
ylabel('f(|V_{L}|)(1/v)')
and i recieve the message
Error using integral (line 86)
A and B must be floating point scalars.
Error in @(x)integral(y_theory,0,x,'ArrayValued',true)
Error in main_distr_polarization4VL_cum (line 160)
plot(x,yi(x),'r','LineWidth',2)
what is the problem?
thank you in advance George

回答(1 个)

John D'Errico
John D'Errico 2016-9-1
编辑:John D'Errico 2016-9-1
integral does not have the capability to solve a problem with multiple limits of integration. The help never states that the limits of integration can be anything other than a scalar value.
The arrayvalued flag is not designed to solve arrays of limits, but array valued functions! There is a big difference. From the help for integral:
'ArrayValued', FUN is an array-valued function when the input is scalar
That you want it to work in away that it is not designed to work is not relevant. Of course, nothing stops you from using a loop to accomplish this task. You can even make it efficient. Thus...
Since x is the upper limit of integration, and x varies as
x=0:1e-6:V2L;
then use integral to compute the integral between each increment in x, then use cumsum to compute the cumulative integral. This avoids forcing integral to compute the integral of the same region multiple times.
Sometimes a loop is the simplest solution. But assuming that code will somehow magically know what it is that you wanted to do is rarely the correct approach.
  3 个评论
Walter Roberson
Walter Roberson 2016-9-1
y_theory=@(t)((1./(P.*Zl)).*f_xi(t./(Zl*P)))./(g_norm);
x=0:1e-6:V2L;
yi_parts = arrayfun( @(idx) integral(y_theory, x(idx), x(idx+1)), 1:length(x)-1 );
yi = cumsum( yi_parts );
John D'Errico
John D'Errico 2016-9-1
Walter has it right. Though it could be done using a loop too.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by