multiple integrals in the same variable in MATLAB - matlab

I need to evaluate the following integral in MATLAB (numerically):
I already tried various things but I can't figure out how to solve this! Following is my last try:
Fdx = #(x) integral(#(y)1./(1+sqrt(y.^2))*(1-pi^2),0,x);
dFdx(1)
F = 8 * integral(dFdx,0,10)
As a result MATLAB gives me this error message:
Error using integral (line 85)
A and B must be floating-point scalars.
Error in #(x)integral(#(y)1./(1+sqrt(y.^2))*(1-pi^2),0,x)
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 test (line 7)
F=8 * integral(dFdx,0,10)

Try using integral2 instead. See example 2 in the documentation:
http://www.mathworks.com/help/matlab/ref/integral2.html
Hope it helps.

Related

Sequential numerical integration in MATLAB

I am trying to write the program in MATLAB to perform sequential integration. First I define the first integral n_array_exact{1}() and save it as function handle in the array. This first integral becomes a function of x only after the integration. As a next step I use this function to calculate next double integral (call it n_array_exact{2}(x)) w.r.t mu1 and s. Before substituting n_array_exact{1}(x) to calculate n_array_exact{2}(x), I replace x in n_array_exact{1}(x) to a function called xp(x, mu1,s) instead.
% Ordinary integration
n_array_exact{1} = #(x) integral(#(mu) exp((x.*mu)), -1,1)/2;
xp2= #(x,mu1,s) sqrt(x*x +s*s +2*x*mu1*s);
for i=2:3
n_array_exact{i} = #(x) integral2(#(mu1,s) exp(-s.*mu1.*x).*n_array_exact{1}(xp2(x,mu1,s)), 0,1,-1,1);
end
n_array_exact{2}(0.05)
%n_array_exact{3}(0.05)
%n_array_exact{4}(0.05)
After performing the integration and trying to evaluate n_array_exact{2}0.05) I got the following errors:
Error using .*
Matrix dimensions must agree.
Error in #(mu)exp((x.*mu))
Error in integralCalc/iterateScalarValued (line 315)
fx = FUN(t);
Error in integralCalc/vadapt (line 133)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 76)
[q,errbnd] = vadapt(#AtoBInvTransform,interval);
Error in integral (line 88)
Q = integralCalc(fun,a,b,opstruct);
Error in #(x)integral(#(mu)exp((x.*mu)),-1,1)/2
Error in #(mu1,s)exp(-s.*mu1.*x).*n_array_exact{1}(xp2(x,mu1,s))
Error in integral2Calc>integral2t/tensor (line 229)
Z = FUN(X,Y); NFE = NFE + 1;
Error in integral2Calc>integral2t (line 56)
[Qsub,esub] = tensor(thetaL,thetaR,phiB,phiT);
Error in integral2Calc (line 10)
[q,errbnd] = integral2t(fun,xmin,xmax,ymin,ymax,optionstruct);
Error in integral2 (line 106)
Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);
Error in #(x)integral2(#(mu1,s)exp(-s.*mu1.*x).*n_array_exact{1}(xp2(x,mu1,s)),0,1,-1,1)
Error in test (line 10)
n_array_exact{2}(0.05)

Array of function handle in matlab

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?

Integrating constant variable in MATLAB

I want to iterate an integral of the form
integral(f,-1,1)
where f = #(t) (s-t)*t, with s being some variable, with respect to which I'm not integrating (so that I'm getting a function of s as a result of integration).
So I defined s as syms s, but I'm getting the following response:
Error using integralCalc/finalInputChecks (line 511)
Input function must return 'double' or 'single' values. Found 'sym'.
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);
What can I do in order to integrate, say, s*dt from 0 to 1 and get s instead of errors in MATLAB?

How to plot x+y in MatLab using ezplot

I tried ezplot in MatLab
ezplot('1.395x-1.935y+16.65')
but got the following errors:
Error using inlineeval (line 14)
Error in inline expression ==> 1.395x-1.935y+16.65
Error: Unexpected MATLAB expression.
Error in inline/feval (line 33)
INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);
Error in ezplotfeval (line 51)
z = feval(f,x(1));
Error in ezplot>ezplot1 (line 468)
[y, f, loopflag] = ezplotfeval(f, x);
Error in ezplot (line 144)
[hp, cax] = ezplot1(cax, f{1}, vars, labels, args{:});
I think you forgot to put a multiplication sign (*). Try this:
ezplot('1.395*x-1.935*y+16.65')

MATLAB: How do you calculate the integral of an exponential function in matlab?

This is the code I have used:
delta=2*10^-6;
f=#(z) ('exp((z^2)/(2*(delta^2))))/(delta*sqrt(2*pi))');
z=0:(0.1*10^-6):(5*10^-6);
integral(f,0,(5*10^-6))
The following error messages come up:
Error using integralCalc/finalInputChecks (line 511)
Input function must return 'double' or 'single' values. Found
'char'.
Error in integralCalc/iterateScalarValued (line 315)
finalInputChecks(x,fx);
Error in integralCalc/vadapt (line 133)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 76)
[q,errbnd] = vadapt(#AtoBInvTransform,interval);
Error in integral (line 89)
Q = integralCalc(fun,a,b,opstruct);
Error in interfacetemp (line 4)
integral(f,0,(5*10^-6))
I'm trying to calculate the following integral: exp((z^2)/(2*(delta^2))))/(delta*sqrt(2*pi)) where delta is the standard deviation of the function and is known to be (2*10^-6). Anyone have any ideas?
Please, try this code.
delta = 2e-6;
f = #(z) exp((z.^2)/(2*(delta.^2)))/(delta*sqrt(2*pi));
integral(f, 0, 5e-6)
Your function should not be between quotes and you have to use .^ to calculate exponent