Compiling issue in doing integration of large function containing two variables

6 views (last 30 days)
My script consists of two varible(theta and phi) functions.
While doing integration w.r.t 'phi' it is not giving result, taking too much time to compile. I waited 2 hours still it was running. Dont know what was wrong in the script, since it is not giving any error also.
My script was given below
syms theta phi
m = 150;
Aos = 40;
nAi = 0;
for i=1:2:m
nAi = nAi+cos(i*phi);
end
nA = vpa((Aos+nAi), 4)
wA = vpa((nA-(1/(2*pi))*int(nA, phi, 0, 2*pi)), 4)
Aog = 52;
k = 40;
Ginvi = 0;
for i=2:2:k
Ginvi = Ginvi+cos(i*(phi-theta));
end
Ginv = vpa((Ginvi+Aog), 4)
l = int((nA*wA*Ginv), phi, 0, 2*pi)
Iam unable to get 'l'(last one) result.
Nothing wrong in the code but still its not giving either result or error. Its just running running hours and hours.
Any suggestions for solving this.
Thanks in advance.

Answers (1)

MULI
MULI on 1 Mar 2024
Hi Teja,
Based on the script which you provided, it is symbolic integration and when the expressions are complicated it may take very long time to compute and in some cases might not be able to give closed-form solution.
So here I am providing the code suggesting for numerical integration which is faster than symbolic integration in such cases.
% Define the symbolic variables
syms theta phi
% Define the constants
m = 150;
Aos = 40;
Aog = 52;
k = 40;
% Calculate nAi using a symbolic sum
nAi = sum(cos((1:2:m)*phi))
% Calculate nA symbolically
nA = Aos + nAi;
% Calculate the integral part of wA symbolically and then the full wA
integral_nA = int(nA, phi, 0, 2*pi) / (2*pi);
wA = nA - integral_nA;
% Calculate Ginvi using a symbolic sum
Ginvi = sum(cos((2:2:k)*(phi-theta)));
Ginv = Aog + Ginvi;
% Convert symbolic expressions to MATLAB functions for numerical evaluation
nA_func = matlabFunction(nA, 'Vars', [phi]);
wA_func = matlabFunction(wA, 'Vars', [phi]);
Ginv_func = matlabFunction(Ginv, 'Vars', [phi, theta]);
% Perform numerical integration for l
% Define a range for theta if you want to evaluate l as a function of theta
theta_val = 0; % Example value for theta
l_numeric = integral(@(phi) nA_func(phi) .* wA_func(phi) .* Ginv_func(phi, theta_val), 0, 2*pi);
% Display the result
disp(l_numeric);
In the provided code “matlabFunction” takes a symbolic expression and converts into MATLAB function handle and can be used to evaluate the expression numerically.
You may refer to this documentation link for more information on “matlabFunction”

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!