필터 지우기
필터 지우기

Need to make surf plot in ode45

조회 수: 1 (최근 30일)
Suganya G
Suganya G 2021년 10월 30일
편집: Chris 2021년 10월 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에서 Surface and Mesh Plots에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by