Integral doesn't work

조회 수: 4 (최근 30일)
Florian Spicher
Florian Spicher 2021년 10월 28일
댓글: Star Strider 2021년 10월 28일
I'm supposed to implement a program which, for a given partition xi and a continuous fonction on [a,b], computes the solution of the linear system Au=r, where , (for which I have computated formulas) and , .
My problem is that my function r does not run. The integral function is apparently not happy with my definition of my bj's and I really don't have much ideas how to solve the problem. I'd much appreciate some help.
Edit: Errors in the command window:
Error using integralCalc/finalInputChecks (line 526)
Output of the function must be the same size as the input. If FUN is an array-valued integrand, set the
'ArrayValued' option to true.
Error in integralCalc/iterateScalarValued (line 315)
finalInputChecks(x,fx);
Error in integralCalc/vadapt (line 132)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 75)
[q,errbnd] = vadapt(@AtoBInvTransform,interval);
Error in integral (line 87)
Q = integralCalc(fun,a,b,opstruct);
Here's my code:
%{
The function b takes as arguments: an index j, a real number x between
a=x_0 and b=x_{n+1}, and the vector of the points x_i of the partition g.
b computes bj(x).
%}
function bj=b(j,x,xi)
n=size(xi,2)-2; %There are n+2 points in the partition
switch j
case 1
if (x>=xi(1) & x<=xi(2))
bj=(xi(2)-x)./(xi(2)-x(1));
else
bj=0;
end
case n
if (x>=xi(n+1) & x<=xi(n+2))
bj=(x-xi(n+1))./(xi(n+2)-xi(n+1));
else
bj=0;
end
otherwise
if (x>=xi(j-1) & x<=xi(j))
bj=(x-xi(j-1))./(xi(j)-xi(j-1));
elseif (x>=xi(j) & x<=xi(j+1))
bj=(xi(j+1)-x)./(xi(j+1)-xi(j));
else
bj=0;
end
end
end
%{
The function A computes the matrix A defined as in the exercise 5.1.a
%}
function mat=A(xi)
n=size(xi,2)-2;
mat=zeros(n,n);
h=zeros(n,1);
for k=1:n+1
h(k)=xi(k+1)-xi(k);
end
for i=1:n
for j=i:n
if i==j
switch i
case 1
mat(i,j)=1/h(1);
case n
mat(i,j)=1/h(n+1);
otherwise
mat(i,j)=(h(i)+h(i+1))*(h(i)*h(i+1)+3)/(3*h(i)*h(i+1));
end
elseif i==j-1
mat(i,j)=(h(i)+h(i+1))/3;
mat(j,i)=mat(i,j);
else
mat(i,j)=0;
mat(j,i)=0;
end
end
end
end
%{
The function r takes a continuous function f and the partion as arguments,
and computes the rj's (and hence r) with the bj's
%}
function int_r=r(f,xi)
n=size(xi,2);
int_r=zeros(1,n);
for j=1:n
g=@(x)f(x).*b(j,x,xi);
int_r(j)=integral(g,0,1);
end
end

채택된 답변

Star Strider
Star Strider 2021년 10월 28일
I have no idea what the arguments are, or what ‘doesn’t work’ means.
However, since the integrand is apparently an array, see if:
int_r(j)=integral(g,0,1, 'ArrayValued',true);
produces the correct result.
NOTE — The ‘int_r’ variable may also need to be defined as an array for this to work correctly.
.
  댓글 수: 6
Florian Spicher
Florian Spicher 2021년 10월 28일
Mine is R2021a, so maybe that's why. However, using your "ArrayValued",true corrected the errors (which I'm going to publish, for the record) and now not only have I a result, but also it is coherent!
Thanks a lot, you saved me!
Star Strider
Star Strider 2021년 10월 28일
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by