Getting an indexing error when using functions
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Tyler Michael Paparella
el 24 de Feb. de 2024
Respondida: Walter Roberson
el 24 de Feb. de 2024
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 comentario
Torsten
el 24 de Feb. de 2024
Editada: Torsten
el 24 de Feb. de 2024
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) ?
Respuesta aceptada
Walter Roberson
el 24 de Feb. de 2024
uexact = uexact + uelem(x1,x2,k,L);
Okay, you are invoking uelem() on a scalar x1
out = integral(@(x) ff(x,x1,x2,k,L),x1,x2);
uelem() takes the scalar x1 as the upper bound of the integral() and invokes ff() passing in a vector of values for the first parameter.
f = k^2*cos(2*pi*k*xi/L);
The vector of values is used to compute a vector of f.
out = integral(@(x) f(x),0,1)/ @(x) A(x,x1,x2);
The integral() call passes a vector of x values to the vector of scalars that is f. This fails because the vector of x values does not happen to be valid indices into the vector of f values.
... Even if the integral() just happened to work, you would not be able to divide the result of the integral by an anonymous function.
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrix Computations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!