How to plot the first derivative of solution?
조회 수: 2 (최근 30일)
이전 댓글 표시
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;
댓글 수: 0
답변 (2개)
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
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
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
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 Center 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!