0

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.

  • 1
    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. – NicNic8 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. – user188062 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. – Winther Oct 28 '14 at 20:05
  • 1
    Doesn't Matlab do that automatically? I mean, can't it bound the integrand so that it decides where to cut off the integral? – user188062 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. – Robert Israel Oct 28 '14 at 20:24

1 Answers1

1

I might do it this way:

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

F = @(x) quadgk(integrand(x),0,Inf);

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

plot(x,y)

enter image description here

Robert Israel
  • 448,999