Need to make surf plot in ode45

2 次查看(过去 30 天)
How to give command for making surf plot in ode45. I have coupled nonlinear ODE system. I need to run a surface diagram for variables using any 2 parameters. The following is the code for 2D plots. Please help me to run surf plot in MATLAB.
function ode
options = odeset('RelTol',1e-6,'Stats','on');
%initial conditions
Xo = [0.5;0.7;2];
tspan =linspace(0,100);
tic
[t,X] = ode45(@TestFunction,tspan,Xo,options);
toc
figure
plot(t, X(:,1), 'red')
plot(t, X(:,2), 'blue')
plot(t, X(:,3), 'red')
return
function [dx_dt]= TestFunction(~,x)
r=0.05; k=0.1; a=0.02; m=0.02; b=0.2; eta=0.06;
dx_dt(1)=r.*x(1).*(1-(x(1)./k))-a.*x(1).*x(3)+x(3).*eta;
dx_dt(2)=a.*x(1).*x(3)-m.*x(2)-b.*x(2)+h.*eta;
dx_dt(3)=b.*x(2)-eta.*x(3)-r.*x(1);
dx_dt = dx_dt';
return

采纳的回答

Chris
Chris 2021-10-30
编辑:Chris 2021-10-30
options = odeset('RelTol',1e-6,'Stats','on');
%initial conditions
Xo = [0.5;0.7;2];
% Choose parameters t and a?
tspan =linspace(0,100);
a_vec = 0.01:0.005:0.03;
for idx = 1:numel(a_vec)
% Collect all X values into a 3D matrix. Additional parameters to
% TestFunction can be added after ode45 options
[t,X(:,:,idx)] = ode45(@TestFunction,tspan,Xo,options,a_vec(idx));
end
34 successful steps 0 failed attempts 205 function evaluations 34 successful steps 0 failed attempts 205 function evaluations 34 successful steps 0 failed attempts 205 function evaluations 34 successful steps 0 failed attempts 205 function evaluations 34 successful steps 0 failed attempts 205 function evaluations
figure
plot(t, X(:,1), 'red')
hold on % keeps all three plots on the axes
plot(t, X(:,2), 'blue')
plot(t, X(:,3), 'red')
figure
tiledlayout(1,3)
nexttile
% Make a surface for each X0
surf(a_vec,t,squeeze(X(:,1,:)))
% Squeeze removes dimensions of size 1, turning this slice into a 2d matrix
nexttile
surf(a_vec,t,squeeze(X(:,2,:)))
nexttile
surf(a_vec,t,squeeze(X(:,3,:)))
function [dx_dt]= TestFunction(~,x,a)
r=0.05; k=0.1; %a=0.02;
m=0.02; b=0.2; eta=0.06; h=1;
dx_dt(1)=r.*x(1).*(1-(x(1)./k))-a.*x(1).*x(3)+x(3).*eta;
dx_dt(2)=a.*x(1).*x(3)-m.*x(2)-b.*x(2)+h.*eta;
dx_dt(3)=b.*x(2)-eta.*x(3)-r.*x(1);
dx_dt = dx_dt';
end
  3 个评论
Walter Roberson
Walter Roberson 2021-10-30
tiledlayout is R2019b or later. You did not specify which MATLAB release you are using, so we are permitted to assume that you are more up to date than that.
You can make calls to subplot() to arrange plots.
Suganya G
Suganya G 2021-10-30
It is working. Thanks a lot!!

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2021-10-30
ode
37 successful steps 0 failed attempts 223 function evaluations Elapsed time is 0.086187 seconds.
function ode
options = odeset('RelTol',1e-6,'Stats','on');
%initial conditions
Xo = [0.5;0.7;2];
tspan =linspace(0,100);
tic
[t,X] = ode45(@TestFunction,tspan,Xo,options);
toc
figure
F = scatteredInterpolant(t, X(:,1), X(:,2));
minx1 = min(X(:,1)); maxx1 = max(X(:,1));
xvec = linspace(minx1, maxx1, 100);
[T, X] = meshgrid(t, xvec);
X2 = F(T, X);
surf(T, X, X2);
xlabel('t'); ylabel('x(:,1)'); zlabel('x(:,2)');
end
function [dx_dt]= TestFunction(~,x)
r=0.05; k=0.1; a=0.02; m=0.02; b=0.2; eta=0.06;
h = .1; %need SOME value
dx_dt(1)=r.*x(1).*(1-(x(1)./k))-a.*x(1).*x(3)+x(3).*eta;
dx_dt(2)=a.*x(1).*x(3)-m.*x(2)-b.*x(2)+h.*eta;
dx_dt(3)=b.*x(2)-eta.*x(3)-r.*x(1);
dx_dt = dx_dt';
end
  1 个评论
Suganya G
Suganya G 2021-10-30
Thanks for the relpy. Since I need x and y axis as parameters, I tried to change x(:,1) in terms of parameter a in this part
F = scatteredInterpolant(t, a_vec, X(:,2));
mina = 0.001; maxa = 0.003;
a_vec = linspace(mina, maxa, 100);
[T, X] = meshgrid(t, a_vec);
X2 = F(T, X);
surf(T, X, X2);
It didn't work. But I will use your code for in future work. Thanks!!

请先登录,再进行评论。

类别

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

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by