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

2 views (last 30 days)
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')

Answers (1)

Walter Roberson
Walter Roberson on 7 Dec 2022
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)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!