How to plot the first derivative of solution?

10 views (last 30 days)
Nauryzbay on 30 May 2023
Commented: Nauryzbay on 1 Jun 2023
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;

James Tursa on 30 May 2023
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 CommentsShow NoneHide None
Steven Lord on 30 May 2023
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 on 30 May 2023
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 on 30 May 2023
Edited: Torsten on 30 May 2023
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
Nauryzbay on 1 Jun 2023
Thank you so much.