Could I use a function to achieve this task?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi guys,
My code here generates a wave from a source (wave a). I want to create a diagonal line of N sources (Starting from wave a, ending at wave n). As you can see from my code, I've manually added a second wave b in diagonally, changing [x,y] manually as well - where it says %offsets secondary source).
I want to know if, instead of adding each source manually, I can create a function to do this that will generate N amounts of sources in a diagonal line, and I want there to be a time delay between each one. Wave a propagates first, then wave b, then wave c, a bit like a domino effect.
clear
%define the total time for simulation
T_total=5;
%set the computation increment
dt=0.05;
%calculate the number of steps
ntstep=T_total/dt+1;
%define Input Waves
alphaa=2; %velocity of wave a
fa=1; %frequency of wave a
ampa=1; %amplitude of wave a
alphab=2; %velocity of wave b
fb=1; %frequency of wave b
ampb=1; %amplitude of wave b
%Calc wave parameters using input
Ta=1/fa; %period of wave a
wa=2*pi*fa; %angular frequency of wave a
la=Ta*alphaa;%wavelength of wave a
ka=2*pi/la; %wavenumber of wave a
Tb=1/fb; %period of wave b
wb=2*pi*fb; %angular frequency of wave b
lb=Tb*alphab;%wavelength of wave b
kb=2*pi/lb; %wavenumber of wave b
x=(-20:0.1:20); %define the x coordinates
y=(-20:0.1:20); %define the y coordinates
t=zeros(length(x),length(y)); %start time at each point in x by y grid (zero everywhere)
%setup the figure
figure(1);
%label axes
hold on
[X,Y]=meshgrid(x,y); %create the mesh: matrix X contains x-by-y values of x, Y contains x-by-y values of y
R1=sqrt(X.^2+Y.^2); %define the distances to all points on the x-by-y grid.
R2=sqrt((X-5).^2+(Y+5).^2); %Offsets secondary source
%loop over nsamp
for n=1:ntstep
t=t+dt; %time
arga=wa*t-ka*R1; %argument of wave a
a=ampa*sin(arga)./max(1,sqrt(R1)); %amplitude of wave a
a(R1>t.*alphaa)=0; %causality condition for a
a(R1<t.*alphaa-la/2)=0; %limit length of input wave a to one half cycle
argb=wb*t-kb*R2; %argument of wave a
b=ampb*sin(argb)./max(1,sqrt(R2)); %amplitude of wave a
b(R2>t.*alphab)=0; %causality condition for a
b(R2<t.*alphab-lb/2)=0; %limit length of input wave a to one half cycle
%set up the figure
figure(1);
hold off;
surf(X,Y,a+b); %plot the sum of the waves a and b at all locations defined by [X,Y]
axis square; %set square axes
shading INTERP; %interpolate the shading
caxis([0,1]); %define the limits of the colour bar
c=colorbar; %draw the colour bar
ylabel(c,'Amplitude (m)'); % label the colour bar
view (2); %set the view to aerial
hold on;
%label axes
xlabel('Distance x (m)');
ylabel('Distance y (m)');
%limit axes
xlim([x(1), x(length(x))]);
ylim([x(1), y(length(y))]);
zlim([0,ampa]);
title(strcat('time =', num2str(t(1)),' s'));
%pause (dt); % can pause each increment of the simulation for slower
%run-time
end
0 Kommentare
Antworten (1)
Ayush Aniket
am 28 Jan. 2025 um 10:28
To automate the generation of multiple wave sources along a diagonal line and introduce a time delay between each, you can define a function that generates waves at specified offsets and applies a time delay for each new wave. Refer to the code snippet below:
function simulateWaves(N, T_total, dt, alpha, f, amp, offset)
% N: Number of sources
% T_total: Total simulation time
% dt: Time increment
% alpha: Velocity of waves
% f: Frequency of waves
% amp: Amplitude of waves
% offset: [x, y] offset for each subsequent source
% Calculate wave parameters
T = 1/f;
w = 2*pi*f;
l = T*alpha;
k = 2*pi/l;
% Define grid
x = (-20:0.1:20);
y = (-20:0.1:20);
[X, Y] = meshgrid(x, y);
% Initialize time
t = zeros(length(x), length(y));
ntstep = T_total/dt + 1;
% Setup figure
figure;
hold on;
% Loop over time steps
for n = 1:ntstep
t = t + dt; % Increment time
% Initialize wave field
waveField = zeros(size(X));
% Loop over each source
for i = 0:(N-1)
% Calculate offset for current source
xOffset = i * offset(1);
yOffset = i * offset(2);
% Calculate distance from current source
R = sqrt((X - xOffset).^2 + (Y - yOffset).^2);
% Calculate wave argument and amplitude
arg = w * (t - i * dt) - k * R;
wave = amp * sin(arg) ./ max(1, sqrt(R));
% Apply causality condition
wave(R > (t - i * dt) * alpha) = 0;
wave(R < (t - i * dt) * alpha - l/2) = 0;
% Add to wave field
waveField = waveField + wave;
end
% Plot the wave field
hold off;
surf(X, Y, waveField);
axis square;
shading interp;
caxis([0, amp]);
view(2);
xlabel('Distance x (m)');
ylabel('Distance y (m)');
title(sprintf('time = %.2f s', t(1)));
% Pause for visualization
pause(dt);
end
end
% Usage
simulateWaves(5, 5, 0.05, 2, 1, 1, [5, -5]); % Example with 5 sources
The function takes the number of sources N, total simulation time T_total, time increment dt, wave parameters (alpha, f, amp) and the offset for each new source. For each time step, the code calculates the wave propagation for each source, applying a time delay (i * dt) for each subsequent source, thus creating a domino effect.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Graph and Network Algorithms finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!