Different results from vpaintegral() and int() on piecewise expression.

7 views (last 30 days)
syms theta
create_polar_curve = @(theta, params) symfun(...
piecewise(params(2) < theta < pi-params(2),...
params(1) + params(3) * (-1+cos( (2*pi)/(pi-2*params(2)) * (theta-params(2)))),...
pi + params(2) < theta < 2*pi-params(2),...
params(1) + params(3) * (-1+cos( (2*pi)/(pi-2*params(2)) * (theta-params(2)-pi))),...
params(1) + theta*0 ...
), theta);
params = [25 1 2];
r = create_polar_curve(theta, params);
double(arc_length(r,[0 1],[2*pi 4]))
double(arc_length_numerical(r,[0 1],[2*pi 4]))
function length_vector = arc_length(r, theta_1, theta_2)
syms theta;
integral(theta) = int(sqrt(r(theta)^2 + diff(r(theta))^2 ),thet
length_vector = integral(theta_2)-integral(theta_1);
end
function length_vector = arc_length_numerical(r, theta_1, theta_2)
syms theta;
length_vector = zeros(1,numel(theta_1));
for i = 1:numel(length_vector)
length_vector(i) = vpaintegral(sqrt(r(theta)^2 + diff(r(theta))^2 ),theta,theta_1(i),theta_2(i));
end
end
output :
ans =
157.0796 75.0000
ans =
155.4071 74.1637
>>
The script above creates a symbolic function representing a curve in polar coordinates, and calculates the arc length of that curve over two intervals. For some reason, computing the integral symbolically is giving slightly incorrect results. I would much prefer to use the symbolic integral, as I need to compute with thousands of different limits of integration. Computing the integral once, and evaluating it multiple times is far faster than approximating the integral multiple times.
Is there any reason int() shouldn't be working here?
  1 Comment
Karan Nandankar
Karan Nandankar on 8 Mar 2021
Hi,
For complex expressions, int() evaluates numerical approximation for the definite integral of the function. You can refer to the documentation below for more information:

Sign in to comment.

Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!