How i can make an animation mp4 for the graph?
4 ビュー (過去 30 日間)
古いコメントを表示
I have found a point 'P(x,y)' connecting 3 points A(6,9),B(20,38),C(49,25) such that the total length PA+PB+PC is minimum by the code
M=500;%we take a fixed value of M
x=6:.01:49; % p is the domain of x
y=9:.01:38; % q is the domain of y
for i=1:length(x)
for j=1:length(y)
L=sqrt((x(i)-6)^2+(y(j)-9)^2)+sqrt((x(i)-20)^2+(y(j)-38)^2)+sqrt((x(i)-49)^2+(y(j)-25)^2);
if L<M
M=L;
a=x(i);
b=y(j);
end
end
end
[a,b]
P(x,y)=(23.1200 29.5000)
Then draw the figure of point P joint three point A,B,C
x = [6 20 49];
y = [9 38 25];
x0 = 23.1200;
y0 = 29.5000;
figure('color','white');
hold on;
for ipt = 1:length(x)
plot([x(ipt) x0],[y(ipt) y0],'color',[0 0 1]);
myChar = char(64+ipt);
text(x(ipt)+0.5,y(ipt)+0.75,myChar);
end
plot(x,y,'P','markerfacecolor','k','markeredgecolor','none', ...
'markersize',4);
plot(x0,y0,'P','markerfacecolor','k','markeredgecolor','none', ...
'markersize',4);
text(x0+0.5,y0+0.75,'P');
xlabel('x');
ylabel('y');

How i can make an animation mp4 of the graph for different value of p ?
Thank you.
0 件のコメント
回答 (1 件)
Deepak
2024 年 12 月 10 日
To animate the optimization of point (P(x, y)) minimizing (PA + PB + PC), we can use “VideoWriter” function to capture frames during each iteration.
Initialize a “VideoWriter” to create an MP4 file, and as the loops iterate over (x) and (y), plot the position of (P) and its connections to (A), (B), and (C). Use “getframe” to capture these plots and write them to the video using “writeVideo” function. Adjust the step size of (x) and (y) to balance smoothness and speed, creating an animation that visually demonstrates path of (P) to the optimal position.
Below is the MATLAB code to achieve the same:
% Initialize parameters
M = 500; % Initial large value for minimum length
x = 6:1:49; % Coarser domain for x
y = 9:1:38; % Coarser domain for y
% Create a video writer object
v = VideoWriter('minimization_animation.mp4', 'MPEG-4');
v.FrameRate = 5; % Lower frame rate
open(v);
% Initialize figure
figure('color', 'white');
hold on;
xlabel('x');
ylabel('y');
xlim([5, 50]);
ylim([8, 40]);
% Plot fixed points A, B, C
fixedX = [6, 20, 49];
fixedY = [9, 38, 25];
plot(fixedX, fixedY, 'ko', 'MarkerFaceColor', 'r');
% Annotate points A, B, C
text(fixedX(1)+0.5, fixedY(1)+0.75, 'A');
text(fixedX(2)+0.5, fixedY(2)+0.75, 'B');
text(fixedX(3)+0.5, fixedY(3)+0.75, 'C');
% Iterate over x and y to find minimum path and create animation
for i = 1:length(x)
for j = 1:length(y)
% Calculate total length
L = sqrt((x(i)-6)^2 + (y(j)-9)^2) + ...
sqrt((x(i)-20)^2 + (y(j)-38)^2) + ...
sqrt((x(i)-49)^2 + (y(j)-25)^2);
% Check if this is the minimum length so far
if L < M
M = L;
a = x(i);
b = y(j);
end
% Plot the current position of P and connecting lines
plot([fixedX a], [fixedY b], '-b');
plot(a, b, 'ko', 'MarkerFaceColor', 'k');
text(a+0.5, b+0.75, 'P');
% Capture the frame every nth iteration
if mod(i*j, 5) == 0 % Capture every 5th frame
frame = getframe(gcf);
writeVideo(v, frame);
end
% Clear the plot for the next frame, keeping the fixed points
clf;
hold on;
plot(fixedX, fixedY, 'ko', 'MarkerFaceColor', 'r');
text(fixedX(1)+0.5, fixedY(1)+0.75, 'A');
text(fixedX(2)+0.5, fixedY(2)+0.75, 'B');
text(fixedX(3)+0.5, fixedY(3)+0.75, 'C');
xlabel('x');
ylabel('y');
xlim([5, 50]);
ylim([8, 40]);
end
end
% Close the video
close(v);
disp(['Minimum length found at P(', num2str(a), ',', num2str(b), ') with length ', num2str(M)]);
Please find attached the documentation of functions used for reference:
VideoWriter: www.mathworks.com/help/matlab/ref/videowriter.html
I hope this assists in resolving the issue.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!