unable to plot 2 graphs

1 次查看(过去 30 天)
Kathleen
Kathleen 2024-2-29
编辑: Voss 2024-2-29
I coded it but can not figure out how to plot it. I will attach an example of the graph that I should get as result.
%normal modes of string section 2.25 Stein/Wysession
clear; close all; clc
L = 20 %string length (m)
v = 3 %string wave velocity (m/s)
xs = 10 %src x-crd (m)
dx = 0.1 %spacing of x-grid
dt = 0.1 %spacing of t-grid
tmax = 6 %lime length
tau = 0.1 %source pulse width (s)
psca = 0.5; %plot scale
x = [ 0: dx : L]; %discrete x
nx = length(x);
t = [0:dt:tmax]; %discrete time
nt = length(t);
n = 1:50; %mode integers
nn = length(n);
fprintf('nt=%d nx=%d nn=%d\n\n', nt, nx, nn)
w = n * pi * v / L; %discrete angular eigen-frequencies
F = exp( -(w*tau).^2 / 4); %source pulse
u = zeros(nt,nx,nn); uu = zeros(nt,nx);
%%
for i = 1 : nt %time
for j = 1 : nx %space
for k = 1: nn %modes done in serially inside triple loops
u(i,j,k) = sin( n(k) *pi*xs/L)*F(k) * sin( n(k) *pi* x(j) /L)*cos( w(k) *t(i) );
end
uu(i,j) = sum( u(i,j,:) ); %sum over modes to get uu space/time matrix
end
end
%%
subplot(1,3,1)
set(gcf,'position'); colormap('turbo')
set(gca,'ydir','normal','TickDir','out');
xlabel ('x(m)');ylabel ('modes (n)'); title('mode')
%%
subplot(1,3,2)
set(gcf,'position');
set(gca,'ydir','normal','TickDir','out');
xlabel('x(m)'); ylabel('time (s)'); title('wavefield')
%%
subplot(1,3,3)
set(gcf,'position', [0 50 150 200]); colormap('turbo')
imagesc(x,t,uu); colorbar;
set(gca,'ydir','normal','TickDir','out');
xlabel('x(m)'); ylabel('time (s)'); title('wavefield')

采纳的回答

Voss
Voss 2024-2-29
编辑:Voss 2024-2-29
Maybe something like this:
%normal modes of string section 2.25 Stein/Wysession
% clear; close all; clc
L = 20; %string length (m)
v = 3; %string wave velocity (m/s)
xs = 10; %src x-crd (m)
dx = 0.1; %spacing of x-grid
dt = 0.1; %spacing of t-grid
tmax = 6; %lime length
tau = 0.1; %source pulse width (s)
psca = 0.5; %plot scale
x = 0:dx:L; %discrete x
nx = length(x);
t = 0:dt:tmax; %discrete time
nt = length(t);
n = 1:50; %mode integers
nn = length(n);
fprintf('nt=%d nx=%d nn=%d\n\n', nt, nx, nn)
nt=61 nx=201 nn=50
w = n * pi * v / L; %discrete angular eigen-frequencies
F = exp( -(w*tau).^2 / 4); %source pulse
u = zeros(nt,nx,nn);
uu = zeros(nt,nx);
%%
for i = 1 : nt %time
for j = 1 : nx %space
for k = 1: nn %modes done in serially inside triple loops
u(i,j,k) = sin( n(k) *pi*xs/L)*F(k) * sin( n(k) *pi* x(j) /L)*cos( w(k) *t(i) );
end
uu(i,j) = sum( u(i,j,:) ); %sum over modes to get uu space/time matrix
end
end
%%
subplot(1,3,1)
u_sum = permute(sum(u,1),[2 3 1]);
scale = 1;
u_sum_norm = scale*u_sum./max(abs(u_sum),[],'all');
u_sum_norm_shift = u_sum_norm+(1:nn);
plot(x,u_sum_norm_shift)
set(gca,'ydir','normal','TickDir','out');
xlabel('x(m)');
ylabel('modes (n)');
title('mode')
ylim([1 nn])
%%
subplot(1,3,2)
scale = 1;
uu_norm = scale*uu./max(abs(uu),[],'all');
uu_norm_shift = uu_norm+(1:nt).';
plot(x,uu_norm_shift.','k')
set(gca,'ydir','normal','TickDir','out');
xlabel('x(m)');
ylabel('time (s)');
title('wavefield')
ylim([1 nt])
%%
subplot(1,3,3)
% set(gcf,'position', [0 50 150 200]);
colormap('turbo')
imagesc(x,t,uu);
colorbar;
set(gca,'ydir','normal','TickDir','out');
xlabel('x(m)');
ylabel('time (s)');
title('wavefield')

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

标签

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by