How to perform symbolic integration?

52 views (last 30 days)
I am trying to perform symbolic integration of a complex equation. The code is given below. But every time I run the code the result in the command window displays the last line. What am I doing wrong. Can anyone help me solving this?
clc
clear all
syms phi wt pi
V=((cos(wt)-cos(phi)*((sin(phi)-(phi*cos(phi))))^(1/2))*(sin(phi)));
int(V,phi,wt,pi)
  2 Comments
Abolfazl Chaman Motlagh
Abolfazl Chaman Motlagh on 17 Feb 2022
Edited: Abolfazl Chaman Motlagh on 17 Feb 2022
can you describe what you trying to do. you want to integrate V as a function of phi with lower bound of wt and upperbound of pi ?? or you want an indefinite triple integration over V ??
Md. Golam Zakaria
Md. Golam Zakaria on 17 Feb 2022
Edited: Md. Golam Zakaria on 17 Feb 2022
@Abolfazl Chaman Motlagh I want to integrate V as a function of phi with lower bound of wt and upperbound of pi

Sign in to comment.

Accepted Answer

Abolfazl Chaman Motlagh
Abolfazl Chaman Motlagh on 17 Feb 2022
it doesn't seems your function has a clear close form primitive function. (or at least it is not easy for matlab symbolic toolbox to find it.) so it's better to find functionality from numerical integration:
v = @(x,t0) (((cos(t0)-cos(x).*((sin(x)-(x.*cos(x)))).^(1/2)).*(sin(x))));
choose the and integrate it numerically:
t0 = 0;
integral(@(x) v(x,t0),t0,pi)
ans = 2.5513
or if you want functionality over lowerbound numerically solve integration over some grid points.
your function depend on starting point of integration, that's why i use this form.
t0 = 0:0.05:pi;
int_V = zeros(length(t0),1);
for i=1:length(t0)
int_V(i) = integral(@(x) v(x,t0(i)),t0(i),pi);
end
plot(t0,int_V,'linewidth',2); xlabel('\theta_o'); ylabel('F(\theta_o)')
title('F(\theta_o)=\int_{\theta_o}^{pi}V(\theta) d\theta')
finally if you need a close expression for your function, you can interpolate this function. and of course made it more precise with increasing number of grid points in t0.
  2 Comments
Md. Golam Zakaria
Md. Golam Zakaria on 17 Feb 2022
Edited: Md. Golam Zakaria on 17 Feb 2022
@Abolfazl Chaman Motlagh you have actually solved my whole problem . Also, I have checked in my program that problem arises when I add the power i.e (^(1/2)). If I remove this it works fine. There are times when I need the output expression of an integration. Doing integration manually is a tedious work. is that any way I can make my code work so that I can get the result of the integration as expression.
Thank you very much.
Abolfazl Chaman Motlagh
Abolfazl Chaman Motlagh on 17 Feb 2022
Good to hear it works.
as i said the numerical array is now close to values of integration in points that we integrate. so a simple way to find a close expression is interpolation.
remember not all functions can be integrated Indefinitely. not all functions have perimiteve function. also not all functions are easy to integrate. maybe you need to define a new functions for their integration. like a common case integration of 1/x .in these cases the the interpolation can give you a functional expression close enough to real function.
there are a lot of method for interpolation. like polynomial interpolation, or spline interpolation. or maybe more efficient in your case (because you have trigonometric functions) trigonometric interpolation.
in matlab for this task you can use curve fitting toolbox. it also has an apps.
here's what you can do:
after running above code type this:
cftool(t0,int_V); % or sftool(t0,int_V)
wait for app to show up.
in top from list of equation select your desired type, for example fourier. and then select number of terms. more terms lead to less error. but lead to more complex expression. you can save it to workspace using fit tab. it will give you the expression and it's parameters that fitts to this data:

Sign in to comment.

More Answers (1)

John D'Errico
John D'Errico on 17 Feb 2022
Do you presume that every integral you write down has a symbolic, analytical solution? Is that perhaps really a good idea?
syms phi wt pi
V=((cos(wt)-cos(phi)*((sin(phi)-(phi*cos(phi))))^(1/2))*(sin(phi)))
V = 
In this case, I'd suggest that phi inside and out of the trig functions, and inside a square root is a problem. So when int just gives up, and displays the result as what you wanted to solve, that means it was unable to find a solution.
By the way, declaring pi as a symbolic variable there is probably a bad idea.
vpa(pi)
ans = 
π
pi is no longer defined as the number 3.14159..., but now as just a variable named pi. And now MATLAB can now no longer use known properties of pi and how trig functions behave in concert with pi.
  1 Comment
Md. Golam Zakaria
Md. Golam Zakaria on 17 Feb 2022
@John D'Errico Good Sir, I dont understand that much about matlab symbolic math. I just want to integrate V as a function of phi with lower bound of wt and upperbound of pi , and get the output expression. All I need the output expression.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!