Need to make surf plot in ode45

2 visualizaciones (últimos 30 días)
Suganya G
Suganya G el 30 de Oct. de 2021
Editada: Chris el 30 de Oct. de 2021
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

Respuesta aceptada

Chris
Chris el 30 de Oct. de 2021
Editada: Chris el 30 de Oct. de 2021
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 comentarios
Walter Roberson
Walter Roberson el 30 de Oct. de 2021
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 el 30 de Oct. de 2021
It is working. Thanks a lot!!

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 30 de Oct. de 2021
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 comentario
Suganya G
Suganya G el 30 de Oct. de 2021
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!!

Iniciar sesión para comentar.

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by