I can not solve the errors
古いコメントを表示
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
s = 5e-5
% 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
% 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);
% 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 件のコメント
Kathleen
2023 年 9 月 12 日
Dyuman Joshi
2023 年 9 月 12 日
You are trying to multiply a 1x3 array with a 1x21 array, which is not possible.
Did you want to use ones(1,N)?
採用された回答
その他の回答 (1 件)
Walter Roberson
2023 年 9 月 12 日
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.
カテゴリ
ヘルプ センター および File Exchange で Image Arithmetic についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!