Getting an indexing error when using functions
Show older comments
I am trying to build a function uelem that takes the integral of another function ff. ff itself is the integral of a function f divided by another function A. I am getting the following error. It says I am trying to index something. I am guessing it has somthing to do with the way I am trying to define functions and use parameters. Can someone tell me what I am doing wrong?
Array indices must be positive integers or logical values.
Error in hw3_872>@(x)f(x) (line 133)
out = integral(@(x) f(x),0,1)/ @(x) A(x,x1,x2);
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
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);
Error in hw3_872>ff (line 133)
out = integral(@(x) f(x),0,1)/ @(x) A(x,x1,x2);
Error in hw3_872>@(x)ff(x,x1,x2,k,L) (line 137)
out = integral(@(x) ff(x,x1,x2,k,L),x1,x2);
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
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);
Error in hw3_872>uelem (line 137)
out = integral(@(x) ff(x,x1,x2,k,L),x1,x2);
Error in hw3_872 (line 32)
uexact = uexact + uelem(x1,x2,k,L);
N = 100;
L = 1;
uexact = 0;
x1 = 0;
x2 = L/N;
for e = 1:N
uexact = uexact + uelem(x1,x2,k,L);
x1 = x2;
x2 = L/N*(e+1);
end
function out = A(xi,x1,x2)
x = 0.5*(1 - xi)*x1 + 0.5*(1+xi)*x2;
if x < .1
out = 2 + 0.*x;
elseif x < .2
out = 1.5 + 0.*x;
elseif x < .3
out = 1.75 + 0.*x;
elseif x < .4
out = 1.5 + 0.*x;
elseif x < .5
out = 3.75 + 0.*x;
elseif x < .6
out = .75 + 0.*x;
elseif x < .7
out = 1.25 + 0.*x;
elseif x < .8
out = .75 + 0.*x;
elseif x < .9
out = 2 + 0.*x;
elseif x <= 1
out = 1 + 0.*x;
end
end
function out = ff(xi,x1,x2,k,L)
f = k^2*cos(2*pi*k*xi/L);
out = integral(@(x) f(x),0,1)/ @(x) A(x,x1,x2);
end
function out = uelem(x1,x2,k,L)
out = integral(@(x) ff(x,x1,x2,k,L),x1,x2);
end
1 Comment
It's not clear what x is in f and in A.
f = k^2*cos(2*pi*k*xi/L);
out = integral(@(x) f(x),0,1)/ @(x) A(x,x1,x2);
And you cannot divide integral(@(x) f(x),0,1) by a function handle, only by the result of an evaluation of A. But what is x in A(x,x1,x2) ?
Accepted Answer
More Answers (0)
Categories
Find more on Numerical Integration and Differentiation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!