19 views (last 30 days)

Show older comments

can i use 'integral' for an analytic integration in case definite integral compare with using 'int'? because when using 'int ' taken time to implement a program.

please,help me, i would like to know about

- error between them ,
- is there an equivalent code for 'int' and
- is 'quad' or ' integral' sufficient. What do you advise me to avoid the accumulated error where it is used several times in the same code?
- Can i Implement ' int' to evaluate int(cos(pi*x).^n,x) , n>=1, by matlab.

This is my code.

>> syms x n

>> int(cos(pi*x).^n,x, -10,10)

- This is what I'm getting by MATLAB 2013 a

Warning: Explicit integral could not be found.

ans =

int(cos(pi*x)^n, x == -10..10)

John BG
on 18 Jun 2017

Hi Work Wolf

1.

command int is for symbolic, to just obtain the primitive, which is what you are after,

int(expr,var)

or definite symbolic integral

int(expr,var,a,b)

command integral is for numerical integration

2.

having said this, int doesn't work for certain expressions, it's a limitation directly mention in MATLAB help. One tries

syms x;int(sin(sinh(x)),x)

= int(sin(sinh(x)), x)

the displayed answer is the same trivial input, like your function (cos(x))^n

3.

but there's a way around

F=(cos(x))^n;int(taylor(F,x,'ExpansionPoint',0,'Order',10),x)

F =

(n^4/3456 - n^3/864 + (7*n^2)/4320 - (17*n)/22680)*x^9 + (- n^3/336 + n^2/168 - n/315)*x^7 + (n^2/40 - n/60)*x^5 - (n*x^3)/6 + x

an approximation of the primitive for (cos(x))^n has been obtained.

4. Now the approximation error:

Please note that you are already in control of the error because you decide how long the input taylor development is, in the field 'Order'.

to measure the error after the integral, turn x and n into numerical values, integrate and obtain the difference with the exact expression

I don't know if you only want n integer, or you may want to check n real, (or complex and then measure for instance real and imaginary integrals)

perhaps the following is enough for the error checks, the following lines can be implemented with a for loop, but don't know the range of n and x you want.

when n=1

n=1;

x=[0:1/10:2*pi];F1=(cos(x)).^n;A0=integral(@(x)(cos(x)).^n,0,pi/2)

A0 = 1.0000

x1=0;A1= (n^4/3456 - n^3/864 + (7*n^2)/4320 - (17*n)/22680)*x1^9 + (- n^3/336 + n^2/168 - n/315)*x1^7 + (n^2/40 - n/60)*x1^5 - (n*x1^3)/6 + x1

A1 = 0

x2=pi/2;A2= (n^4/3456 - n^3/864 + (7*n^2)/4320 - (17*n)/22680)*x2^9 + (- n^3/336 + n^2/168 - n/315)*x2^7 + (n^2/40 - n/60)*x2^5 - (n*x2^3)/6 + x2

A2 = 1.0000

err1=A0-(A2-A1)

err1 = -3.5426e-06

when n=2

n=2;

F2=(cos(x)).^n;A0=integral(@(x)(cos(x)).^n,0,pi/2)

x1=0;A1= (n^4/3456 - n^3/864 + (7*n^2)/4320 - (17*n)/22680)*x1^9 + (- n^3/336 + n^2/168 - n/315)*x1^7 + (n^2/40 - n/60)*x1^5 - (n*x1^3)/6 + x1

x2=pi/2;A2= (n^4/3456 - n^3/864 + (7*n^2)/4320 - (17*n)/22680)*x2^9 + (- n^3/336 + n^2/168 - n/315)*x2^7 + (n^2/40 - n/60)*x2^5 - (n*x2^3)/6 + x2

err1=A0-(A2-A1)

A0 = 0.7854

A1 = 0

A2 = 0.7871

err1 = -0.0017

when n=3

n=3;

F2=(cos(x)).^n;A0=integral(@(x)(cos(x)).^n,0,pi/2)

x1=0;A1= (n^4/3456 - n^3/864 + (7*n^2)/4320 - (17*n)/22680)*x1^9 + (- n^3/336 + n^2/168 - n/315)*x1^7 + (n^2/40 - n/60)*x1^5 - (n*x1^3)/6 + x1

x2=pi/2;A2= (n^4/3456 - n^3/864 + (7*n^2)/4320 - (17*n)/22680)*x2^9 + (- n^3/336 + n^2/168 - n/315)*x2^7 + (n^2/40 - n/60)*x2^5 - (n*x2^3)/6 + x2

err1=A0-(A2-A1)

A0 = 0.6667

A1 = 0

A2 = 0.7130

err1 = -0.0463

5. It's also worth mentioning that if you only want the primitive with symbolic x, that it's ok to sweep n, then a verbose but nevertherless valid primitive is obtained expanding the product:

syms x n

int(cos(x)*cos(x),x)

= x/2+sin(2*x)/4

int(cos(x)*cos(x)*cos(x),x)

=sin(x)-sin(x)^3/3

int(cos(x)*cos(x)*cos(x)*cos(x),x)

=(3*x)/8+sin(2*x)/4+sin(4*x)/32

this primitives, without symbolic n, are exact.

if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?

To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link

thanks in advance

John BG

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!