# Matlab integral with parameter

I'm a beginner with Matlab, and I'm trying to solve the following problem.

I'd like to define a function $$F(x) = \int_0^{+\infty} \frac{\sin t}{1 + xt^4} \, dt$$ and plot it on the interval $$[1,3]$$.

I wrote the following code:

x = 1:0.01:3;
y = [];

for i = 1:201
xi = 1 + (i-1)/100;
y(i) = quadgk(@(t)sin(t)./(xi*t.^4 + 1), 0, inf);
end

plot(x,y)


This looks ridiculously complicated to me, and there's no obvious way to compute values of $$F(x)$$ after this.

I'd like to know how experienced Matlab users would program this.

• Matlab's main purpose is not to do symbolic calculations. It can be done, as you've shown. But usually (at least in my experience) Matlab is used for numerics rather than symbolics. Oct 28 '14 at 20:03
• In this case, I'm mainly interested in numerical calculations. I'd like to simplify the syntax, even if internally Matlab is doing the equivalent of a for loop. Oct 28 '14 at 20:03
• You do not need to integrate up to $\infty$ (I guess you only want a plot and not an superaccurate answer) Since the function falls off like $1/(1+xt^4)$ it should be enough to go up to say $xt^4 \approx 10^2 - 10^3$, i.e. try to integrate from $t=0$ to $t=10$. It should speed up the computation. Oct 28 '14 at 20:05
• Doesn't Matlab do that automatically? I mean, can't it bound the integrand so that it decides where to cut off the integral? Oct 28 '14 at 20:06
• Matlab's quadgk does allow integration over infinite intervals (as long as the integrand decays rapidly enough). Its methods are rather more sophisticated, I think, than picking a finite cutoff. Oct 28 '14 at 20:24

I might do it this way:

integrand = @(x) (@(t) sin(t) ./ (1+x .*t .^4));

x = 1:0.01:3;
y = arrayfun(F,x);

plot(x,y) • This doesn't work for me. I get "Error using ^ Inputs must be a scalar and a square matrix. To compute elementwise POWER, use POWER (.^) instead." and some other stuff. I'm using a 2012 version of Matlab. Could that be why? Or is it something to do with declaring the types of the variables? Oct 28 '14 at 20:28
• Did you have to enter any commands like "syms x", etc., before executing this? Oct 28 '14 at 20:37
• Oops, left out some "."'s. Fixed now. Oct 28 '14 at 21:10
• It works now. If I understand correctly, integrand is a function which, evaluated at x, gives you a function of t. So integrand(x) is actually a function of t. Is that right? Oct 28 '14 at 21:21
• Yes, that's right. Oct 28 '14 at 21:23