How do I delete specific parts of a graph

9 次查看(过去 30 天)
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

采纳的回答

Torsten
Torsten 2025-7-8
移动:Torsten 2025-7-8
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 个评论
Torsten
Torsten 2025-7-8
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

请先登录,再进行评论。

更多回答(1 个)

Umar
Umar 2025-7-9

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.

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by