How to plot the first derivative of solution?

5 次查看(过去 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;

回答(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 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by