How do I solve this integral in matlab?

1 Comment

Hi @Aditya Zade, if you wish to call a specific user for help, you can use this special character "@".
You can also type out the integrand function in MATLAB by clicking the indentation icon .
For example
% declare symbols
syms t
% assign a value
w_g = 120*pi;
% create a function
func = - sin(w_g*t - pi/6)
func = 

Sign in to comment.

 Accepted Answer

% declare symbols
syms t
Pi = sym(pi);
% assign a value
w_g = 2*Pi*60;
% create a function
func = - sin(w_g*t - Pi/6) .* sin(w_g*t) / sin(w_g * t - 2*Pi/3)
func = 
Integration = simplify(3*w_g/Pi * int(func, t, 0, 3*w_g, hold=true))
Integration = 
F = matlabFunction(Integration);
format long g
INT = F()
Warning: Reached the limit on the maximum number of intervals in use. Approximate bound on error is 1.9e+04. The integral may not exist, or it may be difficult to approximate numerically to the requested accuracy.
INT =
-21492.6584717154

7 Comments

@Aditya Zade, if you plot the integrand function, you can see the oscillatory singularities (division-by-zero) repeat at regular intervals along the time-axis.
w_g = 120*pi;
func = @(t) - sin(w_g*t - pi/6).*sin(w_g*t)./sin(w_g*t - 2*pi/3);
t = linspace(0, 0.1, 10001);
plot(t, func(t)), ylim([-250 250]), grid on
xlabel('Time')
Hi @Sam Chak, thank you for the clarification. However, I am trying to integrate from t=0 to t=1/360 where the function is continuous. So, I thought I could integrate it.
Mathematica also produced the same result as MATLAB. You can follow the example provided by @Walter Roberson. The integrand function is likely intended to differ from yours to encourage you to learn how to write the code independently.
This will reinforce your memory in learning how to calculate definite integrals (the area under a curve between two fixed limits) in MATLAB.
@Aditya Zade, you can also try the integral() function.
% integrand
fun = @(x) 5*sech(5*x).^2;
lb = -1; % lower limit
ub = 1; % upper limit
% Evaluate the integral from x = -1 to x = 1.
q = integral(fun, lb, ub)
q = 1.9998
This did the job in matlab:
wg = 2 * pi * 60 ;
syms time real
func = -sin( ( wg * time ) - ( pi / 6 ) ) * sin( ( wg * time ) - ( pi / 6 ) ) * sin( wg * time ) / sin( ( wg * time ) - ( 2 * pi / 3 ) )
func = 
Integration = simplify( ( 3 * wg / pi ) * int( func, time, 0, pi / ( 3 * wg ) ), 1000 )
Integration = 
Thank you both of you for the help.
This is what I am getting: 1/4 - (3*3^(1/2))/(8*pi)
If you attempt to solve a mathematical equation or an integral problem using functions from the Symbolic Math Toolbox, the answers will be returned as symbolic expressions.
format long
Integration = 1/4 - (3*3^(1/2))/(8*pi)
Integration =
0.043251664216828

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!