Surface Plot for ODE solution

7 ビュー (過去 30 日間)
Folke Voß
Folke Voß 2019 年 4 月 22 日
コメント済み: Star Strider 2022 年 12 月 7 日
How can I make a surface plot for the ODE solution for different constants z?
w = 1;
k=1;
figure
tspan = [0 5];
for z=0.1:0.01:0.5
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;
surf(t,z,gs(:,2))
end
  2 件のコメント
T K
T K 2022 年 12 月 7 日
Please, when I run this code, this problem appears.
Error in Untitled7 (line 15)
surf(t,zv,gs2')
Star Strider
Star Strider 2022 年 12 月 7 日
@T K — Check to see if you have a variable or user function called ‘surf’ by running this from a script or your Command Window —
which surf -all
/MATLAB/toolbox/matlab/graph3d/surf.m
It should give only this result. If it returns anything else, that points to the problem. The solution is to rename the other function or variable.
.

サインインしてコメントする。

採用された回答

Star Strider
Star Strider 2019 年 4 月 22 日
You need to make a few changes in your 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')
Experiment to get the result you want.
  2 件のコメント
Folke Voß
Folke Voß 2019 年 4 月 22 日
Thank you a lot Star Strider.
The code is working the way I wanted to.
Star Strider
Star Strider 2019 年 4 月 22 日
As always, my pleasure.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by