small trapz clarification needed

I would appreciate some clarification.
I attempt the following:
zz=zeros(1,31);
for xx=linspace(0,30,31);
x=0:xx;
y=(c*56e9*3.14e7*(1+x).^2)./(1+(1+x).^2);
zz(1,xx+1)=trapz(x,y);
end
I get the following errata description:
??? Error using ==> colon
Maximum variable size allowed by the program is exceeded.
Error in ==> trapz at 43
perm = [dim:max(ndims(y),dim) 1:dim-1];
Error in ==> Cs_kSZ at 14
eta(1,xx+1)=trapz(x,y);
What is the particular problem?

1 个评论

I deleted the duplicate of this question that you posted earlier.

请先登录,再进行评论。

回答(1 个)

[FYI, I put in c=1 to get your code to work at all.]
In the first iteration of your for loop, you are calling trapz() with these inputs:
trapz(0,8.79e17)
which is I suppose you mean to be zero (because you are "integrating" over a single point). However, MATLAB does not handle that case very well, possibly due to floating point error.
I think you might get what you want by skipping that first evaluation, using this modification of your code:
zz=zeros(1,31);
for xx=linspace(1,30,30);
x=0:xx;
y=(c*56e9*3.14e7*(1+x).^2)./(1+(1+x).^2);
zz(1,xx+1)=trapz(x,y);
end

类别

帮助中心File 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