Azzera filtri
Azzera filtri

how do you offset curves?

28 visualizzazioni (ultimi 30 giorni)
Kathleen
Kathleen il 10 Set 2023
Commentato: Star Strider il 11 Set 2023
I am trying to get the same plots as in the picture. I struggle with plot b and e. For plot b, I am not sure how to offset curves for clarity. Should i not use Stairs cmd? I also need to change it from seconds to mili seconds. Where do I put in that conversion? The picture of the curves I am trying to get is attached. Ta!
My code is as follows:
%Exercise 1.3 Kathleen
clear; close all; clc
% a. To calculate the appropriate system matrix G. The G matrix is an upper-triangular matrix with nonzero entries of the data discretization interval (0.2).
set(0,'DefaultAxesLineWidth',2,'DefaultLineLineWidth',3)
set(0,'DefaultAxesXminorTick','on','DefaultAxesYminorTick','on')
set(0,'DefaultAxesFontSize',12,'DefaultAxesFontWeight','bold')
m = 100 %num data, rows
n = 100 %num model params (layers), columns
if m ~= n; fprintf('STOP: m ~= n \n'); return; end
delta_z = 0.2 %layer thickness (m)
data_std = 50e-6 %data noise std (sec)
z=linspace(0,100,n+1)';
z=z(2:end);
z_bottom = [delta_z:delta_z:delta_z*n] %bottom of layer
z_top = z_bottom-delta_z %top of layer
z_middle = z_bottom- delta_z/2 %middle of layer
%velocity gradient
g=40;
velocity_gradient = 40
velocity_0 = 1000
velocity_true = 1000 + 40*z_middle'
slowness_model_true = 1 ./ velocity_true %3B answer
%G matrix
G = tril(ones(m,n)) * delta_z
fprintf('condition(G) = %.2f \n\n', cond(G))
data_true = G * slowness_model_true
data_noisy = data_true + (data_std * randn(n,1))
model_inverse_noisy = inv(G' * G) * G' * data_noisy
data_predicted_noisy = G * model_inverse_noisy
model_inverse_no_noise = inv(G' * G) * G' * data_true
data_predicted_no_noise = G * model_inverse_no_noise
fprintf(' RMS(data_predicted - data_observed) = %.2e \n\n', rms((G * model_inverse_noisy) - data_noisy))
%%
data_true = G * slowness_model_true
data_noisy = data_true + (data_std * randn(n,1))
model_inverse_noisy = inv(G' * G) * G' * data_noisy
data_predicted_noisy = G * model_inverse_noisy
model_inverse_no_noise = inv(G' * G) * G' * data_true
data_predicted_no_noise = G * model_inverse_no_noise
fprintf(' RMS(data_predicted - data_observed) = %.2e \n\n', rms((G * model_inverse_noisy) - data_noisy)) % Formats based on root mean square method (?????)
%% figure(1)
subplot(2,2,2, 'align');
hold on
x= data_true
stairs(x, 'b-', 'linewidth', 2);
hold on
x= data_noisy
stairs(x, 'r-', 'linewidth', 2);
hold on
x= data_predicted_no_noise
stairs(x, 'y-', 'linewidth', 2);
hold on
x= data_predicted_noisy
stairs(x, 'g-', 'linewidth', 2);
set(gca, 'ydir', 'reverse');
axis tight;
grid on
legend('no-noise data', 'noisy data', 'predicted no-noise data', 'predicted noisy data')
xlabel('time (ms)')
xlim ([0 15])
ylabel('depth (m)')
title('(b) travel time data (Curves offset for clarity')
hold off
%% figure(2)
subplot(2,2,4, 'align');
set(gca, 'ydir', 'reverse');
axis tight;
grid on
legend('est model', 'est model', 'true model')
xlabel('slowness (s/m)')
ylabel('depth (m)')
title('(e) N=4 Inverse/true model')
%% figure(3)
% Slowness figure
set(gcf, 'position', [ 0 0 500 1000])
subplot(2,2,3, 'align'); hold on
x = [ model_inverse_noisy(1) model_inverse_noisy' ];
y = [ 0 z_bottom ];
stairs(x, y, 'g-', 'linewidth', 1);
x = [ slowness_model_true(1) slowness_model_true' ];
y = [ 0 z_bottom ];
stairs(x, y, 'b-', 'linewidth', 2);
x = [ model_inverse_no_noise(1) model_inverse_no_noise' ];
y = [ 0 z_bottom ];
stairs(x, y, 'r:', 'linewidth', 2);
set(gca, 'ydir', 'reverse');
axis tight;
grid on
legend('noisy data', 'true', 'no-noise')
xlabel('slowness (s/m)')
ylabel('depth (m)')
title('(c,d) Inverse/True Models')
%% figure (4)
subplot (2,2,1, 'align');
imagesc(G); colorbar
xlabel('N=100 model parameters (layer slowness)'); ylabel('M=100 data (travel-times)'); title('(a) Data Kernal G-Matrix M=N=100')

Risposte (2)

Star Strider
Star Strider il 10 Set 2023
Was my answer to your previous post How to invert axes? helpful?
In:
subplot(2,2,2, 'align');
if you want the time vector to have the appropriate units, you need to specify it, so:
stairs(t, x, 'b-', 'linewidth', 2);
instead of:
stairs(x, 'b-', 'linewidth', 2);
Then the time vector (‘t’ here) can have any values you want. It is not limited to the index values that otherwise are used as the independent varriable.
I am not sure what you intend by ‘offset curves’. I would just plot them using different line styles or line widths, or both, or plot them in different axes if they overlap. If the dependent variables have different independent variables (for example time instants), that should distinguish the curves and offset them.
I do not see any specific problems using the stairs function if it does what you want.
.
  2 Commenti
Kathleen
Kathleen il 10 Set 2023
thank you. I am looking to replicate graph b and e.
Star Strider
Star Strider il 11 Set 2023
My pleasure!
You need to give each of them slightly different time values. Right now, they do not have any time (independent variable) values.
Example —
%Exercise 1.3 Kathleen
clear; close all; clc
% a. To calculate the appropriate system matrix G. The G matrix is an upper-triangular matrix with nonzero entries of the data discretization interval (0.2).
set(0,'DefaultAxesLineWidth',2,'DefaultLineLineWidth',3)
set(0,'DefaultAxesXminorTick','on','DefaultAxesYminorTick','on')
set(0,'DefaultAxesFontSize',12,'DefaultAxesFontWeight','bold')
m = 100; %num data, rows
n = 100; %num model params (layers), columns
if m ~= n; fprintf('STOP: m ~= n \n'); return; end
delta_z = 0.2; %layer thickness (m)
data_std = 50e-6; %data noise std (sec)
z=linspace(0,100,n+1)';
z=z(2:end);
z_bottom = [delta_z:delta_z:delta_z*n]; %bottom of layer
z_top = z_bottom-delta_z; %top of layer
z_middle = z_bottom- delta_z/2; %middle of layer
%velocity gradient
g=40;
velocity_gradient = 40;
velocity_0 = 1000;
velocity_true = 1000 + 40*z_middle';
slowness_model_true = 1 ./ velocity_true; %3B answer
%G matrix
G = tril(ones(m,n)) * delta_z;
fprintf('condition(G) = %.2f \n\n', cond(G))
condition(G) = 127.95
data_true = G * slowness_model_true;
data_noisy = data_true + (data_std * randn(n,1));
model_inverse_noisy = inv(G' * G) * G' * data_noisy;
data_predicted_noisy = G * model_inverse_noisy;
model_inverse_no_noise = inv(G' * G) * G' * data_true;
data_predicted_no_noise = G * model_inverse_no_noise;
fprintf(' RMS(data_predicted - data_observed) = %.2e \n\n', rms((G * model_inverse_noisy) - data_noisy))
RMS(data_predicted - data_observed) = 3.66e-16
%%
data_true = G * slowness_model_true;
data_noisy = data_true + (data_std * randn(n,1));
model_inverse_noisy = inv(G' * G) * G' * data_noisy;
data_predicted_noisy = G * model_inverse_noisy;
model_inverse_no_noise = inv(G' * G) * G' * data_true;
data_predicted_no_noise = G * model_inverse_no_noise;
fprintf(' RMS(data_predicted - data_observed) = %.2e \n\n', rms((G * model_inverse_noisy) - data_noisy)) % Formats based on root mean square method (?????)
RMS(data_predicted - data_observed) = 3.66e-16
%% figure(1)
t = 0:numel(data_true)-1;
subplot(2,2,2, 'align');
hold on
x= data_true;
stairs(t, x, 'b-', 'linewidth', 2);
hold on
x= data_noisy;
stairs(t+0.3, x, 'r-', 'linewidth', 2);
hold on
x= data_predicted_no_noise;
stairs(t+0.6, x, 'y-', 'linewidth', 2);
hold on
x= data_predicted_noisy;
stairs(t+0.9, x, 'g-', 'linewidth', 2);
set(gca, 'ydir', 'reverse');
axis tight;
grid on
legend('no-noise data', 'noisy data', 'predicted no-noise data', 'predicted noisy data')
xlabel('time (ms)')
xlim ([0 15])
ylabel('depth (m)')
title('(b) travel time data (Curves offset for clarity')
hold off
return % STOP HERE
%% figure(2)
subplot(2,2,4, 'align');
set(gca, 'ydir', 'reverse');
axis tight;
grid on
legend('est model', 'est model', 'true model')
xlabel('slowness (s/m)')
ylabel('depth (m)')
title('(e) N=4 Inverse/true model')
%% figure(3)
% Slowness figure
set(gcf, 'position', [ 0 0 500 1000])
subplot(2,2,3, 'align'); hold on
x = [ model_inverse_noisy(1) model_inverse_noisy' ];
y = [ 0 z_bottom ];
stairs(x, y, 'g-', 'linewidth', 1);
x = [ slowness_model_true(1) slowness_model_true' ];
y = [ 0 z_bottom ];
stairs(x, y, 'b-', 'linewidth', 2);
x = [ model_inverse_no_noise(1) model_inverse_no_noise' ];
y = [ 0 z_bottom ];
stairs(x, y, 'r:', 'linewidth', 2);
set(gca, 'ydir', 'reverse');
axis tight;
grid on
legend('noisy data', 'true', 'no-noise')
xlabel('slowness (s/m)')
ylabel('depth (m)')
title('(c,d) Inverse/True Models')
%% figure (4)
subplot (2,2,1, 'align');
imagesc(G); colorbar
xlabel('N=100 model parameters (layer slowness)'); ylabel('M=100 data (travel-times)'); title('(a) Data Kernal G-Matrix M=N=100')
.

Accedi per commentare.


William Rose
William Rose il 10 Set 2023
Please post the simplest verion of the problem and the code that illustrate the problem you are trying to solve, to make it easier for others to helo you.
I saved the four vectors whose data is plotted in figure (b), to shorten my code. I am not sure why you use x for plotting, and I have not done so. Instead, I use data_true, data_noisy, etc., without assigning each to x. The code below is a simplified version of your code for figure b.
load('KatsData'); % load data_true, data_noisy, etc
figure; hold on
stairs(data_true, 'b-', 'linewidth', 2);
stairs(data_noisy, 'r-', 'linewidth', 2);
stairs(data_predicted_no_noise, 'y-', 'linewidth', 2);
stairs(data_predicted_noisy, 'g-', 'linewidth', 2);
set(gca, 'ydir', 'reverse');
axis tight; grid on
legend('no-noise', 'noisy', 'predicted no-noise', 'predicted noisy')
xlabel('time (ms)'); ylabel('depth (m)')
xlim ([0 15])
title('Kat''s (b): travel time')
You say you need to convert s to ms. But the horizontal axis values in the plot above are just the index number in the array. Therefore there are no time values to be converted. Did you mean that you need to convert the vertical values from km to m? YOu have listed the units as m in your y-axis label, but perhaps the actual data are in km, and you wan thte depths in m. If that is the case, then multply the "data_..." values by 1000 as you plot them. I have done this below to illustrate.
figure; hold on
stairs(1000*data_true, 'b-', 'linewidth', 2);
stairs(1000*data_noisy, 'r-', 'linewidth', 2);
stairs(1000*data_predicted_no_noise, 'y-', 'linewidth', 2);
stairs(1000*data_predicted_noisy, 'g-', 'linewidth', 2);
set(gca, 'ydir', 'reverse');
axis tight; grid on
legend('no-noise', 'noisy', 'predicted no-noise', 'predicted noisy')
xlabel('time (ms)'); ylabel('depth (m)')
xlim ([0 15])
title('(b): travel time')
You want to offset the data for clarity. Here is a way.
figure; hold on
stairs(1000*data_true, 'b-', 'linewidth', 2);
stairs(1000*data_noisy-0.2, 'r-', 'linewidth', 2);
stairs(1000*data_predicted_no_noise-0.4, 'y-', 'linewidth', 2);
stairs(1000*data_predicted_noisy-0.6, 'g-', 'linewidth', 2);
set(gca, 'ydir', 'reverse');
axis tight; grid on
legend('no-noise', 'noisy', 'predicted no-noise', 'predicted noisy')
xlabel('time (ms)'); ylabel('depth (m)')
xlim ([0 15])
title('(b): travel time, offset for clarity')
YOu asked if you should use stairs. That is completely up to you. It is just a fdifferent style of plot.

Categorie

Scopri di più su Programming in Help Center e File Exchange

Prodotti


Release

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by