0
votes

I am trying to model the equation y" + 4y = 0, with initial conditions y(0) = 1 and y'(0) = 0, in Matlab. The snippet of code below shows a working approximation of the position as a function of time for the above ODE:

clear      
syms y(t)

%Differential Equation Conversion
P = odeToVectorField(diff(y, 2) == -4*y);
M = matlabFunction(P,'vars',{'t','Y'});

%Position Approximation
pos_solution = ode45(M,[0 20],[1 0]);
x = linspace(0,20,1000);
y = deval(pos_solution,x,1);

%Plot of Position as a function of time
figure(3)
plot(x,y,'b');

I am trying to take this data produced and find the velocity of the system as a function of time, but have no idea how to do so. Any help with this would be appreciated.

1
"the waveform approximation for the velocity of the system" ... I don't know what this is nor what y represents. - TroyHaskin
y = deval(pos_solution,x,1); returns the integral of y', i.e., y (or position if y represents that). If you want y' (velocity if y represents position, the integral of y''), then use y = deval(pos_solution,x,2); as per the documentation. Or just use y = deval(pos_solution,x); to return both together. - horchler
Thank you so much @horchler! I didn't really understand fully what the documentation was talking about with deval(XINT,SOL,IDX). That clears it up for me! Much appreciated. - Alex Walker

1 Answers

0
votes

The optional third argument to deval specifies which index of the solution to return. The index corresponds to the state vector (and initial conditions) and the output of your ODE integration function (M). In the particular case of your system, index 1 corresponds to the position and index 2 to the velocity. If you want output (and plot) both, you can omit the third argument:

...
y = deval(pos_solution,x);

% Plot of Position as a function of time
figure(3)
plot(x,y(1,:),'b',x,y(2,:),'r');
legend('Position','Velocity');