0
$\begingroup$

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.

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

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

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – user188062 Oct 28 '14 at 20:28
  • $\begingroup$ Did you have to enter any commands like "syms x", etc., before executing this? $\endgroup$ – user188062 Oct 28 '14 at 20:37
  • $\begingroup$ Oops, left out some "."'s. Fixed now. $\endgroup$ – Robert Israel Oct 28 '14 at 21:10
  • $\begingroup$ 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? $\endgroup$ – user188062 Oct 28 '14 at 21:21
  • $\begingroup$ Yes, that's right. $\endgroup$ – Robert Israel Oct 28 '14 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.