Buoy move with ocean wave

7 次查看(过去 30 天)
Omer Mert
Omer Mert 2023-12-12
评论: Adam Danz 2023-12-12
Hello guys,
I am here to ask a simulation question. I am trying to model how to buoy makes up and down according to ocean waves. The problem is I couldn't add the sine wave in the figure. Here is the code what i wrote;
clear all;clc
radiusofcrank = 1;
angular_velocity = 60; % angular velocity in radians per second
for t = linspace(0, 1, 1000)
x_crank = radiusofcrank * cos(angular_velocity * t); %motion of the crank radius
y_crank = radiusofcrank * sin(angular_velocity * t);
x_buoy = 0; %x axis of buoy
y_buoy = y_crank - 5; % makes buoy moves up and down
plot(x_crank, y_crank); %crank
hold on;
plot([0, x_crank], [0, y_crank], 'black'); %crank radius
plot(x_buoy, y_buoy, 'o', 'MarkerSize', 50, 'MarkerFaceColor', 'b'); %buoy
plot([x_crank, x_buoy], [y_crank, y_buoy], '-', 'Color', 'b'); % Connecting rod
theta = linspace(0, 2*pi, 100); %plots circle (2pi = 360 degree)
circle_x = radiusofcrank * cos(theta);
circle_y = radiusofcrank * sin(theta);
plot(circle_x, circle_y);
hold off;
title('Buoy Motion');
xlabel('X-coordinate');
ylabel('Y-coordinate');
axis equal;
grid on;
pause(0.01);
end
% Sine Wave
t = 0.1:100;
Wave_amplitude_sine = 3; % Amplitude of the sine wave
wave_frequency = 0.01; % Frequency of the sine wave (in Hz)
% Create the figure and plot initial sine wave
h = plot(t,sin(2 * pi * wave_frequency * t));
title('Ocean Wave');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
for time = 0:1:1000
phase = 2 * pi * wave_frequency * time; % Update the phase based on time and speed
set(h, 'YData', sin(2 * pi * wave_frequency * t + phase));
pause(0.028);
end
I need your help. Thank you!
  1 个评论
Omer Mert
Omer Mert 2023-12-12
编辑:Voss 2023-12-12
Here is the code I wrote. I hope it will be useful for everyone;
clear all;
clc;
radiusofcrank = 1;
angular_velocity = 60; % angular velocity in radians per second
figure;
% Sine Wave parameters
wave_amplitude_sine = 1; % Amplitude of the sine wave
wave_frequency = 0.1; % Frequency of the sine wave (in Hz)
h_sine = plot(0, -5, 'b-', 'LineWidth', 2); % Initialize with a single point
title('Buoy Motion with respect to Sine wave');
xlabel('X-coordinate');
ylabel('Y-coordinate');
grid on;
hold on;
h_crank = plot(0, 0, 'ro-', 'MarkerSize', 10, 'LineWidth', 2);
h_buoy = plot(0, 0, 'go', 'MarkerSize', 20, 'MarkerFaceColor', 'g');
h_rod = plot([0, 0], [0, 0]);
h_radius = plot([0, 0], [0, 0]);
h_circle = plot(0, 0, 'black', 'LineWidth', 2);
% Set the x-axis limits
xlim([-2, 2]);
% Set the y-axis limits
ylim([-6, 2]);
% Initialize circle coordinates outside the loop
circle_x_fixed = radiusofcrank * cos(linspace(0, 2*pi, 100));
circle_y_fixed = radiusofcrank * sin(linspace(0, 2*pi, 100));
% Update circle outside the loop
set(h_circle, 'XData', circle_x_fixed, 'YData', circle_y_fixed);
for t = linspace(0, 1, 1000)
t_s = 0:1:100;
% Update the phase of the sine wave based on time
phase = 2 * pi * wave_frequency * t;
% Calculate x-coordinates of the sine wave based on time (horizontal motion)
x_sine = t_s - 100 * t;
% Update the sine wave plot
set(h_sine, 'XData', x_sine, 'YData', wave_amplitude_sine * sin(2 * pi * wave_frequency * t_s + phase) - 5);
% Crank motion
x_crank = radiusofcrank * cos(angular_velocity * t);
y_crank = radiusofcrank * sin(angular_velocity * t);
% Buoy motion
x_buoy = 0;
y_buoy = y_crank - 5;
% Update plot objects
set(h_crank, 'XData', x_crank, 'YData', y_crank);
set(h_buoy, 'XData', x_buoy, 'YData', y_buoy);
set(h_rod, 'XData', [x_crank, x_buoy], 'YData', [y_crank, y_buoy]);
set(h_radius, 'XData', [0, x_crank], 'YData', [0, y_crank]);
% Set the y-axis limits
ylim([-6, 2]);
pause(0.01);
end
hold off;
You can adjust the sine wave to make the buoy makes up and down correctly.

请先登录,再进行评论。

回答(1 个)

Adam Danz
Adam Danz 2023-12-12
> The problem is I couldn't add the sine wave in the figure
Add hold on after the first loop so that when you add the sine wave it doesn't replace existing content in the figure.
  2 个评论
Adam Danz
Adam Danz 2023-12-12
That's not enough information to describe the problem.

请先登录,再进行评论。

类别

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

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by