surface plotting.surface plot poroblem. Please I want help. An error appears when running

2 次查看(过去 30 天)
Please I want help. An error appears when running #code
w = 1; k=1; figure tspan = linspace(0, 5); % Create Constant ‘tspan’ zv=0.1:0.01:0.5; % Vector Of ‘z’ Values gs2 = zeros(numel(tspan), numel(zv)); % Preallocate for k = 1:numel(zv) z = zv(k); f = @(t,x) [-1i.*(2*w + 2*z).*x(1) + -1i.*sqrt(2).*k.*x(2);-1i.*sqrt(2).*k.*x(1) + -1i.*2*w*x(2)+-1i.*sqrt(2).*k.*x(3);-1i.*sqrt(2).*k.*x(2)+-1i.*2*w*x(3)]; [t,xa] = ode45(f,tspan,[0 1 0]); gs = abs(xa).^2; gs2(:,k) = gs(:,2); % Save Second Column Of ‘gs’ In ‘gs2’ Matrix end
figure surf(t,zv,gs2') grid on xlabel('t') ylabel('z') shading('interp')

回答(1 个)

Walter Roberson
Walter Roberson 2022-12-7
w = 1;
k=1;
figure
tspan = linspace(0, 5); %must be a vector of length 3 or more
zvals = 0.1:0.01:0.5;
numt = length(tspan);
numz = length(zvals);
gs = zeros(numt, 3, numz);
for zidx = 1 : numz
z = zvals(zidx);
f = @(t,x) [-1i.*(2*w + 2*z).*x(1) + -1i.*sqrt(2).*k.*x(2);-1i.*sqrt(2).*k.*x(1) + -1i.*2*w*x(2)+-1i.*sqrt(2).*k.*x(3);-1i.*sqrt(2).*k.*x(2)+-1i.*2*w*x(3)];
[t,xa] = ode45(f,tspan,[0 1 0]);
gs(:,:,zidx) = abs(xa).^2;
end
G = squeeze(gs(:,2,:)) .';
surf(t,zvals,G)

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by