Bessel function integral - matlab

I try to compute the integral of the Bessel function with MATLAB but the result is not correct
syms x a
int(besselj(1,a*x)*x^2,0,1)
The result is
(2*besselj(1, a))/a^2 - besselj(0, a)/a
I expected instead (- besselj(0, a)/a).
Why did I get a wrong result? What do I need to do to get the correct integral?

Related

Symbolic multiplication of functions

I get an error when trying to multiply a symbolic function and a symbolic integral:
eta02=vpa(-i*omega2/((i*alpha2*lambda_0)^(2/3)),prec); %whatever
eta_inf2=vpa(vpa(((i*alpha2*lambda_0)^(1/3))*YMAX,prec)+eta02,prec); %whatever
%%
syms lu
syms x
myairyf(lu)= airy(lu);
mybairyf(lu)=airy(2,lu);
Gi2(x)=-(mybairyf*int(myairyf,lu,[eta_inf2,(x)])-myairyf*int(mybairyf,lu,[eta02,(x)]));
Error using sym/subsindex (line 769)
Invalid indexing or function definition. When defining a function, ensure that the arguments are symbolic variables and the body of the
function is a SYM expression. When indexing, the input must be numeric, logical, or ':'.
How can this be done? Additionally, how could I plot Gi2(x), given that it's symbolic?
By using the symfun function.
Gi2 = sumfun(-(mybairyf*int(myairyf,lu,[eta_inf2,(x)])-myairyf*int(mybairyf,lu,[eta02,(x)])), x);
Now Gi2(val) will evaluate the function for the value ´val´
Plotting then can be done by evaluating this function for the values you want to plot for, storing them in a vector, and plot the x and y values.
xvals = linspace(0,10,100);
yvals = Gi2(xvals);
figure
plot(xvals,vals)

Roots of integral of Airy function (matlab)

I want to solve the following equation
I am doing something wrong with the definition of the function but I still don't get it
function F=myairyint(x)
F=integral(#(x)airy,x,1000)
end
functi2=#(x) myairyint;
x0=-1:-1:-15;
fsolve(functi2,x0)
Why doesn't it work?
Your integral seems to have no roots in [-15 -1] and converges to zero. I have tested it via plotting:
x0 = linspace(-20,20);
airyint = arrayfun(#(x0) integral(#(n) airy(n), x0, inf), x0);
plot(x0, airyint);
I also tested it via fzero() such as:
f = #(x) integral(#(n) airy(n), x, inf);
fzero(f, 0)
Output is as expected:
ans =
115.8524
But it seems like every real numbers for x > 115.8524 will look like roots of this integral equation.
Additional:
fzero() is a function trying to find roots looking at the values of funtion between two dynamic intervals. If it catches minus and positive values, it is narrowing the interval to catch the root. But there is an error rate since you also can observe from this example. This equation will be zero only when x goes to infinity which means there is no real roots of this equation.

Using fsolve with complex roots [duplicate]

I want to solve the following equation
I am doing something wrong with the definition of the function but I still don't get it
function F=myairyint(x)
F=integral(#(x)airy,x,1000)
end
functi2=#(x) myairyint;
x0=-1:-1:-15;
fsolve(functi2,x0)
Why doesn't it work?
Your integral seems to have no roots in [-15 -1] and converges to zero. I have tested it via plotting:
x0 = linspace(-20,20);
airyint = arrayfun(#(x0) integral(#(n) airy(n), x0, inf), x0);
plot(x0, airyint);
I also tested it via fzero() such as:
f = #(x) integral(#(n) airy(n), x, inf);
fzero(f, 0)
Output is as expected:
ans =
115.8524
But it seems like every real numbers for x > 115.8524 will look like roots of this integral equation.
Additional:
fzero() is a function trying to find roots looking at the values of funtion between two dynamic intervals. If it catches minus and positive values, it is narrowing the interval to catch the root. But there is an error rate since you also can observe from this example. This equation will be zero only when x goes to infinity which means there is no real roots of this equation.

How to perform convolution using Fourier Series

OK let me cut to the chase.
I am trying to use MATLAB to
(i)generate the fourier series based on known coefficients and thereafter
(ii) determine the output function when the impulse is known.
So far I used this code to obtain the fourier series:
clear all
syms x k L n
evalin(symengine,'assume(k,Type::Integer)');
a = #(f,x,k,L) (2/(pi*k))* sin((pi*k)/(2 * L));
fs = #(f,x,n,L) (1/2*L) + symsum(a(f,x,k,L)*cos(k*2*pi*x/L),k,1,n);
f = x;
pretty(fs(f,x,11,1))
This works as desired. Now the impulse response is as follows:
h = heaviside(x) * exp(-5*x);
Now, in order to obtain the function, we need to perform the convolution with the respective functions.But when I input the following, I get the error:
x1 = fs(f,x,1,1);
conv(h,x1)
Undefined function 'conv2' for input arguments of type 'sym'.
Error in conv (line 38)
c = conv2(a(:),b(:),shape);
Any help would be appreciated
That is because conv is only defined for numeric inputs. If you want to find the convolution symbolically, you'll have to input the equation yourself symbolically using integration.
If you recall, the convolution integral is defined as:
Source: Wikipedia
Therefore, you would do this:
syms x tau;
F = int(h(tau)*x1(x-tau),'tau',-inf,+inf);
int is a function in MATLAB that does symbolic integration for you. Also note that the convolution integral is commutative, and so this also works:
Source: Wikipedia
Therefore, you should also get the same answer if you did:
syms x tau;
F = int(h(x-tau)*x1(tau),'tau',-inf,+inf);
Hope this helps!

plot matlab function handle that outputs vector

So I have a matlab function I wrote that takes in a number and returns an array of numbers (transposed). I need to plot this function. I was trying to use fplot but it was giving me errors:
Error in fplot (line 105)
x = xmin+minstep; y(2,:) = feval(fun,x,args{4:end});
Am I using the wrong plot function?
the function solves an equation of motion problem. I have this diff eq:
Mx'' + Cx'+ Kx = 0
where M, C, and K are 4x4 matrices and my function solves the general solution and outputs a vector of 4 values.
I fixed my problem by changing my function to accept an array of t values and then I used the plot function instead of fplot