Need to make surf plot in ode45
2 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Chris
on 30 Oct 2021
Edited: Chris
on 30 Oct 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
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 Comments
Walter Roberson
on 30 Oct 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.
More Answers (1)
Walter Roberson
on 30 Oct 2021
ode
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
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!