Integral doesn't work

2 次查看(过去 30 天)
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 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by