Need to make surf plot in ode45
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
Accepted Answer
More Answers (1)
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
1 Comment
Suganya G
on 30 Oct 2021
Categories
Find more on Data Type Identification 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!

