The difference between the results of int function and numerical solution

Hi,
I have a problem with the integral of a function in symbolic matlab. The function is:
t = -(a*(b*c -(b^3*c^3/6)) + c*(1 - b^2*c^2/2))/(c^m*sin(m*pi/2));
And the symbolic MATLAB gives the integral of the function as:
f = int(t,c) = (c^2*(6*m + 6*b^2*c^2 - 24*a*b + 2*g*b^3*c^2 - 3*m*b^2*c^2 + 6*a*m*b - a*m*b^3*c^2 - 24))/(6*c^m*sin((pi*m)/2)*(m^2 - 6*m + 8))
The problem is that, for a definite integral, for example; the solution of:
int(t,c,x,y) and the numerical solution f(y)-f(x) does not match.
Anyone have any idea for this problem?

6 commentaires

Can you show how the results do not match?
Erdal Cokmez
Erdal Cokmez le 26 Août 2022
Modifié(e) : Erdal Cokmez le 26 Août 2022
Hi,
I solved that problem but I have a new problem now.
In the given code, I tried obtain the integral of f1 function with symbolic matlab but I couldn't.
Consequently, in the code given below, I replaced the trigonometric functions in f1 with taylor series. But there is quite a difference between the two results. Do you have any idea to overcome this problem?
a = 1;
b = 2.1642;
c = 7.854;
d = 0.4;
m = 1;
syms x
f1 = -(a*sin(d*x) + x*cos(d*x))/(x^m*sin(m*pi/2));
f2 = -(a*(d*x -(d^3*x^3/6)) + x*(1 - d^2*x^2/2))/(x^m*sin(m*pi/2)); % Taylor series expansion
x1 = (1/(c-b))*int(f1,x,b,c);
x2 = (1/(c-b))*int(f2,x,b,c); % Taylor series expansion
fprintf(' x1 = %f\n x2 = %f\n ',x1,x2)
x1 = 0.155078
x2 = 1.119527
I don't understand why you use Taylor expansion. The integral can be evaluated - either by "int" or "integral".
format long
a = 1;
b = 2.1642;
c = 7.854;
d = 0.4;
m = 1;
syms x
f1 = -(a*sin(d*x) + x*cos(d*x))/(x^m*sin(m*pi/2));
f2 = -(a*(d*x -(d^3*x^3/6)) + x*(1 - d^2*x^2/2))/(x^m*sin(m*pi/2)); % Taylor series expansion
x1 = double((1/(c-b))*int(f1,x,b,c))
x1 =
0.155077599379342
x2 = double((1/(c-b))*int(f2,x,b,c)) % Taylor series expansion
x2 =
1.119527067520000
x3 = 1/(c-b)*integral(matlabFunction(f1),b,c)
x3 =
0.155077599379343
x4 = 1/(c-b)*integral(matlabFunction(f2),b,c)
x4 =
1.119527067520000
Erdal Cokmez
Erdal Cokmez le 26 Août 2022
Modifié(e) : Erdal Cokmez le 26 Août 2022
In my code, the variables a, b, c, d and m are symbolics. I tried to get analytical equation instead of int(f1). But the Matlab couldn't provide the integral of f1 in symbolic Matlab. Then I tried to use Taylor expansion instead of trigonometric expressions to obtain an analytical equation that can be used instead of integral directly. But using Taylor expansion gives quite different results and again I couldn't obtain an analytical equation.
In summary
syms a b c d m x
f1 = -(a*sin(d*x) + x*cos(d*x))/(x^m*sin(m*pi/2));
x1 = (1/(c-b))*int(f1,x,b,c);
% The symbolic Matlab couldn't get a result for x1, so I tried to use Taylor expansion instead of trigonometric expressions which is f2, to obtain an analytical equation. But again I couldn't.
My aim is to obtain x1 in symbolic matlab.
Thanks
Torsten
Torsten le 26 Août 2022
Modifié(e) : Torsten le 26 Août 2022
b and c are very far away from 0. You would have to include many more terms in the Taylor expansion to approximate f1 good enough for the integrals of f1 and f2 to approach each other. I suggest you use the numerical "integral" function to integrate matlabFunction(f1) - it's fast and precise.
But a Taylor expansion is ONLY an approximation, and one that often is not even convergent. So why could you possibly be surprised if the results are different from A Taylor expansion?
For example, feel free to evaluate sin(20*pi) using a Taylor expansion in double precision arithmetic. You won't be able to do so, because the number of terms you would need before it converges will be too large, and then you will find massive subtractive cancellation kills any accuracy you would see anyway.
What is worse, you have stuff in the denominator. So that means you have essential singularities in your function. And Taylor series are notoriously poor when used to approximate singular functions, since a truncated Taylor series is just a polynomial. And polynomials have NO singularities in them.

Connectez-vous pour commenter.

 Réponse acceptée

Torsten
Torsten le 26 Août 2022
Déplacé(e) : Matt J le 27 Août 2022

2 commentaires

Erdal Cokmez
Erdal Cokmez le 27 Août 2022
Déplacé(e) : Matt J le 27 Août 2022
Yes, I got it. Thanks a lot for your help.
@Erdal Cokmez if so, you should Accept-click Torsten's answer.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by