How do I delete specific parts of a graph

Hi, I'm plotting arc length of a function and I want to plot two curves; the first one is the entire curve and the second one only the arc length from x1 to x2. I want to distinguish the plotted arc length from that of the entire curve. This is my code:
clear, clc
syms x y real
y=(1/3)*(nthroot(x^2+2,2)^3); x1=0; x2=3;
y1=subs(y,x1); y2=subs(y,x2);
g=fplot(y); hold on; grid on;
f=fplot(y,[x1,x2]); set(f,'color','r','Linewidth', 1.5, 'Linestyle','--'); hold on;
plot(x1,y1,'.', 'color', 'b','markersize',15); hold on;
plot(x2,y2,'.', 'color', 'b','markersize',15); hold off;
I'd like to remove the first plot (g) for the range of x1 to x2 if possible so that I only show the dotted plot for that range which is the arc length.
Thanks

 Accepted Answer

The arclength of a function will always be increasing. Thus your graph cannot be the arclength of a function.
clear, clc
syms x y real
y=(1/3)*(nthroot(x^2+2,2)^3); x1=0; x2=3;
y1=subs(y,x1); y2=subs(y,x2);
hold on
fplot(y,[-5 0],'color','b');
fplot(y,[x1,x2],'color','r','Linewidth', 1.5, 'Linestyle','--');
fplot(y,[x2 5],'color','b');
plot(x1,y1,'.', 'color', 'b','markersize',15);
plot(x2,y2,'.', 'color', 'b','markersize',15);
hold off
grid on
●

5 Comments

Great! The length of that arc is 12 units. Now, if I change the value of x2 to 2 and compute the arc length, I'm able to get an approximate value, but how do I get an exact value?
syms x y real
y=(1/3)*(nthroot(x^2+2,2)^3); x1=0; x2=2;
dy=diff(y);
discriminant=sqrt(1+(dy)^2);
Length_Exact=int(discriminant,x1,x2);
fprintf('The arc length is exactly %s\n', Length_Exact)
The arc length is exactly int((x^2*(x^2 + 2) + 1)^(1/2), x, 0, 2)
fprintf('The arc length is approximately %.4f\n', Length_Exact)
The arc length is approximately 4.6667
The exact answer should be 14/3
syms x y real
y=(1/3)*(nthroot(x^2+2,2)^3); x1=0; x2=2;
dy=diff(y);
discriminant=sqrt(1+(dy)^2);
Length_Exact=int(discriminant,x1,x2);
simplify(Length_Exact, 'steps', 100)
ans = 
@Walter Roberson: I'm using MATLAB 2021a version and I still get the answer as 4.6667 with your code.
syms x y real
y=(1/3)*(nthroot(x^2+2,2)^3); x1=0; x2=2;
dy=diff(y);
discriminant=simplify(sqrt(1+(dy)^2));
Length_Exact=int(discriminant,x1,x2);
fprintf('The arc length is exactly %s\n', Length_Exact)
The arc length is exactly 14/3

Sign in to comment.

More Answers (1)

Umar
Umar on 9 Jul 2025

Hi @rezheen,

So, I did review @Torsten and @Walter Robertson comments, and highly respect their input. To achieve your desired outcome, I did break down the task into several steps. First, I plotted the entire curve, computed the arc length for the specified range, and then modified the plot to only show the arc length segment. Below is the complete MATLAB code that accomplishes this:

   % Clear workspace and command window
   clear; clc;
   % Define symbolic variables
   syms x y real
   % Define the function
   y = (1/3) * (nthroot(x^2 + 2, 2)^3);
   % Define the range for arc length
   x1 = 0; 
   x2 = 2;
   % Calculate the y-values at x1 and x2
   y1 = subs(y, x, x1); 
   y2 = subs(y, x, x2);
   % Plot the entire curve
   g = fplot(y, 'LineWidth', 1.5); 
   hold on; 
   grid on;
   % Calculate the derivative of the function
   dy = diff(y); 
   % Calculate the discriminant for arc length
   discriminant = sqrt(1 + (dy)^2);
   % Compute the exact arc length
   Length_Exact = int(discriminant, x, x1, x2);
  fprintf('The arc length is exactly %s\n', Length_Exact);
   % Calculate the approximate arc length
   Length_Approx = double(subs(Length_Exact));
   fprintf('The arc length is approximately %.4f\n', Length_Approx);
   % Plot the arc length segment as a dotted line
   f = fplot(y, [x1, x2], 'r', 'LineWidth', 1.5, 'LineStyle', '--'); 
   hold on;
   % Plot the endpoints of the arc length
   plot(x1, y1, '.', 'Color', 'b', 'MarkerSize', 15); 
   plot(x2, y2, '.', 'Color', 'b', 'MarkerSize', 15); 
   % Remove the original curve for the range of x1 to x2
   set(g, 'Visible', 'off');
   % Finalize the plot
   title('Arc Length of the Function');
   xlabel('x-axis');
   ylabel('y-axis');
   legend('Entire Curve', 'Arc Length Segment', 'Endpoints', 'Location', 'Best');
   hold off;

Please see attached.

By following the provided code, you can effectively distinguish between the entire curve and the specific arc length segment, while also computing the exact arc length. If you have any further questions or need additional modifications, feel free to ask!

Hope this helps.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Release

R2021a

Tags

Asked:

on 8 Jul 2025

Answered:

on 9 Jul 2025

Community Treasure Hunt

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

Start Hunting!