How to plot the first derivative of solution?

조회 수: 2 (최근 30일)
Nauryzbay
Nauryzbay 2023년 5월 30일
댓글: Nauryzbay 2023년 6월 1일
function Piecewise1111copy
x1=1;
u=3;
global gamma1;
gamma1=x1;
teta=zeros(3,1);
teta(1)=0;
for i=1:3
teta(i+1)=2*i;
end
for j=1:1
for k=1:u%initial_func=[x1,x2];
[t,x] = ode45(@hop4,teta(k):0.0001:teta(k+1),x1(j));
n=length(t);
x1(j)=x(n,1);
gamma1=x1(j);
hold on
figure(1)
subplot(2,1,1);
plot(t,x(:,1),'color','g','Linewidth',1)
xlabel('$$t$$','interpreter','latex','fontsize',16); ylabel('$$y$$','interpreter','latex','fontsize',16);
hold on
subplot(2,1,2);
syms x(t);
y=diff(x,t);
fplot(y,[0,6],'color','g','Linewidth',1);
xlabel('$$t$$','interpreter','latex','fontsize',16); ylabel('$$\dot{y}$$','interpreter','latex','fontsize',16);
hold on
end
end
function dx=hop4(t,x)
global gamma1;
dx(1)=1+3*gamma1;

답변 (2개)

James Tursa
James Tursa 2023년 5월 30일
After the ode45( ) call, simply pass your x solution through your derivative function to obtain the xdot values. You can either do this with a single call if your derivative function is vectorized, or you can do it in a loop.
  댓글 수: 2
Steven Lord
Steven Lord 2023년 5월 30일
The fact that the ODE function uses a global variable (rather than passing additional parameters into the ODE function) likely will complicate that approach.
James Tursa
James Tursa 2023년 5월 30일
As ugly as that global variable is, I don't see it being changed during the derivative call, so I don't see how that complicates calling the derivative function right after the ode45( ) call to get the derivatives.

댓글을 달려면 로그인하십시오.


Torsten
Torsten 2023년 5월 30일
편집: Torsten 2023년 5월 30일
Piecewise1111copy()
function Piecewise1111copy
x1=1;
u=3;
teta=zeros(3,1);
teta(1)=0;
for i=1:3
teta(i+1)=2*i;
end
for j=1:1
for k=1:u%initial_func=[x1,x2];
[t,x] = ode15s(@(t,x)hop4(t,x,x1),[teta(k),teta(k+1)],x1);
hold on
figure(1)
subplot(2,1,1);
plot(t,x(:,1),'color','g','Linewidth',1)
xlabel('$$t$$','interpreter','latex','fontsize',16); ylabel('$$y$$','interpreter','latex','fontsize',16);
hold on
subplot(2,1,2);
for i=1:numel(t)
y(i) = hop4(t(i),x(i,:),x1);
end
plot(t,y,'color','g','Linewidth',1);
xlabel('$$t$$','interpreter','latex','fontsize',16); ylabel('$$\dot{y}$$','interpreter','latex','fontsize',16);
hold on
n=length(t);
x1=x(n,1);
end
end
end
function dx=hop4(t,x,x1)
dx(1)=1+3*x1;
end

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by