I have an issue with Matlab integration. It tells me that there is an error about dimensions. However they do agree and operations are properly done using vectorized operators (.^ .* etc...). It is simple code yet I am stuck
A = 1:10;
B = 1:10;
K_fun = #(x) (x ./ sqrt((x + A.^2 ) .* (x + B.^2) .* (x + B.^2)) );
K = integral( K_fun, 0,Inf );
and here the error message in the command window:
Error using +
Matrix dimensions must agree.
Error in #(x)(x./sqrt((x+A.^2).*(x+B.^2).*(x+B.^2)))
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
Error in integralCalc/vadapt (line 133)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 84)
[q,errbnd] = vadapt(#AToInfInvTransform,interval);
Error in integral (line 89)
Q = integralCalc(fun,a,b,opstruct);
Error in PROVA_New_Drag3 (line 21)
K = integral( K_fun , 0 , Inf);
Thank you in advance
It evaluates both your integration limits in a vector, so effectively your xin the function is [0 Inf], which is why the lengths don't agree.
You can set the 'ArrayValued' flag to true in the integral call
K = integral( K_fun, 0,Inf,'ArrayValued', true );
to get it to evaluate them separately.
I got another warning about a singularity but this is likely because you're using 0, infinity and division so it's related more to your function than the integration call. It might help to to sprinkle some eps around in K_fun.
UPDATE: Please see Troy's explanation of the singularity warning and note that eps won't actually help with that.
Related
I need to compute this kind of integral
I defined the two f(x,y) and g(x,y) inside a single function
h = {#(x,y) f, #(x,y) g};
where f and g are formulas defined before. In this way I have a 1x2 cell and I want to compute the scalar product as
F = #(x,y) dot(h,E)
where E is a vector 2x1 and then integrate
int = integral2(F,a,b,c,d);
giving me this error
Undefined function 'conj' for input arguments of type 'cell'.
Error in dot (line 37)
c = sum(conj(a).*b);
Error in tm_np>#(R,PHI)dot(e_n_even,E)
Error in integral2Calc>integral2t/tensor (line 228)
Z = FUN(X,Y); NFE = NFE + 1;
Error in integral2Calc>integral2t (line 55)
[Qsub,esub] = tensor(thetaL,thetaR,phiB,phiT);
Error in integral2Calc (line 9)
[q,errbnd] = integral2t(fun,xmin,xmax,ymin,ymax,optionstruct);
Error in integral2 (line 106)
Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);
I don't understand why is giving me this error. As result of the integral I should have a 2x1 vector
The problem is the generation of your data. integral2 accepts only functions that get #(x,y) as vector input and return the same size. in your case h is already not that, and F has no #(x,y) inputs (well, it has, but you decided not to pass them to h!!!).
% sample data
f=#(x,y)x.^2+y;
g=#(x,y)y.^2+x+3;
E=[3 1];
% Create auxiliary fucntion
mydot=#(x,y)(f(x,y).*E(1)+g(x,y).*E(2));
% work
int = integral2(mydot,0,3,1,5);
Starting from the vector Psi_0 I define the propagated vector as Psi (t) = exp{-iHt} Psi_0, where H is the Adjacency matrix (but I think it is irrelevant to my problem here). I need to calculate
1/tau * int^tau_0 |<j|psi(t)>|^2 dt
I tried to do this in the following way but it doesn't work
psi_0 = diag(eye(N))/N;
Psi_t = zeros(N);
Psi_sqared = #(t) (expm(1j*A*t)*psi_0).*(expm(-1j*A*t)*psi_0);
c_tqw = integral(Psi_sqared, 0, 10000)/10000;
The error is the following
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the
second matrix. To perform elementwise multiplication, use '.*'.
Error in centrality_measure>#(t)(expm(1j*A*t)*psi_0).*(expm(-1j*A*t)*psi_0)
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 88)
Q = integralCalc(fun,a,b,opstruct);
Error in centrality_measure (line 75)
c_tqw = integral(Psi_sqared, 0, 10000)/10000;
Any suggestion to avoid this error?
I have been trying to figure out this integral for some time, but have come up short. I have tried doing a symbolic integration, but I get it shot back out at me, so I am assuming there is not solution that way. I have resolved to solve it with a definite integral, but still keep getting errors:
clear
clc
x = 1;
y = 1;
z = 1;
R = 2;
b =#(theta) y.*cos(theta)/((x-R.*cos(theta)).^2+y.^2+(z -
R.*sin(theta)).^2).^(3/2)
integral(b,1,2)
My current error is:
Error using integralCalc/finalInputChecks (line 515)
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 88)
Q = integralCalc(fun,a,b,opstruct);
Any help would be very appreciated!
You need to change the division sign in the first term from / to ./ to ensure that you are doing element-wise division and not matrix right division:
b = #(theta) y.*cos(theta)./((x-R.*cos(theta)).^2+y.^2+(z - ...
R.*sin(theta)).^2).^(3/2);
integral(b,1,2)
ans =
0.055781612354862
I try to get an integral of two function handles in Matlab. The first function handle would be a weibull probability density function and the second function handle is based on a cfit I created with linear interpolation of single points.
x = 0:0.1:35;
fun1 = #(x) wblpdf(x,weibullAlpha,weibullBeta);
fun2 = #(x) feval(cfitObject,x);
fun3 = #(x) (fun(x).*fun2(x));
y = integral(fun3,0,35); % Using quad(fun3,0,35) doesn't work either.
I receive the following error:
Error using integralCalc/finalInputChecks (line 515)
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 88)
Q = integralCalc(fun,a,b,opstruct);
Error in test (line 7) % "test" is the name of the script file.
y = integral(fun3,0,35);
The problem must have to do something with "fun2" since the code works just fine with e.g.
fun2 = x.^2;
Note: if I plot fun2 with the cfitObject I don't get an error. It's also possible to integrate the function using quad().
x = 0:0.1:35;
fun2 = #(x) feval(cfitObject,x);
y = quad(fun2,0,35);
plot(x, fun2(x))
Any help is greatly appreciated!
Your code seems to be ok. Probably the problem is that fun2 cannot take vectorized input, it could be resolved by modifying fun2 (cfitObject) to be able to handle vector input or telling the software that the function in the integral is array valued:
y = integral(fun3, 0, 35, 'ArrayValued', 1);
So im trying to plot f(t) which is a piecewise function using heaviside and ezplot functions in MATLAB. Now im not very familiar with MATLAB at all. If anyone knows why im getting this error it would be helpful.
f = '12+(-2t+8)*heaviside(t-2)+(2t-12)*heaviside(t-6)'
f = 12+(-2t+8)*heaviside(t-2)+(2t-12)*heaviside(t-6)
ezplot(f)
Error using inlineeval (line 15) Error in inline expression ==> 12+(-2t+8).*heaviside(t-2)+(2t-12).*heaviside(t-6) Error: Unexpected MATLAB expression.
Error in inline/feval (line 34)
INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);
Error in ezplotfeval (line 52)
z = feval(f,x(1));
Error in ezplot>ezplot1 (line 469)
[y, f, loopflag] = ezplotfeval(f, x);
Error in ezplot (line 145)
[hp, cax] = ezplot1(cax, f{1}, vars, labels, args{:});
You need to tell Matlab to multiply explicitly, use 2*t instead of 2t.
syms t real
f = 12+(-2*t+8)*heaviside(t-2)+(2*t-12)*heaviside(t-6)
ezplot(f,[0 10])