Hi guys, my code doesn't run i couldn't debug this, could you anyone help me?

1 visualización (últimos 30 días)
% Constants and parameters
alpha = 2; % Shear wave velocity
beta = 1; % Compressional wave velocity
frequencies = 0:15; % Range of frequencies
epsilon = 1e-10; % Small epsilon value
ro = 1; % Define the value of ro
% Define the source and receiver locations
source = [0, 0, 0]; % Source location (x, y, z)
stations = [0.01, 0, 0; 0.02, 0, 0; 0.03, 0, 0; 0.04, 0, 0; 0.05, 0, 0]; % Receiver locations (x, y, z)
% Calculate -Im G11 for different frequencies and stations
results = zeros(size(stations, 1), length(frequencies));
for i = 1:size(stations, 1)
receiver = stations(i, :); % Receiver location (x, y, z)
distance = norm(receiver - source); % Euclidean distance between source and receiver
gamma = (receiver - source) / distance; % Unit vector from source to receiver
gamma = repmat(gamma, length(frequencies), 1); % Repeat gamma for each frequency
for j = 1:length(frequencies)
omega = 2 * pi * frequencies(j);
q = omega / alpha;
f1 = (beta^2 / alpha^2) * (1 - 1j * 2 ./ (q * distance + epsilon) - 2 ./ (q^2 * distance^2 + epsilon)) * exp(-1j * q * distance);
f2 = (beta^2 / alpha^2) * (1j ./ (q * distance + epsilon) + 1 ./ (q^2 * distance^2 + epsilon)) * exp(-1j * q * distance);
results(i, j) = calculate_G11_imag([f1, f2], gamma(j, :), omega, ro, alpha, beta);
end
end
ans = 1×2
1 2
ans = 1×2
1 3
Arrays have incompatible sizes for this operation.

Error in solution>calculate_G11_imag (line 48)
result = -omega / (12 * pi * ro) * (1 / (alpha^3) + 2 / (beta^3)) * sum(f .* gamma);
% Plotting
figure;
for i = 1:size(stations, 1)
plot(frequencies, -imag(results(i, :)), 'DisplayName', sprintf('Station: %s', mat2str(stations(i, :))));
hold on;
end
hold off;
xlabel('Frequency');
ylabel('-Im G11');
title('Variation of -Im G11 for Different Stations');
legend('Location', 'northeast');
grid on;
ylim([-1, 10]);
% Define the function to calculate -Im G11
function result = calculate_G11_imag(f, gamma, omega, ro, alpha, beta)
size(f)
size(gamma)
result = -omega / (12 * pi * ro) * (1 / (alpha^3) + 2 / (beta^3)) * sum(f .* gamma);
end

Respuestas (2)

Image Analyst
Image Analyst el 29 de Mayo de 2023
Put the fuunction definition at the END of your script, not near the beginning. Then you need to CALL IT, passing it all the arguments it needs.
Here is the debugging help you asked for:

Torsten
Torsten el 29 de Mayo de 2023
Movida: Torsten el 29 de Mayo de 2023
When you call "calculate_G11_imag", "f "has size 1x2 and "gamma" has size 1x3. Thus f .* gamma is not defined and sum(f .* gamma) cannot be evaluated.
See above for details.

Categorías

Más información sobre Gamma Functions en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by