2D Plot changing in a for loop - Want to plot in 3D

조회 수: 5 (최근 30일)
Nathan Ray
Nathan Ray 2022년 11월 17일
답변: Voss 2022년 11월 19일
Hello! Newish to MATLAB and struggling with what is probably something simple. I've got a function 'v(x)' that changes in a for loop. I'd like to make a 3D plot of v(x,m), where m is the index of the loop. I didn't write this whole code - I've made some adjustments but most is taken from this paper: https://www.researchgate.net/publication/281441538_An_introduction_to_the_Split_Step_Fourier_Method_using_MATLAB. (Remember, always cite your sources!)
For reference, I've got:
N = 512; % Number of Fourier modes
dt = .001; % Time step
tfinal = 10; % Final time
M = round(tfinal./dt); % Total number of time steps
L = 50; % Space period
h = L/N; % Space step
n =( -N/2:1:N/2-1); % Indices
x = n*h; % Grid points
u = exp(1i*x).*sech(x); % Intial condition
k = 2*n*pi/L; % Wavenumbers.
for m = 1:1:M % Start time loop
u = exp(dt*1i*(abs(u).*abs(u))).*u; % Solve non-linear part of NLSE
c = fftshift(fft(u)); % Take Fourier transform
c = exp(-dt*1i*k.*k/2).*c; % Advance in Fourier space
u = ifft(fftshift(c)); % Return to Physical Space
v = sqrt(abs(u).*abs(u)); % Normalize NLSE Solution
plot(x,v);
hold on
end
The 2D plot this generates is basically what I'm looking for - I just need to spread it out in the 'm' dimension. Any help is appreciated!

답변 (1개)

Voss
Voss 2022년 11월 19일
Something like this?
N = 512; % Number of Fourier modes
dt = .001; % Time step
tfinal = 10; % Final time
M = round(tfinal./dt); % Total number of time steps
L = 50; % Space period
h = L/N; % Space step
n =( -N/2:1:N/2-1); % Indices
x = n*h; % Grid points
u = exp(1i*x).*sech(x); % Initial condition
k = 2*n*pi/L; % Wavenumbers.
v_all = zeros(N,M);
for m = 1:M % Start time loop
u = exp(dt*1i*(abs(u).*abs(u))).*u; % Solve non-linear part of NLSE
c = fftshift(fft(u)); % Take Fourier transform
c = exp(-dt*1i*k.*k/2).*c; % Advance in Fourier space
u = ifft(fftshift(c)); % Return to Physical Space
v = sqrt(abs(u).*abs(u)); % Normalize NLSE Solution
% plot(x,v);
% hold on
v_all(:,m) = v;
end
surf(v_all,'EdgeColor','none')
xlim([1 M])
ylim([1 N])

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by