# Buoy move with ocean wave

6 ビュー (過去 30 日間)
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
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)
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 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Omer Mert 2023 年 12 月 12 日

Here is the code I wrote. I hope it will be useful for everyone;
clear all;
clc;
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 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 件のコメントなしを表示なしを非表示
Omer Mert 2023 年 12 月 12 日
it didn't work
Adam Danz 2023 年 12 月 12 日
That's not enough information to describe the problem.

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeFilter Analysis についてさらに検索

R2023b

### Community Treasure Hunt

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

Start Hunting!

Translated by