Filter löschen
Filter löschen

I can not solve the errors

4 Ansichten (letzte 30 Tage)
Kathleen
Kathleen am 12 Sep. 2023
Beantwortet: Walter Roberson am 12 Sep. 2023
Hi,
I am trying to get some plots but do not understand the errors I get. Thank you.
The code is:
%HW3 Kathleen Van Wesenbeeck
clear; close all; clc
% Parameters Setup
%Parameters Setup:
% `N`: The number of layers in the subsurface model.
% `M`: The number of data points to be generated, which is a multiple of `N`.
% `dz`: The thickness of each layer in the subsurface model.
% `s`: The standard deviation of data noise.
% `max_depth`: The maximum depth of the model domain, calculated based on the number of layers and layer thickness.
N = 3; % Number of layers
M=N*7; % Number of data points
% Layer thickness
dz = 2
dz = 2
s = 5e-5
s = 5.0000e-05
% Data noise standard deviation
max_depth = N * dz; % Maximum depth of the model domain
rng(1); % Set random seed for reproducibility
% The `rng(1)` command sets the random seed to ensure reproducibility.
% Random numbers are then used to generate sensor depths, which are sorted in ascending order.
% Generate Sensor Depths
sensor_depths = sort(rand(1, M) * max_depth); % Generate random sensor depths and sort them
% Generate True Velocity Model and True Data
zm = dz (dz/2) + (0:N-1) * dz
zm = 1×3
2 4 6
% Layer midpoints
% A true velocity model is defined using a linear relationship with layer midpoints.
% This model is used to generate true data without any noise.
true_velocity_model = 1000 + 80 * zm; % True velocity model
%% Generate true data without noise
true_data = true_velocity_model * ones(1, M);
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix individually, use TIMES (.*) for elementwise multiplication.
% Build the G-Matrix
% The G-matrix is constructed based on the relationship between sensor depths and layer boundaries. Logical operators are used to assign rows of the G-matrix.
% Each row of the G-matrix corresponds to a data point, and each column corresponds to a layer in the subsurface.
G = zeros(M, N); % Initialize G-matrix
for i = 1:N
layer_top = (i - 1) * dz;
layer_bottom = i * dz;
% Use logical operator to assign rows of G-matrix
G(:, i) = (sensor_depths >= layer_top) & (sensor_depths < layer_bottom);
end
%%
% Add Noise to True Data
% Gaussian noise with the specified standard deviation (`s`) is added to the true data to simulate real-world measurement errors.
noise = s * randn(1, M); % Generate noise
noisy_data = true_data + noise; % Add noise to true data
%%
% Perform Least-Squares Inversion
% The pseudoinverse of the G-matrix is computed to invert for the slowness model.
% This inversion estimates the slowness (reciprocal of velocity) of each layer in the subsurface.
slowness_model = pinv(G) * noisy_data;
% pinv = moore - penrose pseudoinverse = a matrix that can act as a partial replacement for the matrix inverse in cases where it does not exist. This matrix is frequently used to solve a system of linear equations when the system does not have a unique solution or has many solutions.
% we get the pseudoinverse by taking the reciprocal of each non-zero element on the diagonal, leaving the zeros in place, and then transposing the matrix.
% Calculate velocity model from slowness model
estimated_velocity_model = 1 ./ slowness_model;
%%
% Plot the Results
figures;
%%
subplot(2, 2, 1);
plot(true_velocity_model, zm, 'b', 'LineWidth', 2);
title('True Velocity Model');
xlabel('Velocity (m/s)');
ylabel('Depth (m)');
%%
subplot(2, 2, 2);
plot(noisy_data, sensor_depths, 'r.', 'MarkerSize', 10);
title('Noisy Data');
xlabel('Data (yec dn)');
ylabel('Depth (m)');
%%
subplot(2, 2, 3);
plot('estimated_velocity_model', zm, 'g', 'LineWidth', 2);
title('Estimated Velocity Model');
xlabel('Velocity (m/s)');
ylabel('Depth (m)');
%%
subplot(2, 2, 4);
plot(true_velocity_model, zm, 'b', 'LineWidth', 2);
hold on;
plot('estimated_velocity_model', zm, 'g', 'LineWidth', 2);
legend('True Model', 'Estimated Model');
title('Comparison of True and Estimated Velocity Models');
xlabel('Velocity (m/s)');
ylabel('Depth (m)');
  2 Kommentare
Kathleen
Kathleen am 12 Sep. 2023
this is the first error: Error in HW3_inverse_KVW (line 55)
true_data = true_velocity_model * ones(1, M);
Dyuman Joshi
Dyuman Joshi am 12 Sep. 2023
You are trying to multiply a 1x3 array with a 1x21 array, which is not possible.
Did you want to use ones(1,N)?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

the cyclist
the cyclist am 12 Sep. 2023
The direct cause of the error you are seeing is that in the line that gives the error, you are trying to matrix-multiply a 1x3 vector with a 1x21 vector.
It is not clear to me what you intend here, so I cannot offer specific advice on a solution.
You may benefit from reading this documentation on how to debug MATLAB code.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 12 Sep. 2023
If the intent is to replicate the entries then
true_data = true_velocity_model(:) * ones(1, M);
That would be 3 x 1 * 1 x 21 which would give a 3 x 21 result.

Tags

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by